diff --git a/docs/guide/db-migrations.md b/docs/guide/db-migrations.md index e4f0a68..0027434 100644 --- a/docs/guide/db-migrations.md +++ b/docs/guide/db-migrations.md @@ -185,7 +185,7 @@ A list of all available methods for defining the column types is available in th Since version 2.0.7 migration console provides a convenient way to create migrations. -If the migration name is of a special form, for example `create_xxx` or `drop_xxx` then the generated migration +If the migration name is of a special form, for example `create_xxx` or `drop_xxx` then the generated migration file will contain extra code, in this case for creating/dropping tables. In the following all variants of this feature are described. @@ -193,13 +193,19 @@ In the following all variants of this feature are described. ```php yii migrate/create create_post -``` +``` generates ```php +/** + * Handles the creation for table `post`. + */ class m150811_220037_create_post extends Migration { + /** + * @inheritdoc + */ public function up() { $this->createTable('post', [ @@ -207,6 +213,9 @@ class m150811_220037_create_post extends Migration ]); } + /** + * @inheritdoc + */ public function down() { $this->dropTable('post'); @@ -215,43 +224,59 @@ class m150811_220037_create_post extends Migration ``` To create table fields right away, specify them via `--fields` option. - + ```php yii migrate/create create_post --fields="title:string,body:text" -``` +``` generates ```php +/** + * Handles the creation for table `post`. + */ class m150811_220037_create_post extends Migration { + /** + * @inheritdoc + */ public function up() { $this->createTable('post', [ 'id' => $this->primaryKey(), 'title' => $this->string(), - 'body' => $this->text() + 'body' => $this->text(), ]); } + /** + * @inheritdoc + */ public function down() { $this->dropTable('post'); } } + ``` You can specify more field parameters. ```php yii migrate/create create_post --fields="title:string(12):notNull:unique,body:text" -``` +``` -generates +generates ```php +/** + * Handles the creation for table `post`. + */ class m150811_220037_create_post extends Migration { + /** + * @inheritdoc + */ public function up() { $this->createTable('post', [ @@ -261,6 +286,9 @@ class m150811_220037_create_post extends Migration ]); } + /** + * @inheritdoc + */ public function down() { $this->dropTable('post'); @@ -271,14 +299,134 @@ class m150811_220037_create_post extends Migration > Note: primary key is added automatically and is named `id` by default. If you want to use another name you may > specify it explicitly like `--fields="name:primaryKey"`. +#### Foreign keys + +Since 2.0.8 the generator supports foreign keys using the `foreignKey` keyword. + +```php +yii migrate/create create_post --fields="author_id:integer:notNull:foreignKey(user),category_id:integer:defaultValue(1):foreignKey,title:string,body:text" +``` + +generates + +```php +/** + * Handles the creation for table `post`. + * Has foreign keys to the tables: + * + * - `user` + * - `category` + */ +class m160328_040430_create_post extends Migration +{ + /** + * @inheritdoc + */ + public function up() + { + $this->createTable('post', [ + 'id' => $this->primaryKey(), + 'author_id' => $this->integer()->notNull(), + 'category_id' => $this->integer()->defaultValue(1), + 'title' => $this->string(), + 'body' => $this->text(), + ]); + + // creates index for column `author_id` + $this->createIndex( + 'idx-post-author_id', + 'post', + 'author_id' + ); + + // add foreign key for table `user` + $this->addForeignKey( + 'fk-post-author_id', + 'post', + 'author_id', + 'user', + 'id', + 'CASCADE' + ); + + // creates index for column `category_id` + $this->createIndex( + 'idx-post-category_id', + 'post', + 'category_id' + ); + + // add foreign key for table `category` + $this->addForeignKey( + 'fk-post-category_id', + 'post', + 'category_id', + 'category', + 'id', + 'CASCADE' + ); + } + + /** + * @inheritdoc + */ + public function down() + { + // drops foreign key for table `user` + $this->dropForeignKey( + 'fk-post-author_id', + 'post' + ); + + // drops index for column `author_id` + $this->dropIndex( + 'idx-post-author_id', + 'post' + ); + + // drops foreign key for table `category` + $this->dropForeignKey( + 'fk-post-category_id', + 'post' + ); + + // drops index for column `category_id` + $this->dropIndex( + 'idx-post-category_id', + 'post' + ); + + $this->dropTable('post'); + } +} +``` + +The position of the `foreignKey` keyword in the column description doesn't +change the generated code. That means: + +- `author_id:integer:notNull:foreignKey(user)` +- `author_id:integer:foreignKey(user):notNull` +- `author_id:foreignKey(user):integer:notNull` + +All generate the same code. + +The `foreignKey` keyword can take a parameter between parenthesis which will be +the name of the related table for the generated foreign key. If no parameter +is passed then the table name will be deduced from the column name. + +In the +example above `author_id:integer:notNull:foreignKey(user)` will generate a +column named `author_id` with a foreign key to the `user` table while +`category_id:integer:default(1):foreignKey` will generate a column `category_id` +with a foreign key to the `category` table. ### Drop Table ```php yii migrate/create drop_post --fields="title:string(12):notNull:unique,body:text" -``` +``` -generates +generates ```php class m150811_220037_drop_post extends Migration @@ -359,31 +507,97 @@ If the migration name is in if the form of `create_junction_xxx_and_yyy` then co will be generated. ```php -yii migrate/create create_junction_post_and_tag +yii migrate/create create_junction_post_and_tag --fields="created_at:dateTime" ``` generates ```php -class m150811_220037_create_junction_post_and_tag extends Migration +/** + * Handles the creation for table `post_tag`. + * Has foreign keys to the tables: + * + * - `post` + * - `tag` + */ +class m160328_041642_create_junction_post_and_tag extends Migration { + /** + * @inheritdoc + */ public function up() { $this->createTable('post_tag', [ 'post_id' => $this->integer(), 'tag_id' => $this->integer(), - 'PRIMARY KEY(post_id, tag_id)' + 'created_at' => $this->dateTime(), + 'PRIMARY KEY(post_id, tag_id)', ]); - $this->createIndex('idx-post_tag-post_id', 'post_tag', 'post_id'); - $this->createIndex('idx-post_tag-tag_id', 'post_tag', 'tag_id'); - - $this->addForeignKey('fk-post_tag-post_id', 'post_tag', 'post_id', 'post', 'id', 'CASCADE'); - $this->addForeignKey('fk-post_tag-tag_id', 'post_tag', 'tag_id', 'tag', 'id', 'CASCADE'); + // creates index for column `post_id` + $this->createIndex( + 'idx-post_tag-post_id', + 'post_tag', + 'post_id' + ); + + // add foreign key for table `post` + $this->addForeignKey( + 'fk-post_tag-post_id', + 'post_tag', + 'post_id', + 'post', + 'id', + 'CASCADE' + ); + + // creates index for column `tag_id` + $this->createIndex( + 'idx-post_tag-tag_id', + 'post_tag', + 'tag_id' + ); + + // add foreign key for table `tag` + $this->addForeignKey( + 'fk-post_tag-tag_id', + 'post_tag', + 'tag_id', + 'tag', + 'id', + 'CASCADE' + ); } + /** + * @inheritdoc + */ public function down() { + // drops foreign key for table `post` + $this->dropForeignKey( + 'fk-post_tag-post_id', + 'post_tag' + ); + + // drops index for column `post_id` + $this->dropIndex( + 'idx-post_tag-post_id', + 'post_tag' + ); + + // drops foreign key for table `tag` + $this->dropForeignKey( + 'fk-post_tag-tag_id', + 'post_tag' + ); + + // drops index for column `tag_id` + $this->dropIndex( + 'idx-post_tag-tag_id', + 'post_tag' + ); + $this->dropTable('post_tag'); } } @@ -625,7 +839,7 @@ The migration command comes with a few command-line options that can be used to 'create_junction' => '@yii/views/createJunctionMigration.php' ]`), specifies template files for generating migration code. See "[Generating Migrations](#generating-migrations)" for more details. - + * `fields`: array of column definition strings used for creating migration code. Defaults to `[]`. The format of each definition is `COLUMN_NAME:COLUMN_TYPE:COLUMN_DECORATOR`. For example, `--fields=name:string(12):notNull` produces a string column of size 12 which is not null. diff --git a/framework/CHANGELOG.md b/framework/CHANGELOG.md index d5af6cc..5e89992 100644 --- a/framework/CHANGELOG.md +++ b/framework/CHANGELOG.md @@ -50,6 +50,7 @@ Yii Framework 2 Change Log - Enh #11187: migrate command now generates phpdoc for table migrations (Faryshta) - Enh #11254: Added ability to attach RBAC rule using class name (mdmunir) - Enh: Added `StringHelper::countWords()` that given a string returns number of words in it (samdark) +- Enh #11207: migrate command can create foreign keys. (faryshta) - Chg: HTMLPurifier dependency updated to `~4.6` (samdark) - Chg #10726: Added `yii\rbac\ManagerInterface::canAddChild()` (dkhlystov, samdark) - Chg #10921: Inverts responsibility of database specific column schema builder classes (df2) diff --git a/framework/console/controllers/BaseMigrateController.php b/framework/console/controllers/BaseMigrateController.php index 1b41624..8e64f67 100644 --- a/framework/console/controllers/BaseMigrateController.php +++ b/framework/console/controllers/BaseMigrateController.php @@ -62,6 +62,11 @@ abstract class BaseMigrateController extends Controller */ public $fields = []; + /** + * @var array columns which have a foreign key and their related table. + * @since 2.0.8 + */ + protected $foreignKeys = []; /** * @inheritdoc @@ -498,49 +503,60 @@ abstract class BaseMigrateController extends Controller $className = 'm' . gmdate('ymd_His') . '_' . $name; $file = $this->migrationPath . DIRECTORY_SEPARATOR . $className . '.php'; - if ($this->confirm("Create new migration '$file'?")) { + $table = null; if (preg_match('/^create_junction_(.+)_and_(.+)$/', $name, $matches)) { + $this->templateFile = $this->generatorTemplateFiles['create_junction']; $firstTable = mb_strtolower($matches[1], Yii::$app->charset); $secondTable = mb_strtolower($matches[2], Yii::$app->charset); - $content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['create_junction']), [ - 'className' => $className, - 'table' => $firstTable . '_' . $secondTable, - 'field_first' => $firstTable, - 'field_second' => $secondTable, - ]); + $this->fields = array_merge( + [ + [ + 'property' => $firstTable . '_id', + 'decorators' => 'integer()', + ], + [ + 'property' => $secondTable . '_id', + 'decorators' => 'integer()', + ], + ], + $this->fields, + [ + [ + 'property' => 'PRIMARY KEY(' . + $firstTable . '_id, ' . + $secondTable . '_id)', + ], + ] + ); + + $this->foreignKeys[$firstTable . '_id'] = $firstTable; + $this->foreignKeys[$secondTable . '_id'] = $secondTable; + $table = $firstTable . '_' . $secondTable; } elseif (preg_match('/^add_(.+)_to_(.+)$/', $name, $matches)) { - $content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['add_column']), [ - 'className' => $className, - 'table' => mb_strtolower($matches[2], Yii::$app->charset), - 'fields' => $this->fields - ]); + $this->templateFile = $this->generatorTemplateFiles['add_column']; + $table = mb_strtolower($matches[2], Yii::$app->charset); } elseif (preg_match('/^drop_(.+)_from_(.+)$/', $name, $matches)) { - $content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['drop_column']), [ - 'className' => $className, - 'table' => mb_strtolower($matches[2], Yii::$app->charset), - 'fields' => $this->fields - ]); + $this->templateFile = $this->generatorTemplateFiles['drop_column']; + $table = mb_strtolower($matches[2], Yii::$app->charset); } elseif (preg_match('/^create_(.+)$/', $name, $matches)) { $this->addDefaultPrimaryKey(); - $content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['create_table']), [ - 'className' => $className, - 'table' => mb_strtolower($matches[1], Yii::$app->charset), - 'fields' => $this->fields - ]); + $this->templateFile = $this->generatorTemplateFiles['create_table']; + $table = mb_strtolower($matches[1], Yii::$app->charset); } elseif (preg_match('/^drop_(.+)$/', $name, $matches)) { $this->addDefaultPrimaryKey(); - $content = $this->renderFile(Yii::getAlias($this->generatorTemplateFiles['drop_table']), [ - 'className' => $className, - 'table' => mb_strtolower($matches[1], Yii::$app->charset), - 'fields' => $this->fields - ]); - } else { - $content = $this->renderFile(Yii::getAlias($this->templateFile), ['className' => $className]); + $this->templateFile = $this->generatorTemplateFiles['drop_table']; + $table = mb_strtolower($matches[1], Yii::$app->charset); } - file_put_contents($file, $content); + file_put_contents($file, $this->renderFile($this->templateFile, [ + 'className' => $className, + 'name' => $name, + 'table' => $table, + 'fields' => $this->fields, + 'foreignKeys' => $this->foreignKeys, + ])); $this->stdout("New migration created successfully.\n", Console::FG_GREEN); } } @@ -708,12 +724,25 @@ abstract class BaseMigrateController extends Controller $chunks = preg_split('/\s?:\s?/', $field, null); $property = array_shift($chunks); - foreach ($chunks as &$chunk) { + foreach ($chunks as $i => &$chunk) { + if (strpos($chunk, 'foreignKey') === 0) { + preg_match('/foreignKey\((\w*)\)/', $chunk, $matches); + $this->foreignKeys[$property] = isset($matches[1]) + ? $matches[1] + : preg_replace('/_id$/', '', $property); + + unset($chunks[$i]); + continue; + } + if (!preg_match('/^(.+?)\(([^)]+)\)$/', $chunk)) { $chunk .= '()'; } } - $this->fields[$index] = ['property' => $property, 'decorators' => implode('->', $chunks)]; + $this->fields[$index] = [ + 'property' => $property, + 'decorators' => implode('->', $chunks), + ]; } } diff --git a/framework/console/controllers/MigrateController.php b/framework/console/controllers/MigrateController.php index 04eed64..a8a35f8 100644 --- a/framework/console/controllers/MigrateController.php +++ b/framework/console/controllers/MigrateController.php @@ -72,7 +72,7 @@ class MigrateController extends BaseMigrateController 'drop_table' => '@yii/views/dropTableMigration.php', 'add_column' => '@yii/views/addColumnMigration.php', 'drop_column' => '@yii/views/dropColumnMigration.php', - 'create_junction' => '@yii/views/createJunctionMigration.php' + 'create_junction' => '@yii/views/createTableMigration.php' ]; /** * @var Connection|array|string the DB connection object or the application component ID of the DB connection to use diff --git a/framework/views/_addColumns.php b/framework/views/_addColumns.php new file mode 100644 index 0000000..d929e52 --- /dev/null +++ b/framework/views/_addColumns.php @@ -0,0 +1,14 @@ + + $this->addColumn('', '', $this->); +render('_addForeignKeys', [ + 'table' => $table, + 'foreignKeys' => $foreignKeys, +]); diff --git a/framework/views/_addForeignKeys.php b/framework/views/_addForeignKeys.php new file mode 100644 index 0000000..0ec4912 --- /dev/null +++ b/framework/views/_addForeignKeys.php @@ -0,0 +1,19 @@ + $relatedTable): ?> + + // creates index for column `` + $this->createIndex( + '', + '', + '' + ); + + // add foreign key for table `` + $this->addForeignKey( + '', + '', + '', + '', + 'id', + 'CASCADE' + ); + $this->createTable('', [ + + '', + + \$this->{$field['decorators']}" ?>, + + ]); +render('_addForeignKeys', [ + 'table' => $table, + 'foreignKeys' => $foreignKeys, +]); diff --git a/framework/views/_dropColumns.php b/framework/views/_dropColumns.php new file mode 100644 index 0000000..109c57d --- /dev/null +++ b/framework/views/_dropColumns.php @@ -0,0 +1,10 @@ +render('_dropForeignKeys', [ + 'table' => $table, + 'foreignKeys' => $foreignKeys, +]); + +foreach ($fields as $field): ?> + $this->dropColumn('', ''); + $relatedTable): ?> + // drops foreign key for table `` + $this->dropForeignKey( + '', + '' + ); + + // drops index for column `` + $this->dropIndex( + '', + '' + ); + +render('_dropForeignKeys', [ + 'table' => $table, + 'foreignKeys' => $foreignKeys, +]) ?> + $this->dropTable(''); diff --git a/framework/views/_foreignTables.php b/framework/views/_foreignTables.php new file mode 100644 index 0000000..d6f0d0b --- /dev/null +++ b/framework/views/_foreignTables.php @@ -0,0 +1,14 @@ + + * Has foreign keys to the tables: + * + + * - `` + use yii\db\Migration; /** - * Handles adding the columns - * for table ``. + * Handles adding to table ``. +render('_foreignTables', [ + 'foreignKeys' => $foreignKeys + ]) ?> */ class extends Migration { @@ -30,9 +28,12 @@ class extends Migration */ public function up() { - - $this->addColumn(" . $field['decorators'] ?>); - +render('_addColumns', [ + 'table' => $table, + 'fields' => $fields, + 'foreignKeys' => $foreignKeys, +]) +?> } /** @@ -40,8 +41,11 @@ class extends Migration */ public function down() { - - $this->dropColumn(); - +render('_dropColumns', [ + 'table' => $table, + 'fields' => $fields, + 'foreignKeys' => $foreignKeys, +]) +?> } } diff --git a/framework/views/createJunctionMigration.php b/framework/views/createJunctionMigration.php index 1b5fc8f..0aaf31e 100644 --- a/framework/views/createJunctionMigration.php +++ b/framework/views/createJunctionMigration.php @@ -2,6 +2,8 @@ /** * This view is used by console/controllers/MigrateController.php * The following variables are available in this view: + * @since 2.0.7 + * @deprecated since 2.0.8 */ /* @var $className string the new migration class name */ /* @var $table string the name table */ diff --git a/framework/views/createTableMigration.php b/framework/views/createTableMigration.php index b40ac23..995b177 100644 --- a/framework/views/createTableMigration.php +++ b/framework/views/createTableMigration.php @@ -6,6 +6,7 @@ /* @var $className string the new migration class name */ /* @var $table string the name table */ /* @var $fields array the fields */ +/* @var $foreignKeys array the foreign keys */ echo " @@ -14,6 +15,9 @@ use yii\db\Migration; /** * Handles the creation for table ``. +render('_foreignTables', [ + 'foreignKeys' => $foreignKeys +]) ?> */ class extends Migration { @@ -22,11 +26,12 @@ class extends Migration */ public function up() { - $this->createTable('', [ - - \$this->{$field['decorators']},\n" ?> - - ]); +render('_createTable', [ + 'table' => $table, + 'fields' => $fields, + 'foreignKeys' => $foreignKeys, +]) +?> } /** @@ -34,6 +39,10 @@ class extends Migration */ public function down() { - $this->dropTable(''); +render('_dropTable', [ + 'table' => $table, + 'foreignKeys' => $foreignKeys, +]) +?> } } diff --git a/framework/views/dropColumnMigration.php b/framework/views/dropColumnMigration.php index 5cc6e21..47ff81f 100644 --- a/framework/views/dropColumnMigration.php +++ b/framework/views/dropColumnMigration.php @@ -6,6 +6,8 @@ /* @var $className string the new migration class name */ /* @var $table string the name table */ /* @var $fields array the fields */ +preg_match('/^drop_(.+)_from_(.+)$/', $name, $matches); +$columns = $matches[1]; echo " @@ -13,15 +15,10 @@ echo " - * for table ``. + * Handles dropping from table ``. +render('_foreignTables', [ + 'foreignKeys' => $foreignKeys + ]) ?> */ class extends Migration { @@ -30,9 +27,12 @@ class extends Migration */ public function up() { - - $this->dropColumn(); - +render('_dropColumns', [ + 'table' => $table, + 'fields' => $fields, + 'foreignKeys' => $foreignKeys, +]) +?> } /** @@ -40,8 +40,11 @@ class extends Migration */ public function down() { - - $this->addColumn(" . $field['decorators'] ?>); - +render('_addColumns', [ + 'table' => $table, + 'fields' => $fields, + 'foreignKeys' => $foreignKeys, +]) +?> } } diff --git a/framework/views/dropTableMigration.php b/framework/views/dropTableMigration.php index 0b8a47d..c91de0c 100644 --- a/framework/views/dropTableMigration.php +++ b/framework/views/dropTableMigration.php @@ -14,6 +14,9 @@ use yii\db\Migration; /** * Handles the dropping for table ``. +render('_foreignTables', [ + 'foreignKeys' => $foreignKeys +]) ?> */ class extends Migration { @@ -22,7 +25,11 @@ class extends Migration */ public function up() { - $this->dropTable(''); +render('_dropTable', [ + 'table' => $table, + 'foreignKeys' => $foreignKeys, +]) +?> } /** @@ -30,10 +37,11 @@ class extends Migration */ public function down() { - $this->createTable('', [ - - \$this->{$field['decorators']},\n" ?> - - ]); +render('_createTable', [ + 'table' => $table, + 'fields' => $fields, + 'foreignKeys' => $foreignKeys, +]) +?> } } diff --git a/tests/framework/console/controllers/MigrateControllerTestTrait.php b/tests/framework/console/controllers/MigrateControllerTestTrait.php index ace69f0..1e51bcd 100644 --- a/tests/framework/console/controllers/MigrateControllerTestTrait.php +++ b/tests/framework/console/controllers/MigrateControllerTestTrait.php @@ -371,6 +371,143 @@ class {$class} extends Migration CODE; $this->assertEqualsWithoutLE($code, $file); + + $class = 'm' . gmdate('ymd_His') . '_' . $migrationName; + $this->runMigrateControllerAction('create', [ + $migrationName, + 'fields' => 'user_id:integer:foreignKey, + product_id:foreignKey:integer:unsigned:notNull, + order_id:integer:foreignKey(user_order):notNull, + created_at:dateTime:notNull', + ]); + $file = $this->parseNameClassMigration($class); + $code = <<createTable('test', [ + 'id' => \$this->primaryKey(), + 'user_id' => \$this->integer(), + 'product_id' => \$this->integer()->unsigned()->notNull(), + 'order_id' => \$this->integer()->notNull(), + 'created_at' => \$this->dateTime()->notNull(), + ]); + + // creates index for column `user_id` + \$this->createIndex( + 'idx-test-user_id', + 'test', + 'user_id' + ); + + // add foreign key for table `user` + \$this->addForeignKey( + 'fk-test-user_id', + 'test', + 'user_id', + 'user', + 'id', + 'CASCADE' + ); + + // creates index for column `product_id` + \$this->createIndex( + 'idx-test-product_id', + 'test', + 'product_id' + ); + + // add foreign key for table `product` + \$this->addForeignKey( + 'fk-test-product_id', + 'test', + 'product_id', + 'product', + 'id', + 'CASCADE' + ); + + // creates index for column `order_id` + \$this->createIndex( + 'idx-test-order_id', + 'test', + 'order_id' + ); + + // add foreign key for table `user_order` + \$this->addForeignKey( + 'fk-test-order_id', + 'test', + 'order_id', + 'user_order', + 'id', + 'CASCADE' + ); + } + + /** + * @inheritdoc + */ + public function down() + { + // drops foreign key for table `user` + \$this->dropForeignKey( + 'fk-test-user_id', + 'test' + ); + + // drops index for column `user_id` + \$this->dropIndex( + 'idx-test-user_id', + 'test' + ); + + // drops foreign key for table `product` + \$this->dropForeignKey( + 'fk-test-product_id', + 'test' + ); + + // drops index for column `product_id` + \$this->dropIndex( + 'idx-test-product_id', + 'test' + ); + + // drops foreign key for table `user_order` + \$this->dropForeignKey( + 'fk-test-order_id', + 'test' + ); + + // drops index for column `order_id` + \$this->dropIndex( + 'idx-test-order_id', + 'test' + ); + + \$this->dropTable('test'); + } +} + +CODE; + $this->assertEqualsWithoutLE($code, $file); } public function testGenerateDropMigration() @@ -471,8 +608,7 @@ CODE; use yii\db\Migration; /** - * Handles adding the columns `title`, `body`, `price`, `created_at` - * for table `test`. + * Handles adding columns to table `test`. */ class {$class} extends Migration { @@ -519,8 +655,7 @@ CODE; use yii\db\Migration; /** - * Handles dropping columns `title`, `body`, `price`, `created_at` - * for table `test`. + * Handles dropping columns from table `test`. */ class {$class} extends Migration { @@ -566,8 +701,11 @@ CODE; use yii\db\Migration; /** - * Handles the creation of table `post_tag` which is a junction between - * table `post` and table `tag`. + * Handles the creation for table `post_tag`. + * Has foreign keys to the tables: + * + * - `post` + * - `tag` */ class {$class} extends Migration { @@ -582,18 +720,14 @@ class {$class} extends Migration 'PRIMARY KEY(post_id, tag_id)', ]); + // creates index for column `post_id` \$this->createIndex( 'idx-post_tag-post_id', 'post_tag', 'post_id' ); - \$this->createIndex( - 'idx-post_tag-tag_id', - 'post_tag', - 'tag_id' - ); - + // add foreign key for table `post` \$this->addForeignKey( 'fk-post_tag-post_id', 'post_tag', @@ -603,6 +737,14 @@ class {$class} extends Migration 'CASCADE' ); + // creates index for column `tag_id` + \$this->createIndex( + 'idx-post_tag-tag_id', + 'post_tag', + 'tag_id' + ); + + // add foreign key for table `tag` \$this->addForeignKey( 'fk-post_tag-tag_id', 'post_tag', @@ -618,6 +760,30 @@ class {$class} extends Migration */ public function down() { + // drops foreign key for table `post` + \$this->dropForeignKey( + 'fk-post_tag-post_id', + 'post_tag' + ); + + // drops index for column `post_id` + \$this->dropIndex( + 'idx-post_tag-post_id', + 'post_tag' + ); + + // drops foreign key for table `tag` + \$this->dropForeignKey( + 'fk-post_tag-tag_id', + 'post_tag' + ); + + // drops index for column `tag_id` + \$this->dropIndex( + 'idx-post_tag-tag_id', + 'post_tag' + ); + \$this->dropTable('post_tag'); } }