Skip to content

Instantly share code, notes, and snippets.

@xskif
Forked from bmarston/InitialDbMigrationCommand.php
Last active July 30, 2021 04:26
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save xskif/15e986ef55a0f54778f3 to your computer and use it in GitHub Desktop.
Save xskif/15e986ef55a0f54778f3 to your computer and use it in GitHub Desktop.
1) Copy this gist to project/console/controllers. 2) call -- yii reverse/migrate > tmp.migrate.php 3) copy functions to existing migration class.
<?php
namespace console\controllers;
use \Yii;
class ReverseController extends \yii\console\Controller
{
public function actionMigrate() {
// $schema = $args[0];
$tables = Yii::$app->db->schema->getTableSchemas();
$addForeignKeys = '';
$dropForeignKeys = '';
$result = "<?php\npublic function safeUp()\n{\n";
foreach ($tables as $table) {
$compositePrimaryKeyCols = array();
// Create table
$result .= ' $this->createTable(\'' . $table->name . '\', array(' . "\n";
foreach ($table->columns as $col) {
$result .= ' \'' . $col->name . '\' => ' . $this->getColType($col) . ',' . "\n";
if ($col->isPrimaryKey && !$col->autoIncrement) {
// Add column to composite primary key array
$compositePrimaryKeyCols[] = $col->name;
}
}
$result .= ' ), \'\');' . "\n\n";
// Add foreign key(s) and create indexes
foreach ($table->foreignKeys as $col => $fk) {
// Foreign key naming convention: fk_table_foreignTable_col (max 64 characters)
$refColumn = end($fk);
$column = key($fk);
$fkName = substr('fk_' . $table->name . '_' . $fk[0] . '_' . $column, 0 , 64);
$addForeignKeys .= ' $this->addForeignKey(' . "'$fkName', '$table->name', '$column', '$fk[0]', '$refColumn', 'NO ACTION', 'NO ACTION');\n\n";
$dropForeignKeys .= ' $this->dropForeignKey(' . "'$fkName', '$table->name');\n\n";
// Index naming convention: idx_col
$result .= ' $this->createIndex(\'idx_' . $column . "', '$table->name', '$column', FALSE);\n\n";
}
// Add composite primary key for join tables
if ($compositePrimaryKeyCols) {
$result .= ' $this->addPrimaryKey(\'pk_' . $table->name . "', '$table->name', '" . implode(',', $compositePrimaryKeyCols) . "');\n\n";
}
}
$result .= $addForeignKeys; // This needs to come after all of the tables have been created.
$result .= "}\n\n\n";
$result .= "public function safeDown()\n{\n";
$result .= $dropForeignKeys; // This needs to come before the tables are dropped.
foreach ($tables as $table) {
$result .= ' $this->dropTable(\'' . $table->name . '\');' . "\n";
}
$result .= "}\n";
echo $result;
return self::EXIT_CODE_NORMAL;
}
public function getColType($col) {
if ($col->isPrimaryKey && $col->autoIncrement) {
return "Schema::TYPE_PK";
}
$type = 'Schema::TYPE_'.strtoupper($col->type).' . \'';
$typeSize = $col->size ? "({$col->size})" : "";
$result = $type.$typeSize;
if (!$col->allowNull) {
$result .= ' NOT NULL';
}
if ($col->defaultValue != null) {
$result .= " DEFAULT {$col->defaultValue}";
} elseif ($col->allowNull) {
$result .= ' DEFAULT NULL';
}
return $result.'\'';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment