diff --git a/README.md b/README.md index dbfa28f..860dc26 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,7 @@ Using this task, you can do the following: - `sake dev/tasks/MigrateTask down` 3. Make a new migration file for you with boilerplate code. - `sake dev/tasks/MigrateTask make:migration_name` - - **Note:** This file will be automatically placed in your project directory in the path `/code/migrations`. You can customize this location by defining a `MIGRATIONS_PATH` constant which should be the absolute path to the desired directory (either in your `_ss_environment.php` or `_config.php` files). Also, migration files that are automatically generated will be pseudo-namespaced with a `Migration_` prefix to help reduce possible class name collisions. + - **Note:** This file will be automatically placed in your project directory in the path `/src/migrations`. You can customize this location by defining a `MIGRATIONS_PATH` constant which should be the absolute path to the desired directory (either in your `_ss_environment.php` or `_config.php` files). Also, migration files that are automatically generated will be pseudo-namespaced with a `Migration_` prefix to help reduce possible class name collisions. ### How it Works diff --git a/code/DatabaseMigrations.php b/code/DatabaseMigrations.php index f76d4c7..0bd702a 100644 --- a/code/DatabaseMigrations.php +++ b/code/DatabaseMigrations.php @@ -1,5 +1,9 @@ "Varchar(255)", "MigrationClass" => "Varchar(255)", "Batch" => "Int", - "Stamp" => "SS_DateTime", + "Stamp" => "DBDatetime", ); - + private static $table_name = 'DatabaseMigrations'; } diff --git a/code/MigrateTask.php b/code/MigrateTask.php index c524f94..1a85e26 100644 --- a/code/MigrateTask.php +++ b/code/MigrateTask.php @@ -1,5 +1,18 @@ /code/migrations". + * IMPORTANT: This file will be automatically placed in your project directory in the path "/src/migrations". * This can be overridden by defining an absolute path in the constant "MIGRATION_PATH" in your _ss_environment.php file. * Migration files that are automatically generated will be pseudo-namespaced with a "Migration_" prefix to help reduce * possible class name collisions. @@ -45,8 +58,10 @@ class MigrateTask extends BuildTask { // Used for error reporting purposes. protected $lastMigrationFile = ''; + private static $segment = 'MigrateTask'; + /** - * @param SS_HTTPRequest $request + * @param HTTPRequest $request * @throws MigrationException */ public function run($request) { @@ -81,7 +96,7 @@ public function run($request) { register_shutdown_function(array($this, "shutdown")); // Determine action to take. Wrap everything in a transaction so it can be rolled back in case of error. - DB::getConn()->transactionStart(); + DB::get_conn()->transactionStart(); try { if (isset($args["up"])) { $this->up(); @@ -97,7 +112,7 @@ public function run($request) { } // Commit and clean up error state.. - DB::getConn()->transactionEnd(); + DB::get_conn()->transactionEnd(); $this->error = false; } catch (Exception $e) { @@ -125,7 +140,7 @@ public function shutdown(Exception $e = null) { if ($this->error && !$e) $e = new MigrationException("The migration" . ($this->lastMigrationFile ? " '$this->lastMigrationFile.php'" : "") . " terminated unexpectedly."); if ($e) { // Rollback database changes and notify user. - DB::getConn()->transactionRollback(); + DB::get_conn()->transactionRollback(); $this->output("ERROR" . ($e->getCode() != 0 ? " (" . $e->getCode() . ")" : "") . ": " . $e->getMessage()); $this->output("\nNote: Any database changes have been rolled back."); $this->output("\nStack Trace:"); @@ -143,7 +158,6 @@ public function isEnabled() { return Director::is_cli(); } - ######################## ## MAIN FUNCTIONALITY ## ######################## @@ -251,16 +265,18 @@ public function make($baseName) { } // Get boilerplate file contents, find/replace some contents and write to file path. - $sourceFile = __DIR__ . DIRECTORY_SEPARATOR . "MigrationBoilerplate.php"; - $sourceData = file_get_contents($sourceFile); - $sourceData = str_replace("MigrationBoilerplate", $camelCase, $sourceData); + $reflect = new ReflectionClass(MigrationBoilerplate::class); + $sourceData = str_replace( + [$reflect->getShortName(), 'namespace '.$reflect->getNamespaceName()], + [$camelCase, 'use '.Migration::class], + file_get_contents($reflect->getFileName()) + ); file_put_contents($filePath, $sourceData); // Output status and exit. $this->output("Created new migration: $filePath"); } - #################### ## HELPER METHODS ## #################### @@ -301,7 +317,7 @@ public static function getMigrationPath() { if (empty($project)) throw new MigrationException("Please either define a global '\$project' variable or define a MIGRATION_PATH constant in order to setup a path for migration files to live."); // Build path. - $migrationPath = join(DIRECTORY_SEPARATOR, array(BASE_PATH, $project, "code", "migrations")); + $migrationPath = join(DIRECTORY_SEPARATOR, array(BASE_PATH, $project, "src", "migrations")); } return $migrationPath; @@ -314,8 +330,8 @@ public static function getMigrationPath() { */ public static function getAllMigrations() { // Get all descendants of the abstract "Migration" class but ensure the class "MigrationBoilerplate" is skipped. - $manifest = SS_ClassLoader::instance()->getManifest(); - $classes = array_diff($manifest->getDescendantsOf("Migration"), array("MigrationBoilerplate")); + $manifest = ClassLoader::inst()->getManifest(); + $classes = array_diff($manifest->getDescendantsOf(Migration::class), array(MigrationBoilerplate::class)); $classesOrdered = array(); foreach ($classes as $className) { // Get actual filename of migration class and use that as the key. diff --git a/code/Migration.php b/code/Migration.php index 1a0b8e9..ef5d19a 100644 --- a/code/Migration.php +++ b/code/Migration.php @@ -1,5 +1,24 @@ up() and a ->down() * method. Migrations will be executed in alphanumeric order @@ -27,7 +46,6 @@ public function isObsolete() { return $this->obsolete; } - ####################################### ## DATABASE MIGRATION HELPER METHODS ## ####################################### @@ -92,8 +110,8 @@ public static function getTableForField($className, $field) { // Let's get our hands dirty on this ancestry filth and reference the database because the private static ::$db isn't reliable (seriously). $ancestors = ClassInfo::ancestry($className, true); foreach($ancestors as $ancestor) { - if (DataObject::has_own_table($ancestor)) { - if (DB::get_schema()->hasField($ancestor, $field)) return $ancestor; + if ($tableName = DataObject::getSchema()->tableForField($ancestor, $field)) { + return $ancestor; } } @@ -267,8 +285,8 @@ public static function setRowValuesOnTable($table, array $values, $id = null, $i public static function publish(SiteTree $page, $force = true) { try { static::whileAdmin(function () use ($page, $force) { - if (!$page->getIsModifiedOnStage() || $force) { - $page->doPublish(); + if (!$page->isModifiedOnDraft() || $force) { + $page->publishRecursive(); } else { $page->write(); } @@ -387,7 +405,7 @@ protected static function loginAsAdmin() { * @throws MigrationException */ public static function setPageType(SiteTree $page, $pageType) { - if (!is_a($pageType, "SiteTree", true)) throw new MigrationException("The specifed page type '$pageType' must be an instance (or child) of 'SiteTree'."); + if (!is_a($pageType, SiteTree::class, true)) throw new MigrationException("The specifed page type '$pageType' must be an instance (or child) of 'SiteTree'."); $page = $page->newClassInstance($pageType); static::publish($page); } @@ -502,11 +520,11 @@ public static function copyVersionedTable($fromObject, $toObject, array $fieldMa // Quick validation. foreach(array($fromObject, $toObject) as $validateObject) { if (!class_exists($validateObject)) throw new MigrationException("'$validateObject' doesn't appear to be an object."); - if (is_a($validateObject, 'DataObject')) throw new MigrationException("'$validateObject' must be an instance of DataObject."); + if (is_a($validateObject, DataObject::class)) throw new MigrationException("'$validateObject' must be an instance of DataObject."); /** @var $validateInstance DataObject */ $validateInstance = singleton($validateObject); - if (!$validateInstance->hasExtension('Versioned')) throw new MigrationException("'$validateObject' must be a versioned object (i.e. have the Versioned extension)."); + if (!$validateInstance->hasExtension(Versioned::class)) throw new MigrationException("'$validateObject' must be a versioned object (i.e. have the Versioned extension)."); } // Repeat on each instance of the objects' tables. diff --git a/code/MigrationBoilerplate.php b/code/MigrationBoilerplate.php index 19439c2..68e7994 100644 --- a/code/MigrationBoilerplate.php +++ b/code/MigrationBoilerplate.php @@ -1,5 +1,7 @@ 'Varchar(255)', ]; - + private static $table_name = 'Migration_TestParent'; } class Migration_TestChild extends Migration_TestParent implements TestOnly { @@ -107,7 +118,7 @@ class Migration_TestChild extends Migration_TestParent implements TestOnly { private static $db = [ 'ChildField' => 'Varchar(255)', ]; - + private static $table_name = 'Migration_TestChild'; } class Migration_TestGrandchild extends Migration_TestChild implements TestOnly { @@ -115,5 +126,5 @@ class Migration_TestGrandchild extends Migration_TestChild implements TestOnly { private static $db = [ 'Grandchild' => 'Varchar(255)', ]; - + private static $table_name = 'Migration_TestGrandchild'; } diff --git a/tests/MigrateTaskTest.yml b/tests/MigrateTaskTest.yml index 03e177a..da771ab 100644 --- a/tests/MigrateTaskTest.yml +++ b/tests/MigrateTaskTest.yml @@ -1,16 +1,16 @@ DatabaseMigrations: test1: BaseName: 2015_04_27_000637_test1.php - MigrationClass: Migration_UnitTestOnly + MigrationClass: PatrickNelson\SilverStripeMigrations\Tests\Migration_UnitTestOnly Batch: 1 Stamp: 1430107790 test2: BaseName: 2015_04_27_000638_test2.php - MigrationClass: Migration_UnitTestOnly + MigrationClass: PatrickNelson\SilverStripeMigrations\Tests\Migration_UnitTestOnly Batch: 2 Stamp: 1430107791 test3: BaseName: 2015_04_27_000639_test3.php - MigrationClass: Migration_UnitTestOnly + MigrationClass: PatrickNelson\SilverStripeMigrations\Tests\Migration_UnitTestOnly Batch: 2 Stamp: 1430107792