diff --git a/EDatabaseCommand.php b/EDatabaseCommand.php index 7469193..2c3a0c4 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 = false; + /** * @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,70 @@ 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; + $addIndexes = array(); + + foreach ($indexes as $index) { + if (!isset($table->foreignKeys[$index['Column_name']])) { + $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; + } + + 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 are no results + return $checker ? $code : ''; + } private function generateInserts($table, $schema) { $data = Yii::app()->{$this->dbConnection}->createCommand() @@ -273,9 +337,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"; }