Skip to content

Instantly share code, notes, and snippets.

@yurkinx
Forked from xskif/ReverseController.php
Created November 9, 2016 21:40
Show Gist options
  • Save yurkinx/2f94ed7540a924c2cafde3ec3451215e to your computer and use it in GitHub Desktop.
Save yurkinx/2f94ed7540a924c2cafde3ec3451215e to your computer and use it in GitHub Desktop.

Revisions

  1. xskif revised this gist Sep 11, 2014. No changes.
  2. xskif revised this gist Sep 11, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ReverseController.php
    Original file line number Diff line number Diff line change
    @@ -65,7 +65,7 @@ public function actionMigrate() {

    public function getColType($col) {
    if ($col->isPrimaryKey && $col->autoIncrement) {
    return "'pk'";
    return "Schema::TYPE_PK";
    }
    $type = 'Schema::TYPE_'.strtoupper($col->type).' . \'';
    $typeSize = $col->size ? "({$col->size})" : "";
  3. xskif revised this gist Sep 11, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ReverseController.php
    Original file line number Diff line number Diff line change
    @@ -65,7 +65,7 @@ public function actionMigrate() {

    public function getColType($col) {
    if ($col->isPrimaryKey && $col->autoIncrement) {
    return "pk";
    return "'pk'";
    }
    $type = 'Schema::TYPE_'.strtoupper($col->type).' . \'';
    $typeSize = $col->size ? "({$col->size})" : "";
  4. xskif revised this gist Sep 11, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ReverseController.php
    Original file line number Diff line number Diff line change
    @@ -74,7 +74,7 @@ public function getColType($col) {
    $result .= ' NOT NULL';
    }
    if ($col->defaultValue != null) {
    $result .= " DEFAULT '{$col->defaultValue}'";
    $result .= " DEFAULT {$col->defaultValue}";
    } elseif ($col->allowNull) {
    $result .= ' DEFAULT NULL';
    }
  5. xskif revised this gist Sep 11, 2014. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion ReverseController.php
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ public function actionMigrate() {
    $addForeignKeys = '';
    $dropForeignKeys = '';

    $result = "public function safeUp()\n{\n";
    $result = "<?php\npublic function safeUp()\n{\n";
    foreach ($tables as $table) {
    $compositePrimaryKeyCols = array();

  6. xskif revised this gist Sep 11, 2014. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions ReverseController.php
    Original file line number Diff line number Diff line change
    @@ -11,7 +11,7 @@ public function actionMigrate() {
    $addForeignKeys = '';
    $dropForeignKeys = '';

    $result = "public function up()\n{\n";
    $result = "public function safeUp()\n{\n";
    foreach ($tables as $table) {
    $compositePrimaryKeyCols = array();

    @@ -50,7 +50,7 @@ public function actionMigrate() {
    $result .= $addForeignKeys; // This needs to come after all of the tables have been created.
    $result .= "}\n\n\n";

    $result .= "public function down()\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";
  7. xskif revised this gist Sep 11, 2014. 1 changed file with 0 additions and 81 deletions.
    81 changes: 0 additions & 81 deletions ReverseController
    Original file line number Diff line number Diff line change
    @@ -1,81 +0,0 @@
    <?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 = "public function up()\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 down()\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 "pk";
    }
    $result = $col->dbType;
    if (!$col->allowNull) {
    $result .= ' NOT NULL';
    }
    if ($col->defaultValue != null) {
    $result .= " DEFAULT '{$col->defaultValue}'";
    } elseif ($col->allowNull) {
    $result .= ' DEFAULT NULL';
    }
    return $result;
    }
    }
  8. xskif revised this gist Sep 11, 2014. 1 changed file with 83 additions and 0 deletions.
    83 changes: 83 additions & 0 deletions ReverseController.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,83 @@
    <?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 = "public function up()\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 down()\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 "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.'\'';
    }
    }
  9. xskif revised this gist Sep 11, 2014. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions ReverseController
    Original file line number Diff line number Diff line change
    @@ -78,5 +78,4 @@ class ReverseController extends \yii\console\Controller
    }
    return $result;
    }
    }
    ?>
    }
  10. xskif revised this gist Sep 11, 2014. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion ReverseController
    Original file line number Diff line number Diff line change
    @@ -78,4 +78,5 @@ class ReverseController extends \yii\console\Controller
    }
    return $result;
    }
    }
    }
    ?>
  11. xskif renamed this gist Sep 11, 2014. 1 changed file with 13 additions and 7 deletions.
    20 changes: 13 additions & 7 deletions InitialDbMigrationCommand.php → ReverseController
    Original file line number Diff line number Diff line change
    @@ -1,10 +1,12 @@
    <?php
    class InitialDbMigrationCommand extends CConsoleCommand
    namespace console\controllers;
    use \Yii;
    class ReverseController extends \yii\console\Controller
    {

    public function run($args) {
    $schema = $args[0];
    $tables = Yii::app()->db->schema->getTables($schema);
    public function actionMigrate() {
    // $schema = $args[0];
    $tables = Yii::$app->db->schema->getTableSchemas();

    $addForeignKeys = '';
    $dropForeignKeys = '';
    @@ -28,12 +30,14 @@ public function run($args) {
    // Add foreign key(s) and create indexes
    foreach ($table->foreignKeys as $col => $fk) {
    // Foreign key naming convention: fk_table_foreignTable_col (max 64 characters)
    $fkName = substr('fk_' . $table->name . '_' . $fk[0] . '_' . $col, 0 , 64);
    $addForeignKeys .= ' $this->addForeignKey(' . "'$fkName', '$table->name', '$col', '$fk[0]', '$fk[1]', 'NO ACTION', 'NO ACTION');\n\n";
    $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_' . $col . "', '$table->name', '$col', FALSE);\n\n";
    $result .= ' $this->createIndex(\'idx_' . $column . "', '$table->name', '$column', FALSE);\n\n";
    }

    // Add composite primary key for join tables
    @@ -55,6 +59,8 @@ public function run($args) {


    echo $result;

    return self::EXIT_CODE_NORMAL;
    }

    public function getColType($col) {
  12. @bmarston bmarston created this gist May 8, 2013.
    75 changes: 75 additions & 0 deletions InitialDbMigrationCommand.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,75 @@
    <?php
    class InitialDbMigrationCommand extends CConsoleCommand
    {

    public function run($args) {
    $schema = $args[0];
    $tables = Yii::app()->db->schema->getTables($schema);

    $addForeignKeys = '';
    $dropForeignKeys = '';

    $result = "public function up()\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)
    $fkName = substr('fk_' . $table->name . '_' . $fk[0] . '_' . $col, 0 , 64);
    $addForeignKeys .= ' $this->addForeignKey(' . "'$fkName', '$table->name', '$col', '$fk[0]', '$fk[1]', 'NO ACTION', 'NO ACTION');\n\n";
    $dropForeignKeys .= ' $this->dropForeignKey(' . "'$fkName', '$table->name');\n\n";

    // Index naming convention: idx_col
    $result .= ' $this->createIndex(\'idx_' . $col . "', '$table->name', '$col', 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 down()\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;
    }

    public function getColType($col) {
    if ($col->isPrimaryKey && $col->autoIncrement) {
    return "pk";
    }
    $result = $col->dbType;
    if (!$col->allowNull) {
    $result .= ' NOT NULL';
    }
    if ($col->defaultValue != null) {
    $result .= " DEFAULT '{$col->defaultValue}'";
    } elseif ($col->allowNull) {
    $result .= ' DEFAULT NULL';
    }
    return $result;
    }
    }