From cbb1dc8e114789428d5f2f0fb9872786bf8cb896 Mon Sep 17 00:00:00 2001 From: Franck DAKIA Date: Fri, 19 May 2023 21:33:38 +0000 Subject: [PATCH 1/4] Fixes migration table structore for support pgsql --- src/Console/Command/MigrationCommand.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Console/Command/MigrationCommand.php b/src/Console/Command/MigrationCommand.php index fbfbc0b6..4c99545f 100644 --- a/src/Console/Command/MigrationCommand.php +++ b/src/Console/Command/MigrationCommand.php @@ -274,9 +274,9 @@ private function createMigrationTable() 'create' ); - $generator->addColumn('migration', 'string', ['unique' => true]); - $generator->addColumn('batch', 'int'); - $generator->addColumn('created_at', 'datetime', [ + $generator->addString('migration', ['unique' => true]); + $generator->addInteger('batch'); + $generator->addDatetime('created_at', [ 'default' => 'CURRENT_TIMESTAMP', 'nullable' => true ]); From 9637a7c47451b05518424efce0abb4fd45b17254 Mon Sep 17 00:00:00 2001 From: Franck DAKIA Date: Fri, 19 May 2023 21:34:16 +0000 Subject: [PATCH 2/4] Refactoring migrations for create data type for enum --- .../Migration/Compose/MysqlCompose.php | 6 +++ .../Migration/Compose/PgsqlCompose.php | 53 ++++++++++++++++--- .../Migration/Compose/SqliteCompose.php | 10 ++-- src/Database/Migration/Migration.php | 19 +++++-- .../Migration/Shortcut/ConstraintColumn.php | 25 ++++++--- .../Migration/Shortcut/MixedColumn.php | 14 ++--- 6 files changed, 94 insertions(+), 33 deletions(-) diff --git a/src/Database/Migration/Compose/MysqlCompose.php b/src/Database/Migration/Compose/MysqlCompose.php index 698e4d2c..ab64cf5c 100644 --- a/src/Database/Migration/Compose/MysqlCompose.php +++ b/src/Database/Migration/Compose/MysqlCompose.php @@ -35,6 +35,7 @@ private function composeAddMysqlColumn(string $name, array $description): string $unsigned = $attribute['unsigned'] ?? false; $after = $attribute['after'] ?? false; $first = $attribute['first'] ?? false; + $custom = $attribute['custom'] ?? false; // String to VARCHAR if ($raw_type == 'STRING') { @@ -100,6 +101,11 @@ private function composeAddMysqlColumn(string $name, array $description): string $type = sprintf('%s FIRST', $type); } + // Apply the custom definition + if ($custom) { + $type = sprintf('%s %s', $type, $custom); + } + return trim( sprintf('%s `%s` %s', $description['command'], $name, $type) ); diff --git a/src/Database/Migration/Compose/PgsqlCompose.php b/src/Database/Migration/Compose/PgsqlCompose.php index 7d2cf794..aa2bbb53 100644 --- a/src/Database/Migration/Compose/PgsqlCompose.php +++ b/src/Database/Migration/Compose/PgsqlCompose.php @@ -6,6 +6,18 @@ trait PgsqlCompose { + protected array $custom_types = []; + + /** + * Generate the custom type for pgsql + * + * @return array + */ + public function generateCustomTypes(): array + { + return $this->custom_types; + } + /** * Compose sql statement for pgsql * @@ -35,6 +47,7 @@ private function composeAddPgsqlColumn(string $name, array $description): string $unsigned = $attribute['unsigned'] ?? false; $after = $attribute['after'] ?? false; $first = $attribute['first'] ?? false; + $custom = $attribute['custom'] ?? false; if ($after || $first) { throw new SQLGeneratorException("The key first and after only work on mysql"); @@ -45,18 +58,32 @@ private function composeAddPgsqlColumn(string $name, array $description): string $type = 'VARCHAR'; } + // Redefine the size if (!$size && in_array($raw_type, ['VARCHAR', 'STRING', 'LONG VARCHAR'])) { $size = 255; } // Add column size - if ($size) { - if (in_array($raw_type, ['ENUM', 'CHECK'])) { - $size = (array) $size; - $size = "'" . implode("', '", $size) . "'"; - } - if (in_array($raw_type, ['ENUM', 'CHECK', 'VARCHAR', 'LONG VARCHAR', 'STRING'])) { - $type = sprintf('%s(%s)', $type, $size); + if (in_array($raw_type, ['VARCHAR', 'STRING', 'LONG VARCHAR']) && $size) { + $type = sprintf('%s(%s)', $type, $size); + } + + if (in_array($raw_type, ['ENUM', 'CHECK'])) { + $size = (array) $size; + $size = "'" . implode("', '", $size) . "'"; + if ($raw_type == "ENUM") { + $table = preg_replace("/(ies)$/", "y", $this->table); + $table = preg_replace("/(s)$/", "", $table); + + $this->custom_types[] = sprintf( + "CREATE TYPE %s_%s_enum AS ENUM(%s);", + $table, + $name, + $size + ); + $type = sprintf('%s_%s_enum', $this->table, $name); + } else { + $type = sprintf('TEXT CHECK (%s IN CHECK(%s))', $name, $size); } } @@ -65,6 +92,11 @@ private function composeAddPgsqlColumn(string $name, array $description): string $type = 'SERIAL'; } + // Bind precision + if ($raw_type == "DOUBLE") { + $type = sprintf('DOUBLE PRECISION', $type); + } + // Set column as primary key if ($primary) { $type = sprintf('%s PRIMARY KEY', $type); @@ -98,8 +130,13 @@ private function composeAddPgsqlColumn(string $name, array $description): string $type = sprintf('UNSIGNED %s', $type); } + // Apply the custom definition + if ($custom) { + $type = sprintf('%s %s', $type, $custom); + } + return trim( - sprintf('%s %s %s', $description['command'], $name, $type) + sprintf('%s "%s" %s', $description['command'], $name, $type) ); } diff --git a/src/Database/Migration/Compose/SqliteCompose.php b/src/Database/Migration/Compose/SqliteCompose.php index 9be1e0e5..f23a86bd 100644 --- a/src/Database/Migration/Compose/SqliteCompose.php +++ b/src/Database/Migration/Compose/SqliteCompose.php @@ -33,6 +33,7 @@ private function composeAddSqliteColumn(string $name, array $description): strin $increment = $attribute['increment'] ?? false; $nullable = $attribute['nullable'] ?? false; $unique = $attribute['unique'] ?? false; + $custom = $attribute['custom'] ?? false; // String to VARCHAR if ($raw_type == 'STRING') { @@ -43,10 +44,6 @@ private function composeAddSqliteColumn(string $name, array $description): strin $size = 255; } - // Add column size - if ($size) { - } - // Set column as primary key if ($primary) { $type = sprintf('%s PRIMARY KEY', $type); @@ -79,6 +76,11 @@ private function composeAddSqliteColumn(string $name, array $description): strin $type = sprintf('%s DEFAULT %s', $type, $default); } + // Apply the custom definition + if ($custom) { + $type = sprintf('%s %s', $type, $custom); + } + return trim( sprintf('%s `%s` %s', $description['command'], $name, $type) ); diff --git a/src/Database/Migration/Migration.php b/src/Database/Migration/Migration.php index 4635f766..4cc70787 100644 --- a/src/Database/Migration/Migration.php +++ b/src/Database/Migration/Migration.php @@ -113,12 +113,21 @@ final public function create(string $table, callable $cb): Migration $engine = null; } - if ($this->adapter->getName() === 'pgsql') { - $sql = sprintf("CREATE TABLE %s (%s)%s;", $table, $generator->make(), $engine); - } else { + if ($this->adapter->getName() !== 'pgsql') { $sql = sprintf("CREATE TABLE `%s` (%s)%s;", $table, $generator->make(), $engine); + + return $this->executeSqlQuery($sql); + } + + foreach ($generator->generateCustomTypes() as $sql) { + try { + $this->executeSqlQuery($sql); + } catch (\Exception $exception) { + echo sprintf("%s %s\n", Color::yellow("Warning"), $exception->getMessage()); + } } + $sql = sprintf("CREATE TABLE %s (%s)%s;", $table, $generator->make(), $engine); return $this->executeSqlQuery($sql); } @@ -210,11 +219,11 @@ private function executeSqlQuery(string $sql): Migration try { Database::statement($sql); } catch (\Exception $exception) { - echo sprintf("%s%s\n", Color::red("▶"), $sql); + echo sprintf("%s %s\n", Color::red("▶"), $sql); throw new MigrationException($exception->getMessage(), (int) $exception->getCode()); } - echo sprintf("%s%s\n", Color::green("▶"), $sql); + echo sprintf("%s %s\n", Color::green("▶"), $sql); return $this; } } diff --git a/src/Database/Migration/Shortcut/ConstraintColumn.php b/src/Database/Migration/Shortcut/ConstraintColumn.php index 00445b45..62b93fe6 100644 --- a/src/Database/Migration/Shortcut/ConstraintColumn.php +++ b/src/Database/Migration/Shortcut/ConstraintColumn.php @@ -25,22 +25,33 @@ public function addForeign(string $name, array $attributes = []): SQLGenerator $on = ''; $references = ''; - $target = sprintf("%s_%s_foreign", $this->getTable(), $name); + + if ($this->adapter == "pgsql") { + $target = sprintf("\"%s_%s_foreign\"", $this->getTable(), $name); + } if (isset($attributes['on'])) { $on = strtoupper(' ON ' . $attributes['on']); } if (isset($attributes['references'], $attributes['table'])) { - $references = sprintf( - ' REFERENCES %s(%s)', - $attributes['table'], - $attributes['references'] - ); + if ($this->adapter === 'pgsql') { + $references = sprintf( + ' REFERENCES %s("%s")', + $attributes['table'], + $attributes['references'] + ); + } else { + $references = sprintf( + ' REFERENCES %s(`%s`)', + $attributes['table'], + $attributes['references'] + ); + } } if ($this->adapter === 'pgsql') { - $replacement = '%s %s FOREIGN KEY (%s)%s%s'; + $replacement = '%s %s FOREIGN KEY ("%s")%s%s'; } else { $replacement = '%s %s FOREIGN KEY (`%s`)%s%s'; } diff --git a/src/Database/Migration/Shortcut/MixedColumn.php b/src/Database/Migration/Shortcut/MixedColumn.php index c5884f66..9bd729fe 100644 --- a/src/Database/Migration/Shortcut/MixedColumn.php +++ b/src/Database/Migration/Shortcut/MixedColumn.php @@ -49,10 +49,6 @@ public function addUuid(string $column, array $attribute = []): SQLGenerator return $this->addColumn($column, 'varchar', $attribute); } - if (!isset($attribute['default']) && $this->adapter === 'pgsql') { - $attribute['default'] = 'uuid_generate_v4()'; - } - return $this->addColumn($column, 'uuid', $attribute); } @@ -163,10 +159,14 @@ public function addEnum(string $column, array $attribute = []): SQLGenerator throw new SQLGeneratorException("The enum values should be define!"); } - if (is_array($attribute['size'])) { + if (!is_array($attribute['size'])) { throw new SQLGeneratorException("The enum values should be array"); } + if (count($attribute['size']) === 0) { + throw new SQLGeneratorException("The enum values cannot be empty."); + } + return $this->addColumn($column, 'enum', $attribute); } @@ -228,10 +228,6 @@ public function changeUuid(string $column, array $attribute = []): SQLGenerator return $this->changeColumn($column, 'varchar', $attribute); } - if (!isset($attribute['default']) && $this->adapter === 'pgsql') { - $attribute['default'] = 'uuid_generate_v4()'; - } - return $this->changeColumn($column, 'uuid', $attribute); } From f17203da5e7116e1622aa4ee61a368080dbc5166 Mon Sep 17 00:00:00 2001 From: Franck DAKIA Date: Sat, 20 May 2023 12:58:51 +0000 Subject: [PATCH 3/4] Update pgsql migration generator unity tests --- .../Migration/Compose/PgsqlCompose.php | 10 +- src/Database/Migration/SQLGenerator.php | 2 +- .../Migration/Pgsql/SQLGeneratorTest.php | 24 ++-- .../Migration/Pgsql/SQLGenetorHelpersTest.php | 114 +++++++++--------- 4 files changed, 75 insertions(+), 75 deletions(-) diff --git a/src/Database/Migration/Compose/PgsqlCompose.php b/src/Database/Migration/Compose/PgsqlCompose.php index aa2bbb53..5b4847b1 100644 --- a/src/Database/Migration/Compose/PgsqlCompose.php +++ b/src/Database/Migration/Compose/PgsqlCompose.php @@ -87,16 +87,16 @@ private function composeAddPgsqlColumn(string $name, array $description): string } } - // Bind auto increment action - if ($increment) { - $type = 'SERIAL'; - } - // Bind precision if ($raw_type == "DOUBLE") { $type = sprintf('DOUBLE PRECISION', $type); } + // Bind auto increment action + if ($increment) { + $type = 'SERIAL'; + } + // Set column as primary key if ($primary) { $type = sprintf('%s PRIMARY KEY', $type); diff --git a/src/Database/Migration/SQLGenerator.php b/src/Database/Migration/SQLGenerator.php index f819e3e5..0bd28b04 100644 --- a/src/Database/Migration/SQLGenerator.php +++ b/src/Database/Migration/SQLGenerator.php @@ -158,7 +158,7 @@ public function renameColumn(string $name, string $new): SQLGenerator } if ($this->adapter === 'pgsql') { - $this->sqls[] = sprintf("RENAME COLUMN %s TO %s", $name, $new); + $this->sqls[] = sprintf('RENAME COLUMN "%s" TO %s', $name, $new); } else { $this->sqls[] = sprintf("RENAME COLUMN `%s` TO `%s`", $name, $new); } diff --git a/tests/Database/Migration/Pgsql/SQLGeneratorTest.php b/tests/Database/Migration/Pgsql/SQLGeneratorTest.php index 235debdd..a24ad598 100644 --- a/tests/Database/Migration/Pgsql/SQLGeneratorTest.php +++ b/tests/Database/Migration/Pgsql/SQLGeneratorTest.php @@ -26,19 +26,19 @@ public function test_add_column_sql_statement() { $this->generator->addColumn('name', 'int'); $sql = $this->generator->make(); - $this->assertEquals($sql, 'name INT NOT NULL'); + $this->assertEquals($sql, '"name" INT NOT NULL'); $this->generator->addColumn('name', 'string'); $sql = $this->generator->make(); - $this->assertNotEquals($sql, 'name STRING NOT NULL'); + $this->assertNotEquals($sql, '"name" STRING NOT NULL'); $this->generator->addColumn('name', 'string'); $sql = $this->generator->make(); - $this->assertEquals($sql, 'name VARCHAR(255) NOT NULL'); + $this->assertEquals($sql, '"name" VARCHAR(255) NOT NULL'); $this->generator->addColumn('name', 'string', ['default' => 'bow', 'size' => 100]); $sql = $this->generator->make(); - $this->assertEquals($sql, "name VARCHAR(100) NOT NULL DEFAULT 'bow'"); + $this->assertEquals($sql, '"name" VARCHAR(100) NOT NULL DEFAULT \'bow\''); } /** @@ -48,19 +48,19 @@ public function test_change_column_sql_statement() { $this->generator->changeColumn('name', 'int'); $sql = $this->generator->make(); - $this->assertEquals($sql, 'MODIFY COLUMN name INT NOT NULL'); + $this->assertEquals($sql, 'MODIFY COLUMN "name" INT NOT NULL'); $this->generator->changeColumn('name', 'string'); $sql = $this->generator->make(); - $this->assertNotEquals($sql, 'MODIFY COLUMN name STRING NOT NULL'); + $this->assertNotEquals($sql, 'MODIFY COLUMN "name" STRING NOT NULL'); $this->generator->changeColumn('name', 'string'); $sql = $this->generator->make(); - $this->assertEquals($sql, 'MODIFY COLUMN name VARCHAR(255) NOT NULL'); + $this->assertEquals($sql, 'MODIFY COLUMN "name" VARCHAR(255) NOT NULL'); $this->generator->changeColumn('name', 'string', ['default' => 'bow', 'size' => 100]); $sql = $this->generator->make(); - $this->assertEquals($sql, "MODIFY COLUMN name VARCHAR(100) NOT NULL DEFAULT 'bow'"); + $this->assertEquals($sql, 'MODIFY COLUMN "name" VARCHAR(100) NOT NULL DEFAULT \'bow\''); } /** @@ -70,7 +70,7 @@ public function test_rename_column_sql_statement_for_mysql() { $this->generator->renameColumn('name', 'fullname'); $sql = $this->generator->make(); - $this->assertEquals($sql, 'RENAME COLUMN name TO fullname'); + $this->assertEquals($sql, 'RENAME COLUMN "name" TO fullname'); } /** @@ -128,7 +128,7 @@ public function test_should_create_correct_datetime_sql_statement() $this->generator->addDatetime('created_at', ['default' => 'CURRENT_TIMESTAMP']); $sql = $this->generator->make(); - $this->assertEquals($sql, 'created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP'); + $this->assertEquals($sql, '"created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP'); } public function test_should_create_not_correct_datetime_sql_statement() @@ -136,7 +136,7 @@ public function test_should_create_not_correct_datetime_sql_statement() $this->generator->addDatetime('created_at'); $sql = $this->generator->make(); - $this->assertNotEquals($sql, 'created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP'); + $this->assertNotEquals($sql, '"created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP'); } public function test_should_create_correct_timestamps_sql_statement() @@ -144,6 +144,6 @@ public function test_should_create_correct_timestamps_sql_statement() $this->generator->addTimestamps(); $sql = $this->generator->make(); - $this->assertEquals($sql, 'created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP'); + $this->assertEquals($sql, '"created_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, "updated_at" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP'); } } diff --git a/tests/Database/Migration/Pgsql/SQLGenetorHelpersTest.php b/tests/Database/Migration/Pgsql/SQLGenetorHelpersTest.php index 6334c144..67cd22e9 100644 --- a/tests/Database/Migration/Pgsql/SQLGenetorHelpersTest.php +++ b/tests/Database/Migration/Pgsql/SQLGenetorHelpersTest.php @@ -27,11 +27,11 @@ public function test_add_string_sql_statement(string $type, string $method, int| $type = strtoupper($type); $sql = $this->generator->{"add$method"}('name')->make(); - $this->assertNotEquals($sql, 'name STRING NOT NULL'); + $this->assertNotEquals($sql, '"name" STRING NOT NULL'); if (preg_match('/STRING|VARCHAR/', $type)) { - $this->assertEquals($sql, "name {$type}(255) NOT NULL"); + $this->assertEquals($sql, "\"name\" {$type}(255) NOT NULL"); } else { - $this->assertEquals($sql, "name {$type} NOT NULL"); + $this->assertEquals($sql, "\"name\" {$type} NOT NULL"); } if (preg_match('/TEXT/', $type)) { @@ -40,20 +40,20 @@ public function test_add_string_sql_statement(string $type, string $method, int| } $sql = $this->generator->{"add$method"}('name', ['default' => $default, 'size' => 100])->make(); if (preg_match('/STRING|VARCHAR/', $type)) { - $this->assertEquals($sql, "name {$type}(100) NOT NULL DEFAULT '$default'"); + $this->assertEquals($sql, "\"name\" {$type}(100) NOT NULL DEFAULT '$default'"); } $sql = $this->generator->{"add$method"}('name', ['default' => $default, 'size' => 100, 'nullable' => true])->make(); - $this->assertEquals($sql, "name {$type}(100) NULL DEFAULT '$default'"); + $this->assertEquals($sql, "\"name\" {$type}(100) NULL DEFAULT '$default'"); $sql = $this->generator->{"add$method"}('name', ['primary' => true])->make(); - $this->assertEquals($sql, "name {$type}(255) PRIMARY KEY NOT NULL"); + $this->assertEquals($sql, "\"name\" {$type}(255) PRIMARY KEY NOT NULL"); $sql = $this->generator->{"add$method"}('name', ['primary' => true, 'default' => $default, 'size' => 100, 'nullable' => true])->make(); - $this->assertEquals($sql, "name {$type}(100) PRIMARY KEY NULL DEFAULT '$default'"); + $this->assertEquals($sql, "\"name\" {$type}(100) PRIMARY KEY NULL DEFAULT '$default'"); $sql = $this->generator->{"add$method"}('name', ['unique' => true])->make(); - $this->assertEquals($sql, "name {$type}(255) UNIQUE NOT NULL"); + $this->assertEquals($sql, "\"name\" {$type}(255) UNIQUE NOT NULL"); } /** @@ -65,9 +65,9 @@ public function test_change_string_sql_statement(string $type, string $method, i $sql = $this->generator->{"change$method"}('name')->make(); if (preg_match('/STRING|VARCHAR/', $type)) { - $this->assertEquals($sql, "MODIFY COLUMN name {$type}(255) NOT NULL"); + $this->assertEquals($sql, "MODIFY COLUMN \"name\" {$type}(255) NOT NULL"); } else { - $this->assertEquals($sql, "MODIFY COLUMN name {$type} NOT NULL"); + $this->assertEquals($sql, "MODIFY COLUMN \"name\" {$type} NOT NULL"); } if (preg_match('/TEXT/', $type)) { @@ -76,20 +76,20 @@ public function test_change_string_sql_statement(string $type, string $method, i } $sql = $this->generator->{"change$method"}('name', ['default' => $default, 'size' => 100])->make(); if (preg_match('/STRING|VARCHAR/', $type)) { - $this->assertEquals($sql, "MODIFY COLUMN name {$type}(100) NOT NULL DEFAULT '$default'"); + $this->assertEquals($sql, "MODIFY COLUMN \"name\" {$type}(100) NOT NULL DEFAULT '$default'"); } $sql = $this->generator->{"change$method"}('name', ['default' => $default, 'size' => 100, 'nullable' => true])->make(); - $this->assertEquals($sql, "MODIFY COLUMN name {$type}(100) NULL DEFAULT '$default'"); + $this->assertEquals($sql, "MODIFY COLUMN \"name\" {$type}(100) NULL DEFAULT '$default'"); $sql = $this->generator->{"change$method"}('name', ['primary' => true])->make(); - $this->assertEquals($sql, "MODIFY COLUMN name {$type}(255) PRIMARY KEY NOT NULL"); + $this->assertEquals($sql, "MODIFY COLUMN \"name\" {$type}(255) PRIMARY KEY NOT NULL"); $sql = $this->generator->{"change$method"}('name', ['primary' => true, 'default' => $default, 'size' => 100, 'nullable' => true])->make(); - $this->assertEquals($sql, "MODIFY COLUMN name {$type}(100) PRIMARY KEY NULL DEFAULT '$default'"); + $this->assertEquals($sql, "MODIFY COLUMN \"name\" {$type}(100) PRIMARY KEY NULL DEFAULT '$default'"); $sql = $this->generator->{"change$method"}('name', ['unique' => true])->make(); - $this->assertEquals($sql, "MODIFY COLUMN name {$type}(255) UNIQUE NOT NULL"); + $this->assertEquals($sql, "MODIFY COLUMN \"name\" {$type}(255) UNIQUE NOT NULL"); } /** @@ -101,41 +101,41 @@ public function test_add_string_without_size_sql_statement(string $type, string $sql = $this->generator->{"add$method"}('name')->make(); $this->assertNotEquals($sql, 'name STRING NOT NULL'); - $this->assertEquals($sql, "name {$type} NOT NULL"); + $this->assertEquals($sql, "\"name\" {$type} NOT NULL"); $sql = $this->generator->{"add$method"}('name', ['default' => $default])->make(); if ($type === "CHAR") { - $this->assertEquals($sql, "name {$type} NOT NULL DEFAULT '$default'"); + $this->assertEquals($sql, "\"name\" {$type} NOT NULL DEFAULT '$default'"); } else { - $this->assertEquals($sql, "name {$type} NOT NULL DEFAULT $default"); + $this->assertEquals($sql, "\"name\" {$type} NOT NULL DEFAULT $default"); } $sql = $this->generator->{"add$method"}('name', ['default' => $default, 'size' => 100])->make(); if ($type === "CHAR") { - $this->assertEquals($sql, "name {$type} NOT NULL DEFAULT '$default'"); + $this->assertEquals($sql, "\"name\" {$type} NOT NULL DEFAULT '$default'"); } else { - $this->assertEquals($sql, "name {$type} NOT NULL DEFAULT $default"); + $this->assertEquals($sql, "\"name\" {$type} NOT NULL DEFAULT $default"); } $sql = $this->generator->{"add$method"}('name', ['default' => $default, 'nullable' => true])->make(); if ($type === "CHAR") { - $this->assertEquals($sql, "name {$type} NULL DEFAULT '$default'"); + $this->assertEquals($sql, "\"name\" {$type} NULL DEFAULT '$default'"); } else { - $this->assertEquals($sql, "name {$type} NULL DEFAULT $default"); + $this->assertEquals($sql, "\"name\" {$type} NULL DEFAULT $default"); } $sql = $this->generator->{"add$method"}('name', ['primary' => true])->make(); - $this->assertEquals($sql, "name {$type} PRIMARY KEY NOT NULL"); + $this->assertEquals($sql, "\"name\" {$type} PRIMARY KEY NOT NULL"); $sql = $this->generator->{"add$method"}('name', ['primary' => true, 'default' => $default, 'nullable' => true])->make(); if ($type === "CHAR") { - $this->assertEquals($sql, "name {$type} PRIMARY KEY NULL DEFAULT '$default'"); + $this->assertEquals($sql, "\"name\" {$type} PRIMARY KEY NULL DEFAULT '$default'"); } else { - $this->assertEquals($sql, "name {$type} PRIMARY KEY NULL DEFAULT $default"); + $this->assertEquals($sql, "\"name\" {$type} PRIMARY KEY NULL DEFAULT $default"); } $sql = $this->generator->{"add$method"}('name', ['unique' => true])->make(); - $this->assertEquals($sql, "name {$type} UNIQUE NOT NULL"); + $this->assertEquals($sql, "\"name\" {$type} UNIQUE NOT NULL"); } /** @@ -146,41 +146,41 @@ public function test_change_string_without_size_sql_statement(string $type, stri $type = strtoupper($type); $sql = $this->generator->{"change$method"}('name')->make(); - $this->assertEquals($sql, "MODIFY COLUMN name {$type} NOT NULL"); + $this->assertEquals($sql, "MODIFY COLUMN \"name\" {$type} NOT NULL"); $sql = $this->generator->{"change$method"}('name', ['default' => $default, 'size' => 100])->make(); if ($type === 'CHAR') { - $this->assertEquals($sql, "MODIFY COLUMN name {$type} NOT NULL DEFAULT '$default'"); + $this->assertEquals($sql, "MODIFY COLUMN \"name\" {$type} NOT NULL DEFAULT '$default'"); } else { - $this->assertEquals($sql, "MODIFY COLUMN name {$type} NOT NULL DEFAULT $default"); + $this->assertEquals($sql, "MODIFY COLUMN \"name\" {$type} NOT NULL DEFAULT $default"); } $sql = $this->generator->{"change$method"}('name', ['default' => $default])->make(); if ($type === 'CHAR') { - $this->assertEquals($sql, "MODIFY COLUMN name {$type} NOT NULL DEFAULT '$default'"); + $this->assertEquals($sql, "MODIFY COLUMN \"name\" {$type} NOT NULL DEFAULT '$default'"); } else { - $this->assertEquals($sql, "MODIFY COLUMN name {$type} NOT NULL DEFAULT $default"); + $this->assertEquals($sql, "MODIFY COLUMN \"name\" {$type} NOT NULL DEFAULT $default"); } $sql = $this->generator->{"change$method"}('name', ['default' => $default, 'nullable' => true])->make(); if ($type === 'CHAR') { - $this->assertEquals($sql, "MODIFY COLUMN name {$type} NULL DEFAULT '$default'"); + $this->assertEquals($sql, "MODIFY COLUMN \"name\" {$type} NULL DEFAULT '$default'"); } else { - $this->assertEquals($sql, "MODIFY COLUMN name {$type} NULL DEFAULT $default"); + $this->assertEquals($sql, "MODIFY COLUMN \"name\" {$type} NULL DEFAULT $default"); } $sql = $this->generator->{"change$method"}('name', ['primary' => true])->make(); - $this->assertEquals($sql, "MODIFY COLUMN name {$type} PRIMARY KEY NOT NULL"); + $this->assertEquals($sql, "MODIFY COLUMN \"name\" {$type} PRIMARY KEY NOT NULL"); $sql = $this->generator->{"change$method"}('name', ['primary' => true, 'default' => $default, 'nullable' => true])->make(); if ($type === 'CHAR') { - $this->assertEquals($sql, "MODIFY COLUMN name {$type} PRIMARY KEY NULL DEFAULT '$default'"); + $this->assertEquals($sql, "MODIFY COLUMN \"name\" {$type} PRIMARY KEY NULL DEFAULT '$default'"); } else { - $this->assertEquals($sql, "MODIFY COLUMN name {$type} PRIMARY KEY NULL DEFAULT $default"); + $this->assertEquals($sql, "MODIFY COLUMN \"name\" {$type} PRIMARY KEY NULL DEFAULT $default"); } $sql = $this->generator->{"change$method"}('name', ['unique' => true])->make(); - $this->assertEquals($sql, "MODIFY COLUMN name {$type} UNIQUE NOT NULL"); + $this->assertEquals($sql, "MODIFY COLUMN \"name\" {$type} UNIQUE NOT NULL"); } /** @@ -191,30 +191,30 @@ public function test_add_int_sql_statement(string $type, string $method, int|str $type = strtoupper($type); $sql = $this->generator->{"add$method"}('column')->make(); - $this->assertEquals($sql, "column {$type} NOT NULL"); + $this->assertEquals($sql, "\"column\" {$type} NOT NULL"); $sql = $this->generator->{"add$method"}('column', ['default' => $default, 'size' => 100])->make(); - $this->assertEquals($sql, "column {$type} NOT NULL DEFAULT $default"); + $this->assertEquals($sql, "\"column\" {$type} NOT NULL DEFAULT $default"); $sql = $this->generator->{"add$method"}('column', ['default' => $default, 'size' => 100, 'nullable' => true])->make(); - $this->assertEquals($sql, "column {$type} NULL DEFAULT $default"); + $this->assertEquals($sql, "\"column\" {$type} NULL DEFAULT $default"); $sql = $this->generator->{"add$method"}('column', ['primary' => true])->make(); - $this->assertEquals($sql, "column {$type} PRIMARY KEY NOT NULL"); + $this->assertEquals($sql, "\"column\" {$type} PRIMARY KEY NOT NULL"); $sql = $this->generator->{"add$method"}('column', ['primary' => true, 'default' => $default, 'size' => 100, 'nullable' => true])->make(); - $this->assertEquals($sql, "column {$type} PRIMARY KEY NULL DEFAULT $default"); + $this->assertEquals($sql, "\"column\" {$type} PRIMARY KEY NULL DEFAULT $default"); $sql = $this->generator->{"add$method"}('column', ['primary' => true, 'increment' => true, 'size' => 100, 'nullable' => true])->make(); - $this->assertEquals($sql, "column SERIAL PRIMARY KEY NULL"); + $this->assertEquals($sql, "\"column\" SERIAL PRIMARY KEY NULL"); $sql = $this->generator->{"add$method"}('column', ['unique' => true])->make(); - $this->assertEquals($sql, "column {$type} UNIQUE NOT NULL"); + $this->assertEquals($sql, "\"column\" {$type} UNIQUE NOT NULL"); $method = "add{$method}Increment"; if (method_exists($this->generator, $method)) { $sql = $this->generator->{$method}('column')->make(); - $this->assertEquals($sql, "column SERIAL PRIMARY KEY NOT NULL"); + $this->assertEquals($sql, "\"column\" SERIAL PRIMARY KEY NOT NULL"); } } @@ -226,40 +226,40 @@ public function test_change_int_sql_statement(string $type, string $method, int| $type = strtoupper($type); $sql = $this->generator->{"change$method"}('column')->make(); - $this->assertEquals($sql, "MODIFY COLUMN column {$type} NOT NULL"); + $this->assertEquals($sql, "MODIFY COLUMN \"column\" {$type} NOT NULL"); $sql = $this->generator->{"change$method"}('column', ['default' => $default, 'size' => 100])->make(); - $this->assertEquals($sql, "MODIFY COLUMN column {$type} NOT NULL DEFAULT $default"); + $this->assertEquals($sql, "MODIFY COLUMN \"column\" {$type} NOT NULL DEFAULT $default"); $sql = $this->generator->{"change$method"}('column', ['default' => $default, 'size' => 100, 'nullable' => true])->make(); - $this->assertEquals($sql, "MODIFY COLUMN column {$type} NULL DEFAULT $default"); + $this->assertEquals($sql, "MODIFY COLUMN \"column\" {$type} NULL DEFAULT $default"); $sql = $this->generator->{"change$method"}('column', ['primary' => true])->make(); - $this->assertEquals($sql, "MODIFY COLUMN column {$type} PRIMARY KEY NOT NULL"); + $this->assertEquals($sql, "MODIFY COLUMN \"column\" {$type} PRIMARY KEY NOT NULL"); $sql = $this->generator->{"change$method"}('column', ['primary' => true, 'default' => $default, 'size' => 100, 'nullable' => true])->make(); - $this->assertEquals($sql, "MODIFY COLUMN column {$type} PRIMARY KEY NULL DEFAULT $default"); + $this->assertEquals($sql, "MODIFY COLUMN \"column\" {$type} PRIMARY KEY NULL DEFAULT $default"); $sql = $this->generator->{"change$method"}('column', ['primary' => true, 'increment' => true, 'size' => 100, 'nullable' => true])->make(); - $this->assertEquals($sql, "MODIFY COLUMN column SERIAL PRIMARY KEY NULL"); + $this->assertEquals($sql, "MODIFY COLUMN \"column\" SERIAL PRIMARY KEY NULL"); $sql = $this->generator->{"change$method"}('column', ['unique' => true])->make(); - $this->assertEquals($sql, "MODIFY COLUMN column {$type} UNIQUE NOT NULL"); + $this->assertEquals($sql, "MODIFY COLUMN \"column\" {$type} UNIQUE NOT NULL"); $method = "change{$method}Increment"; if (method_exists($this->generator, $method)) { $sql = $this->generator->{$method}('column')->make(); - $this->assertEquals($sql, "MODIFY COLUMN column {$type} SERIAL PRIMARY KEY NOT NULL"); + $this->assertEquals($sql, "MODIFY COLUMN \"column\" {$type} SERIAL PRIMARY KEY NOT NULL"); } } public function test_uuid_statement() { $sql = $this->generator->addUuid('column', ['unique' => true])->make(); - $this->assertEquals($sql, "column UUID UNIQUE NOT NULL DEFAULT uuid_generate_v4()"); + $this->assertEquals($sql, "\"column\" UUID UNIQUE NOT NULL"); $sql = $this->generator->addUuid('column', ['primary' => true])->make(); - $this->assertEquals($sql, "column UUID PRIMARY KEY NOT NULL DEFAULT uuid_generate_v4()"); + $this->assertEquals($sql, "\"column\" UUID PRIMARY KEY NOT NULL"); $this->expectException(SQLGeneratorException::class); $this->expectExceptionMessage("Cannot define the increment for uuid. You can use addUuidPrimary() instead"); @@ -269,7 +269,7 @@ public function test_uuid_statement() public function test_uuid_primary_statement() { $sql = $this->generator->addUuidPrimary('column')->make(); - $this->assertEquals($sql, "column UUID PRIMARY KEY NOT NULL DEFAULT uuid_generate_v4()"); + $this->assertEquals($sql, "\"column\" UUID PRIMARY KEY NOT NULL DEFAULT uuid_generate_v4()"); } public function test_uuid_should_throw_errors_with_increment_attribute() @@ -286,7 +286,7 @@ public function getNumberTypes() ["bigint", "BigInteger", 1], ["tinyint", "TinyInteger", 1], ["float", "Float", 1], - ["double", "Double", 1], + ["double precision", "Double", 1], ["smallint", "SmallInteger", 1], ["mediumint", "MediumInteger", 1], ]; From 812f4496713bec19d5915b73580cf337971e9ed8 Mon Sep 17 00:00:00 2001 From: Franck DAKIA Date: Sat, 20 May 2023 13:04:42 +0000 Subject: [PATCH 4/4] Fixes undefined target variable --- src/Database/Migration/Shortcut/ConstraintColumn.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Database/Migration/Shortcut/ConstraintColumn.php b/src/Database/Migration/Shortcut/ConstraintColumn.php index 62b93fe6..b4d10121 100644 --- a/src/Database/Migration/Shortcut/ConstraintColumn.php +++ b/src/Database/Migration/Shortcut/ConstraintColumn.php @@ -28,6 +28,8 @@ public function addForeign(string $name, array $attributes = []): SQLGenerator if ($this->adapter == "pgsql") { $target = sprintf("\"%s_%s_foreign\"", $this->getTable(), $name); + } else { + $target = sprintf("%s_%s_foreign", $this->getTable(), $name); } if (isset($attributes['on'])) {