Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 70 additions & 9 deletions EDatabaseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down Expand Up @@ -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 ";

Expand Down Expand Up @@ -155,14 +160,15 @@ 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) {
$codeInserts .= $this->generateInserts($table, $schema);
}
}

$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";
Expand Down Expand Up @@ -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";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this still work for SQLite?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know about SQLite sorry.

}

private function generateForeignKeys($table, $schema)
Expand All @@ -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){
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This if should be moved to line 301. I got a sytnax error unexpected endif when I ran a test.
Could you also verify the changeset by dumping a rather complex MySQL schema and then insert it into SQLite?

$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()
Expand Down Expand Up @@ -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";
}
Expand Down