From a13ff7495bdddcc6b90df2fa9ad6326d663f068f Mon Sep 17 00:00:00 2001 From: Sarunas Date: Thu, 11 Jul 2013 16:38:15 +0100 Subject: [PATCH 1/3] Add index generation and change PK generation Add index generation and change PK generation --- 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 81ab1909716b74e2df540f03701614b22fdc76a2 Mon Sep 17 00:00:00 2001 From: Sarunas Date: Fri, 12 Jul 2013 16:54:48 +0100 Subject: [PATCH 2/3] Enable SQLite checks --- EDatabaseCommand.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/EDatabaseCommand.php b/EDatabaseCommand.php index 500cb11..9f21ed1 100644 --- a/EDatabaseCommand.php +++ b/EDatabaseCommand.php @@ -67,7 +67,7 @@ class EDatabaseCommand extends CConsoleCommand /** * @var bool whether to ignore the SQLite if statements */ - public $ignoreSQLiteChecks = true; + public $ignoreSQLiteChecks = false; /** * @var bool whether to display the Foreign Keys warning @@ -275,7 +275,7 @@ private function generateIndexes($table, $schema) 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"; + $code .= $this->indent(3) . "\$this->createIndex('index_{$index['Column_name']}', '{$index['Table']}', '{$index['Column_name']}', {$unique}); \n"; $checker = true; } From 40a825cf896e985c39afde8b473a5b89e86f848d Mon Sep 17 00:00:00 2001 From: Sarunas Date: Fri, 12 Jul 2013 17:39:51 +0100 Subject: [PATCH 3/3] Multiple Indexes --- EDatabaseCommand.php | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/EDatabaseCommand.php b/EDatabaseCommand.php index 9f21ed1..2c3a0c4 100644 --- a/EDatabaseCommand.php +++ b/EDatabaseCommand.php @@ -271,21 +271,35 @@ private function generateIndexes($table, $schema) } $checker = false; + $addIndexes = array(); 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['Column_name']}', '{$index['Table']}', '{$index['Column_name']}', {$unique}); \n"; - $checker = true; + $key = $index['Key_name']; + if (!isset($addIndexes[$key])) { + $addIndexes[$key] = array( + 'name' => 'index_'.$index['Column_name'], + 'columns' => array(), + 'unique'=> !$index['Non_unique'] ? 'True' : 'False', + ); + $checker = true; + } + $addIndexes[$key]['columns'][] = $index['Column_name']; } + } + if (!$checker) { + return false; } - if(!$this->ignoreSQLiteChecks){ - $code .= $this->indent(2) . "endif;\n"; + foreach ($addIndexes as $index) { + $code .= $this->indent(3) . "\$this->createIndex('{$index['name']}', '{$table->name}', '".implode(',', $index['columns'])."', {$index['unique']}); \n"; + if(!$this->ignoreSQLiteChecks){ + $code .= $this->indent(2) . "endif;\n"; + } } - //remove everything if there were no results + //remove everything if there are no results return $checker ? $code : ''; } private function generateInserts($table, $schema)