diff --git a/src/AbstractMigration.php b/src/AbstractMigration.php new file mode 100644 index 00000000..7b4a1714 --- /dev/null +++ b/src/AbstractMigration.php @@ -0,0 +1,42 @@ +autoId === false) { + $options['id'] = false; + } + + return new Table($tableName, $options, $this->getAdapter()); + } +} diff --git a/src/Shell/Task/MigrationSnapshotTask.php b/src/Shell/Task/MigrationSnapshotTask.php index d7fd1c62..07b34cae 100644 --- a/src/Shell/Task/MigrationSnapshotTask.php +++ b/src/Shell/Task/MigrationSnapshotTask.php @@ -142,6 +142,12 @@ public function templateData() } } } + + $autoId = true; + if (isset($this->params['disable-autoid'])) { + $autoId = !$this->params['disable-autoid']; + } + return [ 'plugin' => $this->plugin, 'pluginPath' => $pluginPath, @@ -150,6 +156,7 @@ public function templateData() 'tables' => $tables, 'action' => 'create_table', 'name' => $this->BakeTemplate->viewVars['name'], + 'autoId' => $autoId ]; } @@ -271,6 +278,10 @@ public function getOptionParser() 'boolean' => true, 'default' => false, 'help' => 'If require-table is set to true, check also that the table class exists.' + ])->addOption('disable-autoid', [ + 'boolean' => true, + 'default' => false, + 'help' => 'Disable phinx behavior of automatically adding an id field.' ]); return $parser; diff --git a/src/Shell/Task/MigrationTask.php b/src/Shell/Task/MigrationTask.php index cb038bb9..c398f199 100644 --- a/src/Shell/Task/MigrationTask.php +++ b/src/Shell/Task/MigrationTask.php @@ -24,6 +24,7 @@ */ class MigrationTask extends SimpleMigrationTask { + /** * {@inheritDoc} */ @@ -75,6 +76,11 @@ public function templateData() $columnParser = new ColumnParser; $fields = $columnParser->parseFields($arguments); $indexes = $columnParser->parseIndexes($arguments); + $primaryKey = $columnParser->parsePrimaryKey($arguments); + + if (in_array($action[0], ['alter_table', 'add_field']) && !empty($primaryKey)) { + $this->error('Adding a primary key to an already existing table is not supported.'); + } list($action, $table) = $action; return [ @@ -86,6 +92,7 @@ public function templateData() 'columns' => [ 'fields' => $fields, 'indexes' => $indexes, + 'primaryKey' => $primaryKey ], 'name' => $className ]; diff --git a/src/Table.php b/src/Table.php new file mode 100644 index 00000000..d93a1451 --- /dev/null +++ b/src/Table.php @@ -0,0 +1,116 @@ +primaryKey = $columns; + return $this; + } + + /** + * You can pass `autoIncrement` as an option and it will be converted + * to the correct option for phinx to create the column with an + * auto increment attribute + * + * {@inheritdoc} + */ + public function addColumn($columnName, $type = null, $options = []) + { + if (isset($options['autoIncrement']) && $options['autoIncrement'] === true) { + $options['identity'] = true; + unset($options['autoIncrement']); + } + + return parent::addColumn($columnName, $type, $options); + } + + /** + * {@inheritdoc} + */ + public function create() + { + if ((!isset($this->options['id']) || $this->options['id'] === false) && !empty($this->primaryKey)) { + $this->options['primary_key'] = $this->primaryKey; + $this->filterPrimaryKey(); + } + + parent::create(); + } + + /** + * This method is called in case a primary key was defined using the addPrimaryKey() method. + * It currently does something only if using SQLite. + * If a column is an auto-increment key in SQLite, it has to be a primary key and it has to defined + * when defining the column. Phinx takes care of that so we have to make sure columns defined as autoincrement were + * not added with the addPrimaryKey method, otherwise, SQL queries will be wrong. + * + * @return void + */ + protected function filterPrimaryKey() + { + if (!($this->getAdapter() instanceof SQLiteAdapter) || empty($this->options['primary_key'])) { + return; + } + + $primaryKey = $this->options['primary_key']; + if (!is_array($primaryKey)) { + $primaryKey = [$primaryKey]; + } + $primaryKey = array_flip($primaryKey); + + $columnsCollection = new Collection($this->columns); + $primaryKeyColumns = $columnsCollection->filter(function ($columnDef, $key) use ($primaryKey) { + return isset($primaryKey[$columnDef->getName()]); + })->toArray(); + + if (empty($primaryKeyColumns)) { + return; + } + + foreach ($primaryKeyColumns as $primaryKeyColumn) { + if ($primaryKeyColumn->isIdentity()) { + unset($primaryKey[$primaryKeyColumn->getName()]); + } + } + + $primaryKey = array_flip($primaryKey); + + if (!empty($primaryKey)) { + $this->options['primary_key'] = $primaryKey; + } else { + unset($this->options['primary_key']); + } + } +} diff --git a/src/Template/Bake/config/skeleton.ctp b/src/Template/Bake/config/skeleton.ctp index 72c30b34..b197a1c7 100644 --- a/src/Template/Bake/config/skeleton.ctp +++ b/src/Template/Bake/config/skeleton.ctp @@ -13,16 +13,21 @@ * @license http://www.opensource.org/licenses/mit-license.php MIT License */ -$wantedOptions = array_flip(['length', 'limit', 'default', 'unsigned', 'null', 'comment']); +$wantedOptions = array_flip(['length', 'limit', 'default', 'unsigned', 'null', 'comment', 'autoIncrement']); $tableMethod = $this->Migration->tableMethod($action); $columnMethod = $this->Migration->columnMethod($action); $indexMethod = $this->Migration->indexMethod($action); %> extends AbstractMigration { + <%- if ($tableMethod === 'create' && !empty($columns['primaryKey'])): %> + + public $autoId = false; + + <%- endif; %> /** * Change Method. * @@ -61,6 +66,11 @@ class <%= $name %> extends AbstractMigration echo $this->Migration->stringifyList($config['options'], ['indent' => 3]); %>]); <% endforeach; %> +<% if ($tableMethod === 'create' && !empty($columns['primaryKey'])): %> + $table->addPrimaryKey([<%= + $this->Migration->stringifyList($columns['primaryKey'], ['indent' => 3]) + %>]); +<% endif; %> <% endif; %> <% endif; %> $table-><%= $tableMethod %>(); diff --git a/src/Template/Bake/config/snapshot.ctp b/src/Template/Bake/config/snapshot.ctp index 3195f3d1..7ede32f4 100644 --- a/src/Template/Bake/config/snapshot.ctp +++ b/src/Template/Bake/config/snapshot.ctp @@ -13,24 +13,29 @@ * @license http://www.opensource.org/licenses/mit-license.php MIT License */ -$wantedOptions = array_flip(['length', 'limit', 'default', 'unsigned', 'null', 'comment']); +$wantedOptions = array_flip(['length', 'limit', 'default', 'unsigned', 'null', 'comment', 'autoIncrement']); $tableMethod = $this->Migration->tableMethod($action); $columnMethod = $this->Migration->columnMethod($action); $indexMethod = $this->Migration->indexMethod($action); $constraints = $foreignKeys = $dropForeignKeys = []; %> extends AbstractMigration { + <%- if (!$autoId): %> + + public $autoId = false; + + <%- endif; %> public function up() { <%- foreach ($tables as $table): $foreignKeys = []; $primaryKeysColumns = $this->Migration->primaryKeysColumnsList($table); $primaryKeys = $this->Migration->primaryKeys($table); - $specialPk = count($primaryKeys) > 1 || $primaryKeys[0]['name'] !== 'id' || $primaryKeys[0]['info']['columnType'] !== 'integer'; + $specialPk = (count($primaryKeys) > 1 || $primaryKeys[0]['name'] !== 'id' || $primaryKeys[0]['info']['columnType'] !== 'integer') && $autoId; if ($specialPk): %> $table = $this->table('<%= $table%>', ['id' => false, 'primary_key' => ['<%= implode("', '", \Cake\Utility\Hash::extract($primaryKeys, '{n}.name')) %>']]); @@ -38,7 +43,7 @@ class <%= $name %> extends AbstractMigration $table = $this->table('<%= $table%>'); <%- endif; %> $table - <%- if ($specialPk): + <%- if ($specialPk || !$autoId): foreach ($primaryKeys as $primaryKey) : %> -><%= $columnMethod %>('<%= $primaryKey['name'] %>', '<%= $primaryKey['info']['columnType'] %>', [<% $options = []; @@ -46,9 +51,15 @@ class <%= $name %> extends AbstractMigration if (empty($columnOptions['comment'])) { unset($columnOptions['comment']); } + if (empty($columnOptions['autoIncrement'])) { + unset($columnOptions['autoIncrement']); + } echo $this->Migration->stringifyList($columnOptions, ['indent' => 4]); %>]) <%- endforeach; + if (!$autoId): %> + ->addPrimaryKey(['<%= implode("', '", \Cake\Utility\Hash::extract($primaryKeys, '{n}.name')) %>']) + <%- endif; endif; foreach ($this->Migration->columns($table) as $column => $config): %> -><%= $columnMethod %>('<%= $column %>', '<%= $config['columnType'] %>', [<% @@ -57,6 +68,9 @@ class <%= $name %> extends AbstractMigration if (empty($columnOptions['comment'])) { unset($columnOptions['comment']); } + if (empty($columnOptions['autoIncrement'])) { + unset($columnOptions['autoIncrement']); + } echo $this->Migration->stringifyList($columnOptions, ['indent' => 4]); %>]) <%- endforeach; diff --git a/src/Util/ColumnParser.php b/src/Util/ColumnParser.php index 3f3333c8..7507b8e7 100644 --- a/src/Util/ColumnParser.php +++ b/src/Util/ColumnParser.php @@ -5,14 +5,18 @@ use Cake\Utility\Hash; use ReflectionClass; +/** + * Utility class used to parse arguments passed to a ``bake migration`` class + */ class ColumnParser { + /** * Parses a list of arguments into an array of fields * * @param array $arguments A list of arguments being parsed * @return array - **/ + */ public function parseFields($arguments) { $fields = []; @@ -21,9 +25,16 @@ public function parseFields($arguments) preg_match('/^(\w*)(?::(\w*))?(?::(\w*))?(?::(\w*))?/', $field, $matches); $field = $matches[1]; $type = Hash::get($matches, 2); + $indexType = Hash::get($matches, 3); - if (in_array($type, ['primary', 'primary_key'])) { - $type = 'primary'; + $typeIsPk = in_array($type, ['primary', 'primary_key']); + $isPrimaryKey = false; + if ($typeIsPk || in_array($indexType, ['primary', 'primary_key'])) { + $isPrimaryKey = true; + + if ($typeIsPk) { + $type = 'primary'; + } } $type = $this->getType($field, $type); @@ -39,6 +50,10 @@ public function parseFields($arguments) if ($length !== null) { $fields[$field]['options']['limit'] = $length; } + + if ($isPrimaryKey === true && $type === 'integer') { + $fields[$field]['options']['autoIncrement'] = true; + } } return $fields; @@ -49,7 +64,7 @@ public function parseFields($arguments) * * @param array $arguments A list of arguments being parsed * @return array - **/ + */ public function parseIndexes($arguments) { $indexes = []; @@ -61,18 +76,14 @@ public function parseIndexes($arguments) $indexType = Hash::get($matches, 3); $indexName = Hash::get($matches, 4); - if (in_array($type, ['primary', 'primary_key'])) { - $indexType = 'primary'; - } - - if ($indexType === null) { + if (in_array($type, ['primary', 'primary_key']) || + in_array($indexType, ['primary', 'primary_key']) || + $indexType === null) { continue; } $indexUnique = false; - if ($indexType == 'primary') { - $indexUnique = true; - } elseif ($indexType == 'unique') { + if ($indexType == 'unique') { $indexUnique = true; } @@ -94,12 +105,37 @@ public function parseIndexes($arguments) return $indexes; } + /** + * Parses a list of arguments into an array of fields composing the primary key + * of the table + * + * @param array $arguments A list of arguments being parsed + * @return array + */ + public function parsePrimaryKey($arguments) + { + $primaryKey = []; + $arguments = $this->validArguments($arguments); + foreach ($arguments as $field) { + preg_match('/^(\w*)(?::(\w*))?(?::(\w*))?(?::(\w*))?/', $field, $matches); + $field = $matches[1]; + $type = Hash::get($matches, 2); + $indexType = Hash::get($matches, 3); + + if (in_array($type, ['primary', 'primary_key']) || in_array($indexType, ['primary', 'primary_key'])) { + $primaryKey[] = $field; + } + } + + return $primaryKey; + } + /** * Returns a list of only valid arguments * * @param array $arguments A list of arguments * @return array - **/ + */ public function validArguments($arguments) { $collection = new Collection($arguments); @@ -115,7 +151,7 @@ public function validArguments($arguments) * @param string $field Name of field * @param string $type User-specified type * @return string - **/ + */ public function getType($field, $type) { $reflector = new ReflectionClass('Phinx\Db\Adapter\AdapterInterface'); @@ -145,7 +181,7 @@ public function getType($field, $type) * * @param string $type User-specified type * @return int - **/ + */ public function getLength($type) { $length = null; @@ -168,7 +204,7 @@ public function getLength($type) * @param string $indexName Name of index * @param bool $indexUnique Whether this is a unique index or not * @return string - **/ + */ public function getIndexName($field, $indexType, $indexName, $indexUnique) { if (empty($indexName)) { diff --git a/src/View/Helper/MigrationHelper.php b/src/View/Helper/MigrationHelper.php index 8e1f2b6a..a6f34adb 100644 --- a/src/View/Helper/MigrationHelper.php +++ b/src/View/Helper/MigrationHelper.php @@ -284,7 +284,8 @@ public function attributes($table, $column) 'precision', 'scale', 'after', 'update', 'comment', 'unsigned', - 'signed', 'properties' + 'signed', 'properties', + 'autoIncrement' ]; $attributes = []; diff --git a/tests/TestCase/MigrationsTest.php b/tests/TestCase/MigrationsTest.php index 649cd445..07c1f1e0 100644 --- a/tests/TestCase/MigrationsTest.php +++ b/tests/TestCase/MigrationsTest.php @@ -108,6 +108,11 @@ public function testStatus() 'status' => 'down', 'id' => '20150724233100', 'name' => 'UpdateNumbersTable' + ], + [ + 'status' => 'down', + 'id' => '20150826191400', + 'name' => 'CreateLettersTable' ] ]; $this->assertEquals($expected, $result); @@ -135,19 +140,35 @@ public function testMigrateAndRollback() 'status' => 'up', 'id' => '20150724233100', 'name' => 'UpdateNumbersTable' + ], + [ + 'status' => 'up', + 'id' => '20150826191400', + 'name' => 'CreateLettersTable' ] ]; $this->assertEquals($expectedStatus, $status); - $table = TableRegistry::get('Numbers', ['connection' => $this->Connection]); - $columns = $table->schema()->columns(); + $numbersTable = TableRegistry::get('Numbers', ['connection' => $this->Connection]); + $columns = $numbersTable->schema()->columns(); $expected = ['id', 'number', 'radix']; $this->assertEquals($columns, $expected); + $primaryKey = $numbersTable->schema()->primaryKey(); + $this->assertEquals($primaryKey, ['id']); + + $lettersTable = TableRegistry::get('Letters', ['connection' => $this->Connection]); + $columns = $lettersTable->schema()->columns(); + $expected = ['id', 'letter']; + $this->assertEquals($expected, $columns); + $idColumn = $lettersTable->schema()->column('id'); + $this->assertEquals(true, $idColumn['autoIncrement']); + $primaryKey = $lettersTable->schema()->primaryKey(); + $this->assertEquals($primaryKey, ['id']); // Rollback last $rollback = $this->migrations->rollback(); $this->assertTrue($rollback); - $expectedStatus[1]['status'] = 'down'; + $expectedStatus[2]['status'] = 'down'; $status = $this->migrations->status(); $this->assertEquals($expectedStatus, $status); @@ -155,7 +176,7 @@ public function testMigrateAndRollback() $this->migrations->migrate(); $rollback = $this->migrations->rollback(['target' => 0]); $this->assertTrue($rollback); - $expectedStatus[0]['status'] = 'down'; + $expectedStatus[0]['status'] = $expectedStatus[1]['status'] = 'down'; $status = $this->migrations->status(); $this->assertEquals($expectedStatus, $status); } @@ -215,6 +236,11 @@ public function testOverrideOptions() 'status' => 'down', 'id' => '20150724233100', 'name' => 'UpdateNumbersTable' + ], + [ + 'status' => 'down', + 'id' => '20150826191400', + 'name' => 'CreateLettersTable' ] ]; $this->assertEquals($expectedStatus, $result); @@ -269,6 +295,11 @@ public function testMigrateDateOption() 'status' => 'down', 'id' => '20150724233100', 'name' => 'UpdateNumbersTable' + ], + [ + 'status' => 'down', + 'id' => '20150826191400', + 'name' => 'CreateLettersTable' ] ]; $this->assertEquals($expectedStatus, $this->migrations->status()); @@ -286,6 +317,11 @@ public function testMigrateDateOption() 'status' => 'down', 'id' => '20150724233100', 'name' => 'UpdateNumbersTable' + ], + [ + 'status' => 'down', + 'id' => '20150826191400', + 'name' => 'CreateLettersTable' ] ]; $this->assertEquals($expectedStatus, $this->migrations->status()); @@ -304,6 +340,11 @@ public function testMigrateDateOption() 'status' => 'up', 'id' => '20150724233100', 'name' => 'UpdateNumbersTable' + ], + [ + 'status' => 'down', + 'id' => '20150826191400', + 'name' => 'CreateLettersTable' ] ]; $this->assertEquals($expectedStatus, $this->migrations->status()); @@ -321,6 +362,11 @@ public function testMigrateDateOption() 'status' => 'down', 'id' => '20150724233100', 'name' => 'UpdateNumbersTable' + ], + [ + 'status' => 'down', + 'id' => '20150826191400', + 'name' => 'CreateLettersTable' ] ]; $this->assertEquals($expectedStatus, $this->migrations->status()); @@ -341,6 +387,11 @@ public function testMigrateDateOption() 'status' => 'down', 'id' => '20150724233100', 'name' => 'UpdateNumbersTable' + ], + [ + 'status' => 'down', + 'id' => '20150826191400', + 'name' => 'CreateLettersTable' ] ]; $this->assertEquals($expectedStatus, $this->migrations->status()); @@ -352,7 +403,7 @@ public function testMigrateDateOption() * @dataProvider migrationsProvider * @return void */ - public function testMigrateSnapshots($basePath) + public function testMigrateSnapshots($basePath, $files) { $destination = ROOT . 'config' . DS . 'SnapshotTests' . DS; $timestamp = Util::getCurrentTimestamp(); @@ -361,29 +412,19 @@ public function testMigrateSnapshots($basePath) mkdir($destination); } - copy( - $basePath . 'testCompositeConstraintsSnapshot.php', - $destination . $timestamp . '_testCompositeConstraintsSnapshot.php' - ); - - $result = $this->migrations->migrate(['source' => 'SnapshotTests']); - $this->assertTrue($result); - - $this->migrations->rollback(['source' => 'SnapshotTests']); - - unlink($destination . $timestamp . '_testCompositeConstraintsSnapshot.php'); - - copy( - $basePath . 'testNotEmptySnapshot.php', - $destination . $timestamp . '_testNotEmptySnapshot.php' - ); - - $result = $this->migrations->migrate(['source' => 'SnapshotTests']); - $this->assertTrue($result); + foreach ($files as $file) { + $copiedFileName = $timestamp . '_' . $file . '.php'; + copy( + $basePath . $file . '.php', + $destination . $copiedFileName + ); - $this->migrations->rollback(['source' => 'SnapshotTests']); + $result = $this->migrations->migrate(['source' => 'SnapshotTests']); + $this->assertTrue($result); - unlink($destination . $timestamp . '_testNotEmptySnapshot.php'); + $this->migrations->rollback(['source' => 'SnapshotTests']); + unlink($destination . $copiedFileName); + } } /** @@ -394,9 +435,24 @@ public function testMigrateSnapshots($basePath) public function migrationsProvider() { return [ - [Plugin::path('Migrations') . 'tests' . DS . 'comparisons' . DS . 'Migration' . DS], - [Plugin::path('Migrations') . 'tests' . DS . 'comparisons' . DS . 'Migration' . DS . 'sqlite' . DS], - [Plugin::path('Migrations') . 'tests' . DS . 'comparisons' . DS . 'Migration' . DS . 'pgsql' . DS] + [ + Plugin::path('Migrations') . 'tests' . DS . 'comparisons' . DS . 'Migration' . DS, + [ + 'testCompositeConstraintsSnapshot', + 'testNotEmptySnapshot', + 'testAutoIdDisabledSnapshot', + 'testCreatePrimaryKey', + 'testCreatePrimaryKeyUuid' + ] + ], + [ + Plugin::path('Migrations') . 'tests' . DS . 'comparisons' . DS . 'Migration' . DS . 'sqlite' . DS, + ['testCompositeConstraintsSnapshot', 'testNotEmptySnapshot', 'testAutoIdDisabledSnapshot'] + ], + [ + Plugin::path('Migrations') . 'tests' . DS . 'comparisons' . DS . 'Migration' . DS . 'pgsql' . DS, + ['testCompositeConstraintsSnapshot', 'testNotEmptySnapshot', 'testAutoIdDisabledSnapshot'] + ] ]; } } diff --git a/tests/TestCase/Shell/Task/MigrationSnapshotTaskTest.php b/tests/TestCase/Shell/Task/MigrationSnapshotTaskTest.php index c25e65e0..d44a87bb 100644 --- a/tests/TestCase/Shell/Task/MigrationSnapshotTaskTest.php +++ b/tests/TestCase/Shell/Task/MigrationSnapshotTaskTest.php @@ -92,6 +92,18 @@ public function testNotEmptySnapshot() $this->assertCorrectSnapshot(__FUNCTION__, $result); } + public function testAutoIdDisabledSnapshot() + { + $this->Task->params['require-table'] = false; + $this->Task->params['disable-autoid'] = true; + $this->Task->params['connection'] = 'test'; + $this->Task->params['plugin'] = 'BogusPlugin'; + + $result = $this->Task->bake('AutoIdDisabledSnapshot'); + + $this->assertCorrectSnapshot(__FUNCTION__, $result); + } + public function testCompositeConstraintsSnapshot() { $this->skipIf( diff --git a/tests/TestCase/Shell/Task/MigrationTaskTest.php b/tests/TestCase/Shell/Task/MigrationTaskTest.php index 00181045..8cb1b505 100644 --- a/tests/TestCase/Shell/Task/MigrationTaskTest.php +++ b/tests/TestCase/Shell/Task/MigrationTaskTest.php @@ -37,7 +37,7 @@ public function setUp() $this->Task = $this->getMock( 'Migrations\Shell\Task\MigrationTask', - ['in', 'err', 'createFile', '_stop'], + ['in', 'err', 'createFile', '_stop', 'error'], [$inputOutput] ); $this->Task->name = 'Migration'; @@ -59,7 +59,7 @@ public function testNoContents() } /** - * Test the excute method. + * Test the execute method. * * @return void */ @@ -80,6 +80,50 @@ public function testCreate() ]; $result = $this->Task->bake('CreateUsers'); $this->assertSameAsFile(__FUNCTION__ . 'Datetime.php', $result); + + $this->Task->args = [ + 'create_users', + 'id:integer:primary_key', + 'name', + 'created', + 'modified' + ]; + $result = $this->Task->bake('CreateUsers'); + $this->assertSameAsFile(__FUNCTION__ . 'PrimaryKey.php', $result); + + $this->Task->args = [ + 'create_users', + 'id:uuid:primary_key', + 'name', + 'created', + 'modified' + ]; + $result = $this->Task->bake('CreateUsers'); + $this->assertSameAsFile(__FUNCTION__ . 'PrimaryKeyUuid.php', $result); + } + + /** + * Test that adding a field or altering a table with a primary + * key will error out + * + * @return void + */ + public function testAddPrimaryKeyToExistingTable() + { + $this->Task->expects($this->any()) + ->method('error'); + + $this->Task->args = [ + 'add_pk_to_users', + 'somefield:primary_key' + ]; + $this->Task->bake('AddPkToUsers'); + + $this->Task->args = [ + 'alter_users', + 'somefield:primary_key' + ]; + $this->Task->bake('AlterUsers'); } /** diff --git a/tests/TestCase/Util/ColumnParserTest.php b/tests/TestCase/Util/ColumnParserTest.php index a0adf596..6ee4b686 100644 --- a/tests/TestCase/Util/ColumnParserTest.php +++ b/tests/TestCase/Util/ColumnParserTest.php @@ -47,6 +47,7 @@ public function testParseFields() ], ], ], $this->columnParser->parseFields(['id'])); + $this->assertEquals([ 'id' => [ 'columnType' => 'integer', @@ -54,16 +55,18 @@ public function testParseFields() 'null' => false, 'default' => null, 'limit' => 11, + 'autoIncrement' => true ], ], ], $this->columnParser->parseFields(['id:primary'])); + $this->assertEquals([ 'id' => [ 'columnType' => 'integer', 'options' => [ 'null' => false, 'default' => null, - 'limit' => 11, + 'limit' => 11 ], ], 'name' => [ @@ -75,13 +78,14 @@ public function testParseFields() ], ], ], $this->columnParser->parseFields(['id', 'name'])); + $this->assertEquals([ 'id' => [ 'columnType' => 'integer', 'options' => [ 'null' => false, 'default' => null, - 'limit' => 11, + 'limit' => 11 ], ], 'created' => [ @@ -113,22 +117,6 @@ public function testParseFields() */ public function testParseIndexes() { - $this->assertEquals(['PRIMARY' => [ - 'columns' => ['id'], - 'options' => ['unique' => true, 'name' => 'PRIMARY'] - ]], $this->columnParser->parseIndexes(['id:primary_key'])); - $this->assertEquals(['PRIMARY' => [ - 'columns' => ['id'], - 'options' => ['unique' => true, 'name' => 'PRIMARY'] - ]], $this->columnParser->parseIndexes(['id:primary'])); - $this->assertEquals(['PRIMARY' => [ - 'columns' => ['id'], - 'options' => ['unique' => true, 'name' => 'PRIMARY'] - ]], $this->columnParser->parseIndexes(['id:integer:primary'])); - $this->assertEquals(['ID_INDEX' => [ - 'columns' => ['id'], - 'options' => ['unique' => true, 'name' => 'ID_INDEX'] - ]], $this->columnParser->parseIndexes(['id:integer:primary:ID_INDEX'])); $this->assertEquals(['UNIQUE_ID' => [ 'columns' => ['id'], 'options' => ['unique' => true, 'name' => 'UNIQUE_ID'] @@ -147,6 +135,20 @@ public function testParseIndexes() ])); } + /** + * @covers Migrations\Util\ColumnParser::parsePrimaryKey + */ + public function testParsePrimaryKey() + { + $this->assertEquals(['id'], $this->columnParser->parsePrimaryKey(['id:primary'])); + $this->assertEquals(['id'], $this->columnParser->parsePrimaryKey(['id:integer:primary'])); + $this->assertEquals(['id'], $this->columnParser->parsePrimaryKey(['id:integer:primary:ID_INDEX'])); + $this->assertEquals( + ['id', 'name'], + $this->columnParser->parsePrimaryKey(['id:integer:primary', 'name:primary_key']) + ); + } + /** * @covers Migrations\Util\ColumnParser::validArguments */ diff --git a/tests/TestCase/View/Helper/MigrationHelperTest.php b/tests/TestCase/View/Helper/MigrationHelperTest.php index ca36cf3a..637069d3 100644 --- a/tests/TestCase/View/Helper/MigrationHelperTest.php +++ b/tests/TestCase/View/Helper/MigrationHelperTest.php @@ -179,6 +179,7 @@ public function testColumn() 'precision' => null, 'comment' => $this->values['comment'], 'signed' => true, + 'autoIncrement' => true ], ], $this->Helper->column($tableSchema, 'id')); @@ -258,6 +259,7 @@ public function testAttributes() 'precision' => null, 'comment' => $this->values['comment'], 'signed' => true, + 'autoIncrement' => true ], $this->Helper->attributes('users', 'id')); $this->assertEquals([ @@ -301,6 +303,7 @@ public function testAttributes() 'precision' => null, 'comment' => $this->values['comment'], 'signed' => true, + 'autoIncrement' => null ], $this->Helper->attributes('special_tags', 'article_id')); } diff --git a/tests/comparisons/Migration/pgsql/testAutoIdDisabledSnapshot.php b/tests/comparisons/Migration/pgsql/testAutoIdDisabledSnapshot.php new file mode 100644 index 00000000..b36abd0e --- /dev/null +++ b/tests/comparisons/Migration/pgsql/testAutoIdDisabledSnapshot.php @@ -0,0 +1,317 @@ +table('articles'); + $table + ->addColumn('id', 'integer', [ + 'autoIncrement' => true, + 'default' => null, + 'limit' => 10, + 'null' => false, + ]) + ->addPrimaryKey(['id']) + ->addColumn('title', 'string', [ + 'comment' => 'Article title', + 'default' => 'NULL::character varying', + 'limit' => 255, + 'null' => true, + ]) + ->addColumn('category_id', 'integer', [ + 'default' => null, + 'limit' => 10, + 'null' => true, + ]) + ->addColumn('product_id', 'integer', [ + 'default' => null, + 'limit' => 10, + 'null' => true, + ]) + ->addColumn('created', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addColumn('modified', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addIndex( + [ + 'category_id', + ] + ) + ->addIndex( + [ + 'product_id', + ] + ) + ->create(); + + $table = $this->table('categories'); + $table + ->addColumn('id', 'integer', [ + 'autoIncrement' => true, + 'default' => null, + 'limit' => 10, + 'null' => false, + ]) + ->addPrimaryKey(['id']) + ->addColumn('parent_id', 'integer', [ + 'default' => null, + 'limit' => 10, + 'null' => true, + ]) + ->addColumn('title', 'string', [ + 'default' => 'NULL::character varying', + 'limit' => 255, + 'null' => true, + ]) + ->addColumn('slug', 'string', [ + 'default' => 'NULL::character varying', + 'limit' => 255, + 'null' => true, + ]) + ->addColumn('created', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addColumn('modified', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addIndex( + [ + 'slug', + ], + ['unique' => true] + ) + ->create(); + + $table = $this->table('composite_pks'); + $table + ->addColumn('id', 'uuid', [ + 'default' => 'a4950df3-515f-474c-be4c-6a027c1957e7', + 'limit' => null, + 'null' => false, + ]) + ->addColumn('name', 'string', [ + 'default' => '', + 'limit' => 50, + 'null' => false, + ]) + ->addPrimaryKey(['id', 'name']) + ->create(); + + $table = $this->table('products'); + $table + ->addColumn('id', 'integer', [ + 'autoIncrement' => true, + 'default' => null, + 'limit' => 10, + 'null' => false, + ]) + ->addPrimaryKey(['id']) + ->addColumn('title', 'string', [ + 'default' => 'NULL::character varying', + 'limit' => 255, + 'null' => true, + ]) + ->addColumn('slug', 'string', [ + 'default' => 'NULL::character varying', + 'limit' => 255, + 'null' => true, + ]) + ->addColumn('category_id', 'integer', [ + 'default' => null, + 'limit' => 10, + 'null' => true, + ]) + ->addColumn('created', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addColumn('modified', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addIndex( + [ + 'slug', + ], + ['unique' => true] + ) + ->addIndex( + [ + 'category_id', + 'id', + ], + ['unique' => true] + ) + ->addIndex( + [ + 'category_id', + ] + ) + ->create(); + + $table = $this->table('special_pks'); + $table + ->addColumn('id', 'uuid', [ + 'default' => 'a4950df3-515f-474c-be4c-6a027c1957e7', + 'limit' => null, + 'null' => false, + ]) + ->addPrimaryKey(['id']) + ->addColumn('name', 'string', [ + 'default' => 'NULL::character varying', + 'limit' => 256, + 'null' => true, + ]) + ->create(); + + $table = $this->table('special_tags'); + $table + ->addColumn('id', 'integer', [ + 'autoIncrement' => true, + 'default' => null, + 'limit' => 10, + 'null' => false, + ]) + ->addPrimaryKey(['id']) + ->addColumn('article_id', 'integer', [ + 'default' => null, + 'limit' => 10, + 'null' => false, + ]) + ->addColumn('author_id', 'integer', [ + 'default' => null, + 'limit' => 10, + 'null' => true, + ]) + ->addColumn('tag_id', 'integer', [ + 'default' => null, + 'limit' => 10, + 'null' => false, + ]) + ->addColumn('highlighted', 'boolean', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addColumn('highlighted_time', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addIndex( + [ + 'article_id', + ], + ['unique' => true] + ) + ->create(); + + $table = $this->table('users'); + $table + ->addColumn('id', 'integer', [ + 'autoIncrement' => true, + 'default' => null, + 'limit' => 10, + 'null' => false, + ]) + ->addPrimaryKey(['id']) + ->addColumn('username', 'string', [ + 'default' => 'NULL::character varying', + 'limit' => 256, + 'null' => true, + ]) + ->addColumn('password', 'string', [ + 'default' => 'NULL::character varying', + 'limit' => 256, + 'null' => true, + ]) + ->addColumn('created', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addColumn('updated', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->create(); + + $this->table('articles') + ->addForeignKey( + 'category_id', + 'categories', + 'id', + [ + 'update' => 'CASCADE', + 'delete' => 'CASCADE' + ] + ) + ->addForeignKey( + 'product_id', + 'products', + 'id', + [ + 'update' => 'CASCADE', + 'delete' => 'CASCADE' + ] + ) + ->update(); + + $this->table('products') + ->addForeignKey( + 'category_id', + 'categories', + 'id', + [ + 'update' => 'CASCADE', + 'delete' => 'CASCADE' + ] + ) + ->update(); + + } + + public function down() + { + $this->table('articles') + ->dropForeignKey( + 'category_id' + ) + ->dropForeignKey( + 'product_id' + ) + ->update(); + + $this->table('products') + ->dropForeignKey( + 'category_id' + ) + ->update(); + + $this->dropTable('articles'); + $this->dropTable('categories'); + $this->dropTable('composite_pks'); + $this->dropTable('products'); + $this->dropTable('special_pks'); + $this->dropTable('special_tags'); + $this->dropTable('users'); + } +} diff --git a/tests/comparisons/Migration/pgsql/testCompositeConstraintsSnapshot.php b/tests/comparisons/Migration/pgsql/testCompositeConstraintsSnapshot.php index a7d04af3..18b91fa3 100644 --- a/tests/comparisons/Migration/pgsql/testCompositeConstraintsSnapshot.php +++ b/tests/comparisons/Migration/pgsql/testCompositeConstraintsSnapshot.php @@ -1,5 +1,5 @@ table('articles'); + $table + ->addColumn('id', 'integer', [ + 'autoIncrement' => true, + 'default' => null, + 'limit' => null, + 'null' => false, + ]) + ->addPrimaryKey(['id']) + ->addColumn('title', 'string', [ + 'default' => null, + 'limit' => 255, + 'null' => true, + ]) + ->addColumn('category_id', 'integer', [ + 'default' => null, + 'limit' => 11, + 'null' => true, + ]) + ->addColumn('product_id', 'integer', [ + 'default' => null, + 'limit' => 11, + 'null' => true, + ]) + ->addColumn('created', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addColumn('modified', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addIndex( + [ + 'category_id', + ] + ) + ->addIndex( + [ + 'product_id', + ] + ) + ->create(); + + $table = $this->table('categories'); + $table + ->addColumn('id', 'integer', [ + 'autoIncrement' => true, + 'default' => null, + 'limit' => null, + 'null' => false, + ]) + ->addPrimaryKey(['id']) + ->addColumn('parent_id', 'integer', [ + 'default' => null, + 'limit' => 11, + 'null' => true, + ]) + ->addColumn('title', 'string', [ + 'default' => null, + 'limit' => 255, + 'null' => true, + ]) + ->addColumn('slug', 'string', [ + 'default' => null, + 'limit' => 255, + 'null' => true, + ]) + ->addColumn('created', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addColumn('modified', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addIndex( + [ + 'slug', + ], + ['unique' => true] + ) + ->create(); + + $table = $this->table('composite_pks'); + $table + ->addColumn('id', 'uuid', [ + 'default' => 'a4950df3-515f-474c-be4c-6a027c1957e7', + 'limit' => null, + 'null' => false, + ]) + ->addColumn('name', 'string', [ + 'default' => '', + 'limit' => 50, + 'null' => false, + ]) + ->addPrimaryKey(['id', 'name']) + ->create(); + + $table = $this->table('products'); + $table + ->addColumn('id', 'integer', [ + 'autoIncrement' => true, + 'default' => null, + 'limit' => null, + 'null' => false, + ]) + ->addPrimaryKey(['id']) + ->addColumn('title', 'string', [ + 'default' => null, + 'limit' => 255, + 'null' => true, + ]) + ->addColumn('slug', 'string', [ + 'default' => null, + 'limit' => 255, + 'null' => true, + ]) + ->addColumn('category_id', 'integer', [ + 'default' => null, + 'limit' => 11, + 'null' => true, + ]) + ->addColumn('created', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addColumn('modified', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addIndex( + [ + 'slug', + ], + ['unique' => true] + ) + ->addIndex( + [ + 'category_id', + 'id', + ], + ['unique' => true] + ) + ->addIndex( + [ + 'category_id', + ] + ) + ->create(); + + $table = $this->table('special_pks'); + $table + ->addColumn('id', 'uuid', [ + 'default' => 'a4950df3-515f-474c-be4c-6a027c1957e7', + 'limit' => null, + 'null' => false, + ]) + ->addPrimaryKey(['id']) + ->addColumn('name', 'string', [ + 'default' => null, + 'limit' => 256, + 'null' => true, + ]) + ->create(); + + $table = $this->table('special_tags'); + $table + ->addColumn('id', 'integer', [ + 'autoIncrement' => true, + 'default' => null, + 'limit' => null, + 'null' => false, + ]) + ->addPrimaryKey(['id']) + ->addColumn('article_id', 'integer', [ + 'default' => null, + 'limit' => 11, + 'null' => false, + ]) + ->addColumn('author_id', 'integer', [ + 'default' => null, + 'limit' => 11, + 'null' => true, + ]) + ->addColumn('tag_id', 'integer', [ + 'default' => null, + 'limit' => 11, + 'null' => false, + ]) + ->addColumn('highlighted', 'boolean', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addColumn('highlighted_time', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addIndex( + [ + 'article_id', + ], + ['unique' => true] + ) + ->create(); + + $table = $this->table('users'); + $table + ->addColumn('id', 'integer', [ + 'autoIncrement' => true, + 'default' => null, + 'limit' => null, + 'null' => false, + ]) + ->addPrimaryKey(['id']) + ->addColumn('username', 'string', [ + 'default' => null, + 'limit' => 256, + 'null' => true, + ]) + ->addColumn('password', 'string', [ + 'default' => null, + 'limit' => 256, + 'null' => true, + ]) + ->addColumn('created', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addColumn('updated', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->create(); + + $this->table('articles') + ->addForeignKey( + 'category_id', + 'categories', + 'id', + [ + 'update' => 'CASCADE', + 'delete' => 'CASCADE' + ] + ) + ->addForeignKey( + 'product_id', + 'products', + 'id', + [ + 'update' => 'CASCADE', + 'delete' => 'CASCADE' + ] + ) + ->update(); + + $this->table('products') + ->addForeignKey( + 'category_id', + 'categories', + 'id', + [ + 'update' => 'CASCADE', + 'delete' => 'CASCADE' + ] + ) + ->update(); + + } + + public function down() + { + $this->table('articles') + ->dropForeignKey( + 'category_id' + ) + ->dropForeignKey( + 'product_id' + ) + ->update(); + + $this->table('products') + ->dropForeignKey( + 'category_id' + ) + ->update(); + + $this->dropTable('articles'); + $this->dropTable('categories'); + $this->dropTable('composite_pks'); + $this->dropTable('products'); + $this->dropTable('special_pks'); + $this->dropTable('special_tags'); + $this->dropTable('users'); + } +} diff --git a/tests/comparisons/Migration/sqlite/testCompositeConstraintsSnapshot.php b/tests/comparisons/Migration/sqlite/testCompositeConstraintsSnapshot.php index 6bff32fc..94cf9fe4 100644 --- a/tests/comparisons/Migration/sqlite/testCompositeConstraintsSnapshot.php +++ b/tests/comparisons/Migration/sqlite/testCompositeConstraintsSnapshot.php @@ -1,5 +1,5 @@ table('articles'); + $table + ->addColumn('id', 'integer', [ + 'autoIncrement' => true, + 'default' => null, + 'limit' => 11, + 'null' => false, + ]) + ->addPrimaryKey(['id']) + ->addColumn('title', 'string', [ + 'comment' => 'Article title', + 'default' => null, + 'limit' => 255, + 'null' => true, + ]) + ->addColumn('category_id', 'integer', [ + 'default' => null, + 'limit' => 11, + 'null' => true, + ]) + ->addColumn('product_id', 'integer', [ + 'default' => null, + 'limit' => 11, + 'null' => true, + ]) + ->addColumn('created', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addColumn('modified', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addIndex( + [ + 'category_id', + ] + ) + ->addIndex( + [ + 'product_id', + ] + ) + ->create(); + + $table = $this->table('categories'); + $table + ->addColumn('id', 'integer', [ + 'autoIncrement' => true, + 'default' => null, + 'limit' => 11, + 'null' => false, + ]) + ->addPrimaryKey(['id']) + ->addColumn('parent_id', 'integer', [ + 'default' => null, + 'limit' => 11, + 'null' => true, + ]) + ->addColumn('title', 'string', [ + 'default' => null, + 'limit' => 255, + 'null' => true, + ]) + ->addColumn('slug', 'string', [ + 'default' => null, + 'limit' => 255, + 'null' => true, + ]) + ->addColumn('created', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addColumn('modified', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addIndex( + [ + 'slug', + ], + ['unique' => true] + ) + ->create(); + + $table = $this->table('composite_pks'); + $table + ->addColumn('id', 'uuid', [ + 'default' => 'a4950df3-515f-474c-be4c-6a027c1957e7', + 'limit' => null, + 'null' => false, + ]) + ->addColumn('name', 'string', [ + 'default' => '', + 'limit' => 50, + 'null' => false, + ]) + ->addPrimaryKey(['id', 'name']) + ->create(); + + $table = $this->table('products'); + $table + ->addColumn('id', 'integer', [ + 'autoIncrement' => true, + 'default' => null, + 'limit' => 11, + 'null' => false, + ]) + ->addPrimaryKey(['id']) + ->addColumn('title', 'string', [ + 'default' => null, + 'limit' => 255, + 'null' => true, + ]) + ->addColumn('slug', 'string', [ + 'default' => null, + 'limit' => 255, + 'null' => true, + ]) + ->addColumn('category_id', 'integer', [ + 'default' => null, + 'limit' => 11, + 'null' => true, + ]) + ->addColumn('created', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addColumn('modified', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addIndex( + [ + 'slug', + ], + ['unique' => true] + ) + ->addIndex( + [ + 'category_id', + 'id', + ], + ['unique' => true] + ) + ->addIndex( + [ + 'category_id', + ] + ) + ->create(); + + $table = $this->table('special_pks'); + $table + ->addColumn('id', 'uuid', [ + 'default' => 'a4950df3-515f-474c-be4c-6a027c1957e7', + 'limit' => null, + 'null' => false, + ]) + ->addPrimaryKey(['id']) + ->addColumn('name', 'string', [ + 'default' => null, + 'limit' => 256, + 'null' => true, + ]) + ->create(); + + $table = $this->table('special_tags'); + $table + ->addColumn('id', 'integer', [ + 'autoIncrement' => true, + 'default' => null, + 'limit' => 11, + 'null' => false, + ]) + ->addPrimaryKey(['id']) + ->addColumn('article_id', 'integer', [ + 'default' => null, + 'limit' => 11, + 'null' => false, + ]) + ->addColumn('author_id', 'integer', [ + 'default' => null, + 'limit' => 11, + 'null' => true, + ]) + ->addColumn('tag_id', 'integer', [ + 'default' => null, + 'limit' => 11, + 'null' => false, + ]) + ->addColumn('highlighted', 'boolean', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addColumn('highlighted_time', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addIndex( + [ + 'article_id', + ], + ['unique' => true] + ) + ->create(); + + $table = $this->table('users'); + $table + ->addColumn('id', 'integer', [ + 'autoIncrement' => true, + 'default' => null, + 'limit' => 11, + 'null' => false, + ]) + ->addPrimaryKey(['id']) + ->addColumn('username', 'string', [ + 'default' => null, + 'limit' => 256, + 'null' => true, + ]) + ->addColumn('password', 'string', [ + 'default' => null, + 'limit' => 256, + 'null' => true, + ]) + ->addColumn('created', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->addColumn('updated', 'timestamp', [ + 'default' => null, + 'limit' => null, + 'null' => true, + ]) + ->create(); + + $this->table('articles') + ->addForeignKey( + 'category_id', + 'categories', + 'id', + [ + 'update' => 'CASCADE', + 'delete' => 'CASCADE' + ] + ) + ->addForeignKey( + 'product_id', + 'products', + 'id', + [ + 'update' => 'CASCADE', + 'delete' => 'CASCADE' + ] + ) + ->update(); + + $this->table('products') + ->addForeignKey( + 'category_id', + 'categories', + 'id', + [ + 'update' => 'CASCADE', + 'delete' => 'CASCADE' + ] + ) + ->update(); + + } + + public function down() + { + $this->table('articles') + ->dropForeignKey( + 'category_id' + ) + ->dropForeignKey( + 'product_id' + ) + ->update(); + + $this->table('products') + ->dropForeignKey( + 'category_id' + ) + ->update(); + + $this->dropTable('articles'); + $this->dropTable('categories'); + $this->dropTable('composite_pks'); + $this->dropTable('products'); + $this->dropTable('special_pks'); + $this->dropTable('special_tags'); + $this->dropTable('users'); + } +} diff --git a/tests/comparisons/Migration/testCompositeConstraintsSnapshot.php b/tests/comparisons/Migration/testCompositeConstraintsSnapshot.php index 1027c454..ae5b3d0f 100644 --- a/tests/comparisons/Migration/testCompositeConstraintsSnapshot.php +++ b/tests/comparisons/Migration/testCompositeConstraintsSnapshot.php @@ -1,5 +1,5 @@ table('users'); + $table->addColumn('id', 'integer', [ + 'autoIncrement' => true, + 'default' => null, + 'limit' => 11, + 'null' => false, + ]); + $table->addColumn('name', 'string', [ + 'default' => null, + 'limit' => 255, + 'null' => false, + ]); + $table->addColumn('created', 'datetime', [ + 'default' => null, + 'null' => false, + ]); + $table->addColumn('modified', 'datetime', [ + 'default' => null, + 'null' => false, + ]); + $table->addPrimaryKey([ + 'id', + ]); + $table->create(); + } +} diff --git a/tests/comparisons/Migration/testCreatePrimaryKeyUuid.php b/tests/comparisons/Migration/testCreatePrimaryKeyUuid.php new file mode 100644 index 00000000..f46b59fe --- /dev/null +++ b/tests/comparisons/Migration/testCreatePrimaryKeyUuid.php @@ -0,0 +1,41 @@ +table('users'); + $table->addColumn('id', 'uuid', [ + 'default' => null, + 'null' => false, + ]); + $table->addColumn('name', 'string', [ + 'default' => null, + 'limit' => 255, + 'null' => false, + ]); + $table->addColumn('created', 'datetime', [ + 'default' => null, + 'null' => false, + ]); + $table->addColumn('modified', 'datetime', [ + 'default' => null, + 'null' => false, + ]); + $table->addPrimaryKey([ + 'id', + ]); + $table->create(); + } +} diff --git a/tests/comparisons/Migration/testNoContents.php b/tests/comparisons/Migration/testNoContents.php index 2835cc2d..8be04b13 100644 --- a/tests/comparisons/Migration/testNoContents.php +++ b/tests/comparisons/Migration/testNoContents.php @@ -1,5 +1,5 @@ table('letters'); + $table + ->addColumn('id', 'integer', [ + 'autoIncrement' => true, + 'default' => null, + 'limit' => null, + 'null' => false, + ]) + ->addPrimaryKey(['id']) + ->addColumn('letter', 'string', [ + 'limit' => 1 + ]) + ->create(); + } + + public function down() + { + $this->dropTable('letters'); + } +}