-
Notifications
You must be signed in to change notification settings - Fork 11
Add index generation and change PK generation #11
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sarunast
wants to merge
3
commits into
schmunk42:master
Choose a base branch
from
sarunast:patch-1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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,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"; | ||
|
|
@@ -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){ | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This |
||
| $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"; | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.