-
-
Save yurkinx/2f94ed7540a924c2cafde3ec3451215e to your computer and use it in GitHub Desktop.
Revisions
-
xskif revised this gist
Sep 11, 2014 . No changes.There are no files selected for viewing
-
xskif revised this gist
Sep 11, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 "Schema::TYPE_PK"; } $type = 'Schema::TYPE_'.strtoupper($col->type).' . \''; $typeSize = $col->size ? "({$col->size})" : ""; -
xskif revised this gist
Sep 11, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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'"; } $type = 'Schema::TYPE_'.strtoupper($col->type).' . \''; $typeSize = $col->size ? "({$col->size})" : ""; -
xskif revised this gist
Sep 11, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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}"; } elseif ($col->allowNull) { $result .= ' DEFAULT NULL'; } -
xskif revised this gist
Sep 11, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,7 +11,7 @@ public function actionMigrate() { $addForeignKeys = ''; $dropForeignKeys = ''; $result = "<?php\npublic function safeUp()\n{\n"; foreach ($tables as $table) { $compositePrimaryKeyCols = array(); -
xskif revised this gist
Sep 11, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -11,7 +11,7 @@ public function actionMigrate() { $addForeignKeys = ''; $dropForeignKeys = ''; $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 safeDown()\n{\n"; $result .= $dropForeignKeys; // This needs to come before the tables are dropped. foreach ($tables as $table) { $result .= ' $this->dropTable(\'' . $table->name . '\');' . "\n"; -
xskif revised this gist
Sep 11, 2014 . 1 changed file with 0 additions and 81 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,81 +0,0 @@ -
xskif revised this gist
Sep 11, 2014 . 1 changed file with 83 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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.'\''; } } -
xskif revised this gist
Sep 11, 2014 . 1 changed file with 1 addition and 2 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -78,5 +78,4 @@ class ReverseController extends \yii\console\Controller } return $result; } } -
xskif revised this gist
Sep 11, 2014 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -78,4 +78,5 @@ class ReverseController extends \yii\console\Controller } return $result; } } ?> -
xskif renamed this gist
Sep 11, 2014 . 1 changed file with 13 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,10 +1,12 @@ <?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 = ''; @@ -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) $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 @@ -55,6 +59,8 @@ public function run($args) { echo $result; return self::EXIT_CODE_NORMAL; } public function getColType($col) { -
bmarston created this gist
May 8, 2013 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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; } }