From e73d904c891afc43a4a5e0be12a71b5c902e0ab1 Mon Sep 17 00:00:00 2001 From: Sarunas Date: Thu, 11 Jul 2013 15:19:38 +0100 Subject: [PATCH 1/6] Change GitIgnore --- .gitignore | 219 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 219 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f26703f --- /dev/null +++ b/.gitignore @@ -0,0 +1,219 @@ +################# +## Eclipse +################# + +*.pydevproject +.project +.metadata +bin/ +tmp/ +*.tmp +*.bak +*.swp +*~.nib +local.properties +.classpath +.settings/ +.loadpath + +# External tool builders +.externalToolBuilders/ + +# Locally stored "Eclipse launch configurations" +*.launch + +# CDT-specific +.cproject + +# PDT-specific +.buildpath + +################# +# PHPStorm +################# +.idea + +################# +## Visual Studio +################# + +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. + +# User-specific files +*.suo +*.user +*.sln.docstates + +# Build results + +[Dd]ebug/ +[Rr]elease/ +x64/ +build/ +[Bb]in/ +[Oo]bj/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +*_i.c +*_p.c +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.log +*.scc + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opensdf +*.sdf +*.cachefile + +# Visual Studio profiler +*.psess +*.vsp +*.vspx + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# NCrunch +*.ncrunch* +.*crunch*.local.xml + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.Publish.xml +*.pubxml + +# NuGet Packages Directory +## TODO: If you have NuGet Package Restore enabled, uncomment the next line +#packages/ + +# Windows Azure Build Output +csx +*.build.csdef + +# Windows Store app package directory +AppPackages/ + +# Others +sql/ +*.Cache +ClientBin/ +[Ss]tyle[Cc]op.* +~$* +*~ +*.dbmdl +*.[Pp]ublish.xml +*.pfx +*.publishsettings + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file to a newer +# Visual Studio version. Backup files are not needed, because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +App_Data/*.mdf +App_Data/*.ldf + +############# +## Windows detritus +############# + +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Mac crap +.DS_Store + + +############# +## Python +############# + +*.py[co] + +# Packages +*.egg +*.egg-info +dist/ +build/ +eggs/ +parts/ +var/ +sdist/ +develop-eggs/ +.installed.cfg + +# Installer logs +pip-log.txt + +# Unit test / coverage reports +.coverage +.tox + +#Translations +*.mo + +#Mr Developer +.mr.developer.cfg From b0afa9b0429db82d89e0a8b163a62057a232ad1e Mon Sep 17 00:00:00 2001 From: Sarunas Date: Thu, 11 Jul 2013 15:37:42 +0100 Subject: [PATCH 2/6] Add Index generation, change primary key gen Add Index generation, change primary key generation. With MySQL it works perfectly --- EDatabaseCommand.php | 65 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/EDatabaseCommand.php b/EDatabaseCommand.php index 7469193..500cb11 100644 --- a/EDatabaseCommand.php +++ b/EDatabaseCommand.php @@ -64,6 +64,11 @@ class EDatabaseCommand extends CConsoleCommand */ public $ignoreMigrationTable = true; + /** + * @var bool whether to ignore the SQLite if statements + */ + public $ignoreSQLiteChecks = true; + /** * @var bool whether to display the Foreign Keys warning */ @@ -124,7 +129,7 @@ public function actionDump($args) $filename = $this->migrationPath . DIRECTORY_SEPARATOR . $migrationClassName . ".php"; $prefixes = explode(",", $this->prefix); - $codeTruncate = $codeSchema = $codeForeignKeys = $codeInserts = ''; + $codeTruncate = $codeSchema = $codeForeignKeys = $codeIndexes = $codeInserts = ''; echo "Querying tables "; @@ -155,6 +160,7 @@ public function actionDump($args) if ($this->createSchema == true) { $codeSchema .= $this->generateSchema($table, $schema); $codeForeignKeys .= $this->generateForeignKeys($table, $schema); + $codeIndexes .= $this->generateIndexes($table, $schema); } if ($this->insertData == true) { @@ -162,7 +168,7 @@ public function actionDump($args) } } - $code .= $codeTruncate."\n".$codeSchema."\n".$codeForeignKeys."\n".$codeForeignKeys."\n".$codeInserts; + $code .= $codeTruncate."\n".$codeSchema."\n".$codeForeignKeys."\n".$codeIndexes."\n".$codeInserts; if ($this->foreignKeyChecks == false) { $code .= $this->indent(2) . "if (Yii::app()->db->schema instanceof CMysqlSchema)\n"; @@ -216,11 +222,14 @@ private function generateSchema($table, $schema) private function generatePrimaryKeys($columns) { + $keys = array(); foreach ($columns as $col) { - if ($col->isPrimaryKey && !$col->autoIncrement) { - return $this->indent(3) . '"PRIMARY KEY (' . $col->name . ')"' . "\n"; + if ($col->isPrimaryKey) { + $keys[] = "`$col->name`"; } } + + return $this->indent(3) . '"PRIMARY KEY (' . implode(',', $keys) . ')"' . "\n"; } private function generateForeignKeys($table, $schema) @@ -229,15 +238,56 @@ private function generateForeignKeys($table, $schema) return ""; } $code = "\n\n\n" . $this->indent(2) . "// Foreign Keys for table '" . $table->name . "'\n"; - $code .= $this->indent(2) . "if ((Yii::app()->db->schema instanceof CSqliteSchema) == false):\n"; + + if(!$this->ignoreSQLiteChecks){ + $code .= $this->indent(2) . "if ((Yii::app()->db->schema instanceof CSqliteSchema) == false):\n"; + } + foreach ($table->foreignKeys as $name => $foreignKey) { $code .= $this->indent(3) . "\$this->addForeignKey('fk_{$table->name}_{$foreignKey[0]}_{$name}', '{$table->name}', '{$name}', '{$foreignKey[0]}', '{$foreignKey[1]}', null, null); // FIX RELATIONS \n"; } - $code .= $this->indent(2) . "endif;\n"; + + if(!$this->ignoreSQLiteChecks){ + $code .= $this->indent(2) . "endif;\n"; + } $this->_displayFkWarning = TRUE; return $code; } + private function generateIndexes($table, $schema) + { + $indexes = Yii::app()->db->createCommand( + "SHOW INDEX FROM " . $table->name . " + WHERE Key_name != 'PRIMARY'" + )->queryAll(); + + if (count($indexes) == 0) { + return ""; + } + + $code = "\n" . $this->indent(2) . "// Indexes for table '" . $table->name . "'\n"; + if(!$this->ignoreSQLiteChecks){ + $code .= $this->indent(2) . "if ((Yii::app()->db->schema instanceof CSqliteSchema) == false):\n"; + } + + $checker = false; + + foreach ($indexes as $index) { + if (!isset($table->foreignKeys[$index['Column_name']])) { + $unique = !$index['Non_unique'] ? 'True' : 'False'; + $code .= $this->indent(3) . "\$this->createIndex('index_{$index['Table']}_{$index['Column_name']}', '{$index['Table']}', '{$index['Column_name']}', {$unique}); \n"; + $checker = true; + } + + } + + if(!$this->ignoreSQLiteChecks){ + $code .= $this->indent(2) . "endif;\n"; + } + + //remove everything if there were no results + return $checker ? $code : ''; + } private function generateInserts($table, $schema) { $data = Yii::app()->{$this->dbConnection}->createCommand() @@ -273,9 +323,6 @@ private function resolveColumnType($col) if ($col->defaultValue != null) { $result .= " DEFAULT '{$col->defaultValue}'"; } - if ($col->isPrimaryKey) { - $result .= " PRIMARY KEY"; - } if ($col->autoIncrement) { $result .= " AUTO_INCREMENT"; } From 0077f137f293a5ee90f3be1ce20798e16a38c6b2 Mon Sep 17 00:00:00 2001 From: Sarunas Date: Thu, 11 Jul 2013 15:58:12 +0100 Subject: [PATCH 3/6] Revert "Add Index generation, change primary key gen" This reverts commit b0afa9b0429db82d89e0a8b163a62057a232ad1e. --- EDatabaseCommand.php | 65 ++++++-------------------------------------- 1 file changed, 9 insertions(+), 56 deletions(-) diff --git a/EDatabaseCommand.php b/EDatabaseCommand.php index 500cb11..7469193 100644 --- a/EDatabaseCommand.php +++ b/EDatabaseCommand.php @@ -64,11 +64,6 @@ class EDatabaseCommand extends CConsoleCommand */ public $ignoreMigrationTable = true; - /** - * @var bool whether to ignore the SQLite if statements - */ - public $ignoreSQLiteChecks = true; - /** * @var bool whether to display the Foreign Keys warning */ @@ -129,7 +124,7 @@ public function actionDump($args) $filename = $this->migrationPath . DIRECTORY_SEPARATOR . $migrationClassName . ".php"; $prefixes = explode(",", $this->prefix); - $codeTruncate = $codeSchema = $codeForeignKeys = $codeIndexes = $codeInserts = ''; + $codeTruncate = $codeSchema = $codeForeignKeys = $codeInserts = ''; echo "Querying tables "; @@ -160,7 +155,6 @@ public function actionDump($args) if ($this->createSchema == true) { $codeSchema .= $this->generateSchema($table, $schema); $codeForeignKeys .= $this->generateForeignKeys($table, $schema); - $codeIndexes .= $this->generateIndexes($table, $schema); } if ($this->insertData == true) { @@ -168,7 +162,7 @@ public function actionDump($args) } } - $code .= $codeTruncate."\n".$codeSchema."\n".$codeForeignKeys."\n".$codeIndexes."\n".$codeInserts; + $code .= $codeTruncate."\n".$codeSchema."\n".$codeForeignKeys."\n".$codeForeignKeys."\n".$codeInserts; if ($this->foreignKeyChecks == false) { $code .= $this->indent(2) . "if (Yii::app()->db->schema instanceof CMysqlSchema)\n"; @@ -222,14 +216,11 @@ private function generateSchema($table, $schema) private function generatePrimaryKeys($columns) { - $keys = array(); foreach ($columns as $col) { - if ($col->isPrimaryKey) { - $keys[] = "`$col->name`"; + if ($col->isPrimaryKey && !$col->autoIncrement) { + return $this->indent(3) . '"PRIMARY KEY (' . $col->name . ')"' . "\n"; } } - - return $this->indent(3) . '"PRIMARY KEY (' . implode(',', $keys) . ')"' . "\n"; } private function generateForeignKeys($table, $schema) @@ -238,56 +229,15 @@ private function generateForeignKeys($table, $schema) return ""; } $code = "\n\n\n" . $this->indent(2) . "// Foreign Keys for table '" . $table->name . "'\n"; - - if(!$this->ignoreSQLiteChecks){ - $code .= $this->indent(2) . "if ((Yii::app()->db->schema instanceof CSqliteSchema) == false):\n"; - } - + $code .= $this->indent(2) . "if ((Yii::app()->db->schema instanceof CSqliteSchema) == false):\n"; foreach ($table->foreignKeys as $name => $foreignKey) { $code .= $this->indent(3) . "\$this->addForeignKey('fk_{$table->name}_{$foreignKey[0]}_{$name}', '{$table->name}', '{$name}', '{$foreignKey[0]}', '{$foreignKey[1]}', null, null); // FIX RELATIONS \n"; } - - if(!$this->ignoreSQLiteChecks){ - $code .= $this->indent(2) . "endif;\n"; - } + $code .= $this->indent(2) . "endif;\n"; $this->_displayFkWarning = TRUE; return $code; } - private function generateIndexes($table, $schema) - { - $indexes = Yii::app()->db->createCommand( - "SHOW INDEX FROM " . $table->name . " - WHERE Key_name != 'PRIMARY'" - )->queryAll(); - - if (count($indexes) == 0) { - return ""; - } - - $code = "\n" . $this->indent(2) . "// Indexes for table '" . $table->name . "'\n"; - if(!$this->ignoreSQLiteChecks){ - $code .= $this->indent(2) . "if ((Yii::app()->db->schema instanceof CSqliteSchema) == false):\n"; - } - - $checker = false; - - foreach ($indexes as $index) { - if (!isset($table->foreignKeys[$index['Column_name']])) { - $unique = !$index['Non_unique'] ? 'True' : 'False'; - $code .= $this->indent(3) . "\$this->createIndex('index_{$index['Table']}_{$index['Column_name']}', '{$index['Table']}', '{$index['Column_name']}', {$unique}); \n"; - $checker = true; - } - - } - - if(!$this->ignoreSQLiteChecks){ - $code .= $this->indent(2) . "endif;\n"; - } - - //remove everything if there were no results - return $checker ? $code : ''; - } private function generateInserts($table, $schema) { $data = Yii::app()->{$this->dbConnection}->createCommand() @@ -323,6 +273,9 @@ private function resolveColumnType($col) if ($col->defaultValue != null) { $result .= " DEFAULT '{$col->defaultValue}'"; } + if ($col->isPrimaryKey) { + $result .= " PRIMARY KEY"; + } if ($col->autoIncrement) { $result .= " AUTO_INCREMENT"; } From 8dd89708c5bae99bc2e682be149775a5b5b4cdb7 Mon Sep 17 00:00:00 2001 From: Sarunas Date: Thu, 11 Jul 2013 16:07:19 +0100 Subject: [PATCH 4/6] Revert --- .gitignore | 219 ----------------------------------------------------- 1 file changed, 219 deletions(-) delete mode 100644 .gitignore diff --git a/.gitignore b/.gitignore deleted file mode 100644 index f26703f..0000000 --- a/.gitignore +++ /dev/null @@ -1,219 +0,0 @@ -################# -## Eclipse -################# - -*.pydevproject -.project -.metadata -bin/ -tmp/ -*.tmp -*.bak -*.swp -*~.nib -local.properties -.classpath -.settings/ -.loadpath - -# External tool builders -.externalToolBuilders/ - -# Locally stored "Eclipse launch configurations" -*.launch - -# CDT-specific -.cproject - -# PDT-specific -.buildpath - -################# -# PHPStorm -################# -.idea - -################# -## Visual Studio -################# - -## Ignore Visual Studio temporary files, build results, and -## files generated by popular Visual Studio add-ons. - -# User-specific files -*.suo -*.user -*.sln.docstates - -# Build results - -[Dd]ebug/ -[Rr]elease/ -x64/ -build/ -[Bb]in/ -[Oo]bj/ - -# MSTest test Results -[Tt]est[Rr]esult*/ -[Bb]uild[Ll]og.* - -*_i.c -*_p.c -*.ilk -*.meta -*.obj -*.pch -*.pdb -*.pgc -*.pgd -*.rsp -*.sbr -*.tlb -*.tli -*.tlh -*.tmp -*.tmp_proj -*.log -*.vspscc -*.vssscc -.builds -*.pidb -*.log -*.scc - -# Visual C++ cache files -ipch/ -*.aps -*.ncb -*.opensdf -*.sdf -*.cachefile - -# Visual Studio profiler -*.psess -*.vsp -*.vspx - -# Guidance Automation Toolkit -*.gpState - -# ReSharper is a .NET coding add-in -_ReSharper*/ -*.[Rr]e[Ss]harper - -# TeamCity is a build add-in -_TeamCity* - -# DotCover is a Code Coverage Tool -*.dotCover - -# NCrunch -*.ncrunch* -.*crunch*.local.xml - -# Installshield output folder -[Ee]xpress/ - -# DocProject is a documentation generator add-in -DocProject/buildhelp/ -DocProject/Help/*.HxT -DocProject/Help/*.HxC -DocProject/Help/*.hhc -DocProject/Help/*.hhk -DocProject/Help/*.hhp -DocProject/Help/Html2 -DocProject/Help/html - -# Click-Once directory -publish/ - -# Publish Web Output -*.Publish.xml -*.pubxml - -# NuGet Packages Directory -## TODO: If you have NuGet Package Restore enabled, uncomment the next line -#packages/ - -# Windows Azure Build Output -csx -*.build.csdef - -# Windows Store app package directory -AppPackages/ - -# Others -sql/ -*.Cache -ClientBin/ -[Ss]tyle[Cc]op.* -~$* -*~ -*.dbmdl -*.[Pp]ublish.xml -*.pfx -*.publishsettings - -# RIA/Silverlight projects -Generated_Code/ - -# Backup & report files from converting an old project file to a newer -# Visual Studio version. Backup files are not needed, because we have git ;-) -_UpgradeReport_Files/ -Backup*/ -UpgradeLog*.XML -UpgradeLog*.htm - -# SQL Server files -App_Data/*.mdf -App_Data/*.ldf - -############# -## Windows detritus -############# - -# Windows image file caches -Thumbs.db -ehthumbs.db - -# Folder config file -Desktop.ini - -# Recycle Bin used on file shares -$RECYCLE.BIN/ - -# Mac crap -.DS_Store - - -############# -## Python -############# - -*.py[co] - -# Packages -*.egg -*.egg-info -dist/ -build/ -eggs/ -parts/ -var/ -sdist/ -develop-eggs/ -.installed.cfg - -# Installer logs -pip-log.txt - -# Unit test / coverage reports -.coverage -.tox - -#Translations -*.mo - -#Mr Developer -.mr.developer.cfg From 0e5c3b856bf8e0e80c735c670f8002bd47843590 Mon Sep 17 00:00:00 2001 From: Sarunas Date: Thu, 11 Jul 2013 16:14:31 +0100 Subject: [PATCH 5/6] Add Index generation, change primary key gen Add Index generation, change primary key gen --- EDatabaseCommand.php | 65 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 56 insertions(+), 9 deletions(-) diff --git a/EDatabaseCommand.php b/EDatabaseCommand.php index 7469193..500cb11 100644 --- a/EDatabaseCommand.php +++ b/EDatabaseCommand.php @@ -64,6 +64,11 @@ class EDatabaseCommand extends CConsoleCommand */ public $ignoreMigrationTable = true; + /** + * @var bool whether to ignore the SQLite if statements + */ + public $ignoreSQLiteChecks = true; + /** * @var bool whether to display the Foreign Keys warning */ @@ -124,7 +129,7 @@ public function actionDump($args) $filename = $this->migrationPath . DIRECTORY_SEPARATOR . $migrationClassName . ".php"; $prefixes = explode(",", $this->prefix); - $codeTruncate = $codeSchema = $codeForeignKeys = $codeInserts = ''; + $codeTruncate = $codeSchema = $codeForeignKeys = $codeIndexes = $codeInserts = ''; echo "Querying tables "; @@ -155,6 +160,7 @@ public function actionDump($args) if ($this->createSchema == true) { $codeSchema .= $this->generateSchema($table, $schema); $codeForeignKeys .= $this->generateForeignKeys($table, $schema); + $codeIndexes .= $this->generateIndexes($table, $schema); } if ($this->insertData == true) { @@ -162,7 +168,7 @@ public function actionDump($args) } } - $code .= $codeTruncate."\n".$codeSchema."\n".$codeForeignKeys."\n".$codeForeignKeys."\n".$codeInserts; + $code .= $codeTruncate."\n".$codeSchema."\n".$codeForeignKeys."\n".$codeIndexes."\n".$codeInserts; if ($this->foreignKeyChecks == false) { $code .= $this->indent(2) . "if (Yii::app()->db->schema instanceof CMysqlSchema)\n"; @@ -216,11 +222,14 @@ private function generateSchema($table, $schema) private function generatePrimaryKeys($columns) { + $keys = array(); foreach ($columns as $col) { - if ($col->isPrimaryKey && !$col->autoIncrement) { - return $this->indent(3) . '"PRIMARY KEY (' . $col->name . ')"' . "\n"; + if ($col->isPrimaryKey) { + $keys[] = "`$col->name`"; } } + + return $this->indent(3) . '"PRIMARY KEY (' . implode(',', $keys) . ')"' . "\n"; } private function generateForeignKeys($table, $schema) @@ -229,15 +238,56 @@ private function generateForeignKeys($table, $schema) return ""; } $code = "\n\n\n" . $this->indent(2) . "// Foreign Keys for table '" . $table->name . "'\n"; - $code .= $this->indent(2) . "if ((Yii::app()->db->schema instanceof CSqliteSchema) == false):\n"; + + if(!$this->ignoreSQLiteChecks){ + $code .= $this->indent(2) . "if ((Yii::app()->db->schema instanceof CSqliteSchema) == false):\n"; + } + foreach ($table->foreignKeys as $name => $foreignKey) { $code .= $this->indent(3) . "\$this->addForeignKey('fk_{$table->name}_{$foreignKey[0]}_{$name}', '{$table->name}', '{$name}', '{$foreignKey[0]}', '{$foreignKey[1]}', null, null); // FIX RELATIONS \n"; } - $code .= $this->indent(2) . "endif;\n"; + + if(!$this->ignoreSQLiteChecks){ + $code .= $this->indent(2) . "endif;\n"; + } $this->_displayFkWarning = TRUE; return $code; } + private function generateIndexes($table, $schema) + { + $indexes = Yii::app()->db->createCommand( + "SHOW INDEX FROM " . $table->name . " + WHERE Key_name != 'PRIMARY'" + )->queryAll(); + + if (count($indexes) == 0) { + return ""; + } + + $code = "\n" . $this->indent(2) . "// Indexes for table '" . $table->name . "'\n"; + if(!$this->ignoreSQLiteChecks){ + $code .= $this->indent(2) . "if ((Yii::app()->db->schema instanceof CSqliteSchema) == false):\n"; + } + + $checker = false; + + foreach ($indexes as $index) { + if (!isset($table->foreignKeys[$index['Column_name']])) { + $unique = !$index['Non_unique'] ? 'True' : 'False'; + $code .= $this->indent(3) . "\$this->createIndex('index_{$index['Table']}_{$index['Column_name']}', '{$index['Table']}', '{$index['Column_name']}', {$unique}); \n"; + $checker = true; + } + + } + + if(!$this->ignoreSQLiteChecks){ + $code .= $this->indent(2) . "endif;\n"; + } + + //remove everything if there were no results + return $checker ? $code : ''; + } private function generateInserts($table, $schema) { $data = Yii::app()->{$this->dbConnection}->createCommand() @@ -273,9 +323,6 @@ private function resolveColumnType($col) if ($col->defaultValue != null) { $result .= " DEFAULT '{$col->defaultValue}'"; } - if ($col->isPrimaryKey) { - $result .= " PRIMARY KEY"; - } if ($col->autoIncrement) { $result .= " AUTO_INCREMENT"; } From 5452aa55486776ea0c2b142aee6494a94b5974de Mon Sep 17 00:00:00 2001 From: Sarunas Date: Thu, 11 Jul 2013 16:17:09 +0100 Subject: [PATCH 6/6] The finnal version --- EDatabaseCommand.php | 80 +++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 34 deletions(-) diff --git a/EDatabaseCommand.php b/EDatabaseCommand.php index 500cb11..7e90ccf 100644 --- a/EDatabaseCommand.php +++ b/EDatabaseCommand.php @@ -3,10 +3,10 @@ /** * Class file. * - * @author Tobias Munk - * @link http://www.phundament.com/ + * @author Tobias Munk + * @link http://www.phundament.com/ * @copyright Copyright © 2005-2011 diemeisterei GmbH - * @license http://www.phundament.com/license/ + * @license http://www.phundament.com/license/ */ /** @@ -27,7 +27,7 @@ class EDatabaseCommand extends CConsoleCommand * Defaults to 'application.runtime' (meaning 'protected/runtime'). * Copy the created migration into eg. application.migrations to activate it for your project. */ - public $migrationPath='application.runtime'; + public $migrationPath = 'application.migrations'; /** * @var string database connection component @@ -35,22 +35,22 @@ class EDatabaseCommand extends CConsoleCommand public $dbConnection = "db"; /** - * @var string wheter to dump a create table statement + * @var string whether to dump a create table statement */ public $createSchema = true; /** - * @var string wheter to dump a insert data statements + * @var string whether to dump a insert data statements */ public $insertData = true; /** - * @var string wheter to add truncate table data statements + * @var string whether to add truncate table data statements */ public $truncateTable = false; /** - * @var string wheter to disable foreign key checks + * @var string whether to disable foreign key checks */ public $foreignKeyChecks = true; @@ -60,7 +60,7 @@ class EDatabaseCommand extends CConsoleCommand public $prefix = ""; /** - * @var string wheter to ignore the migration table + * @var string whether to ignore the migration table */ public $ignoreMigrationTable = true; @@ -74,17 +74,16 @@ class EDatabaseCommand extends CConsoleCommand */ protected $_displayFkWarning = false; - public function beforeAction($action,$params) + public function beforeAction($action, $params) { - $path=Yii::getPathOfAlias($this->migrationPath); - if($path===false || !is_dir($path)) - { - echo 'Error: The migration directory does not exist: '.$this->migrationPath."\n"; + $path = Yii::getPathOfAlias($this->migrationPath); + if ($path === false || !is_dir($path)) { + echo 'Error: The migration directory does not exist: ' . $this->migrationPath . "\n"; exit(1); } - $this->migrationPath=$path; + $this->migrationPath = $path; - return parent::beforeAction($action,$params); + return parent::beforeAction($action, $params); } public function getHelp() @@ -99,13 +98,16 @@ public function getHelp() [--ignoreMigrationTable=<1|0>] [--truncateTable=<0|1>] [--migrationPath=] + //////To get only schema + php yiic database dump all_schema --insertData=0 + EOS; } public function actionDump($args) { - echo "Connecting to '".Yii::app()->{$this->dbConnection}->connectionString."'\n"; + echo "Connecting to '" . Yii::app()->{$this->dbConnection}->connectionString . "'\n"; $schema = Yii::app()->{$this->dbConnection}->schema; $tables = Yii::app()->{$this->dbConnection}->schema->tables; @@ -129,7 +131,7 @@ public function actionDump($args) $filename = $this->migrationPath . DIRECTORY_SEPARATOR . $migrationClassName . ".php"; $prefixes = explode(",", $this->prefix); - $codeTruncate = $codeSchema = $codeForeignKeys = $codeIndexes = $codeInserts = ''; + $codeTruncate = $codeSchema = $codeForeignKeysAndIndexes = $codeInserts = ''; echo "Querying tables "; @@ -159,8 +161,8 @@ public function actionDump($args) if ($this->createSchema == true) { $codeSchema .= $this->generateSchema($table, $schema); - $codeForeignKeys .= $this->generateForeignKeys($table, $schema); - $codeIndexes .= $this->generateIndexes($table, $schema); + $codeForeignKeysAndIndexes .= + $this->generateForeignKeys($table, $schema) . $this->generateIndexes($table, $schema); } if ($this->insertData == true) { @@ -168,7 +170,7 @@ public function actionDump($args) } } - $code .= $codeTruncate."\n".$codeSchema."\n".$codeForeignKeys."\n".$codeIndexes."\n".$codeInserts; + $code .= $codeTruncate . "\n" . $codeSchema . "\n" . $codeForeignKeysAndIndexes . "\n" . $codeInserts; if ($this->foreignKeyChecks == false) { $code .= $this->indent(2) . "if (Yii::app()->db->schema instanceof CMysqlSchema)\n"; @@ -176,8 +178,13 @@ public function actionDump($args) } $migrationClassCode = $this->renderFile( - dirname(__FILE__) . '/views/migration.php', array('migrationClassName' => $migrationClassName, - 'functionUp' => $code), true); + dirname(__FILE__) . '/views/migration.php', + array( + 'migrationClassName' => $migrationClassName, + 'functionUp' => $code + ), + true + ); file_put_contents($filename, $migrationClassCode); @@ -199,13 +206,13 @@ public function actionDump($args) private function indent($level = 0) { - return str_repeat(" ", $level); + return str_repeat(" ", $level); } private function generateSchema($table, $schema) { $options = "ENGINE=InnoDB DEFAULT CHARSET=utf8"; - $code = "\n\n\n" . $this->indent(2) . "// Schema for table '" . $table->name . "'\n"; + $code = "\n\n" . $this->indent(2) . "// Schema for table '" . $table->name . "'\n"; $code .= $this->indent(2) . '$this->createTable("' . $table->name . '", '; $code .= "\n"; $code .= $this->indent(3) . 'array(' . "\n"; @@ -228,7 +235,6 @@ private function generatePrimaryKeys($columns) $keys[] = "`$col->name`"; } } - return $this->indent(3) . '"PRIMARY KEY (' . implode(',', $keys) . ')"' . "\n"; } @@ -237,20 +243,21 @@ private function generateForeignKeys($table, $schema) if (count($table->foreignKeys) == 0) { return ""; } - $code = "\n\n\n" . $this->indent(2) . "// Foreign Keys for table '" . $table->name . "'\n"; + + $code = "\n" . $this->indent(2) . "// FOREIGN KEYS for table '" . $table->name . "'\n"; if(!$this->ignoreSQLiteChecks){ $code .= $this->indent(2) . "if ((Yii::app()->db->schema instanceof CSqliteSchema) == false):\n"; } foreach ($table->foreignKeys as $name => $foreignKey) { - $code .= $this->indent(3) . "\$this->addForeignKey('fk_{$table->name}_{$foreignKey[0]}_{$name}', '{$table->name}', '{$name}', '{$foreignKey[0]}', '{$foreignKey[1]}', null, null); // FIX RELATIONS \n"; + $code .= $this->indent(3) . "\$this->addForeignKey('fk_{$table->name}_{$foreignKey[0]}_{$name}', '{$table->name}', '{$name}', '{$foreignKey[0]}', '{$foreignKey[1]}', 'CASCADE', 'CASCADE'); // FIX RELATIONS \n"; } if(!$this->ignoreSQLiteChecks){ $code .= $this->indent(2) . "endif;\n"; } - $this->_displayFkWarning = TRUE; + $this->_displayFkWarning = true; return $code; } @@ -258,14 +265,14 @@ private function generateIndexes($table, $schema) { $indexes = Yii::app()->db->createCommand( "SHOW INDEX FROM " . $table->name . " - WHERE Key_name != 'PRIMARY'" + WHERE Key_name != 'PRIMARY'" )->queryAll(); if (count($indexes) == 0) { return ""; } - $code = "\n" . $this->indent(2) . "// Indexes for table '" . $table->name . "'\n"; + $code = "\n" . $this->indent(2) . "// INDEXES for table '" . $table->name . "'\n"; if(!$this->ignoreSQLiteChecks){ $code .= $this->indent(2) . "if ((Yii::app()->db->schema instanceof CSqliteSchema) == false):\n"; } @@ -280,7 +287,6 @@ private function generateIndexes($table, $schema) } } - if(!$this->ignoreSQLiteChecks){ $code .= $this->indent(2) . "endif;\n"; } @@ -288,6 +294,7 @@ private function generateIndexes($table, $schema) //remove everything if there were no results return $checker ? $code : ''; } + private function generateInserts($table, $schema) { $data = Yii::app()->{$this->dbConnection}->createCommand() @@ -299,7 +306,7 @@ private function generateInserts($table, $schema) $code .= $this->indent(2) . '$this->insert("' . $table->name . '", array(' . "\n"; foreach ($row AS $column => $value) { $code .= $this->indent(3) . '"' . $column . '"=>' . (($value === null) ? 'null' : - '"' . addcslashes($value, '"\\$') . '"') . ',' . "\n"; + '"' . addcslashes($value, '"\\$') . '"') . ',' . "\n"; } $code .= $this->indent(2) . ') );' . "\n\n"; } @@ -315,14 +322,19 @@ private function generateTruncate($table) private function resolveColumnType($col) { + $result = $col->dbType; if (!$col->allowNull) { $result .= ' NOT NULL'; } - if ($col->defaultValue != null) { + if ($col->defaultValue !== null) { + $result .= " DEFAULT '{$col->defaultValue}'"; } + if ($col->isPrimaryKey) { +// $result .= " PRIMARY KEY"; + } if ($col->autoIncrement) { $result .= " AUTO_INCREMENT"; }