From 76483508fb52e42561319feb8a0d4aae999474eb Mon Sep 17 00:00:00 2001 From: Alireza Date: Tue, 28 Mar 2023 00:09:09 +0430 Subject: [PATCH] v1.3.0 --- composer.json | 6 ++- src/Command/Command.php | 52 +++++++++++++++++++ src/Command/CommandGenerator.php | 14 ----- src/Command/CommandInterface.php | 5 +- src/Command/Commands/CreateTableCommand.php | 20 ++++--- .../Commands/DropIfExistsTableCommand.php | 18 +++++-- src/Command/Commands/DropTableCommand.php | 18 +++++-- src/Enums/Command.php | 10 ++++ src/Migration.php | 23 ++++---- .../ColumnTranslateManager.php} | 6 +-- .../ColumnTranslator.php} | 8 +-- .../ColumnTranslatorInterface.php} | 4 +- .../Translators/MySqlTranslator.php | 6 +-- .../Translators/PostgreSqlTranslator.php | 6 +-- .../CommandTranslatorInterface.php | 9 ++++ .../CreateTableCommandTranslator.php | 27 ++++++++++ .../DropIfExistsTableCommandTranslator.php | 25 +++++++++ .../DropTableCommandTranslator.php | 26 ++++++++++ test/Structure/StructureBuilderTest.php | 22 ++++---- .../CommandTranslatorTest.php | 50 ++++++++++++++++++ 20 files changed, 287 insertions(+), 68 deletions(-) create mode 100644 src/Command/Command.php delete mode 100644 src/Command/CommandGenerator.php create mode 100644 src/Enums/Command.php rename src/Translation/{TranslateManager.php => ColumnTranslator/ColumnTranslateManager.php} (80%) rename src/Translation/{Translator.php => ColumnTranslator/ColumnTranslator.php} (63%) rename src/Translation/{TranslatorInterface.php => ColumnTranslator/ColumnTranslatorInterface.php} (77%) rename src/Translation/{ => ColumnTranslator}/Translators/MySqlTranslator.php (89%) rename src/Translation/{ => ColumnTranslator}/Translators/PostgreSqlTranslator.php (89%) create mode 100644 src/Translation/CommandTranslator/CommandTranslatorInterface.php create mode 100644 src/Translation/CommandTranslator/Translators/CreateTableCommandTranslator.php create mode 100644 src/Translation/CommandTranslator/Translators/DropIfExistsTableCommandTranslator.php create mode 100644 src/Translation/CommandTranslator/Translators/DropTableCommandTranslator.php create mode 100644 test/Translator/CommandTranslator/CommandTranslatorTest.php diff --git a/composer.json b/composer.json index 35a7a89..07cb966 100644 --- a/composer.json +++ b/composer.json @@ -4,7 +4,11 @@ "license": "MIT", "autoload": { "psr-4": { - "Alirezasalehizadeh\\QuickMigration\\": "src/", + "Alirezasalehizadeh\\QuickMigration\\": "src/" + } + }, + "autoload-dev": { + "psr-4": { "Alirezasalehizadeh\\QuickMigration\\Test\\": "test/" } }, diff --git a/src/Command/Command.php b/src/Command/Command.php new file mode 100644 index 0000000..9cfc136 --- /dev/null +++ b/src/Command/Command.php @@ -0,0 +1,52 @@ +name->value; + } + + public function setName(EnumsCommand $name) + { + $this->name = $name; + + return $this; + } + + public function getPattern() + { + return $this->pattern; + } + + public function setPattern(string $pattern) + { + $this->pattern = $pattern; + + return $this; + } + + public function getIncludes() + { + return $this->includes; + } + + public function setIncludes(array $includes) + { + $this->includes = $includes; + + return $this; + } +} diff --git a/src/Command/CommandGenerator.php b/src/Command/CommandGenerator.php deleted file mode 100644 index 470987b..0000000 --- a/src/Command/CommandGenerator.php +++ /dev/null @@ -1,14 +0,0 @@ -sqlCommands = $sqlCommands; } - public function generate(): string + public function getCommand() : self { - $sqlString = implode(" ,", $this->sqlCommands); - return sprintf($this->pattern, $this->database, $this->table, $sqlString); + return $this + ->setName(EnumsCommand::Create) + ->setPattern($this->pattern) + ->setIncludes([ + 'database' => $this->database, + 'table' => $this->table, + 'sqlCommands' => $this->sqlCommands, + ]); } } diff --git a/src/Command/Commands/DropIfExistsTableCommand.php b/src/Command/Commands/DropIfExistsTableCommand.php index 4f39571..42272fc 100644 --- a/src/Command/Commands/DropIfExistsTableCommand.php +++ b/src/Command/Commands/DropIfExistsTableCommand.php @@ -1,14 +1,16 @@ table = $table; } - public function generate(): string + public function getCommand() : self { - return sprintf($this->pattern, $this->database, $this->table); + return $this + ->setName(EnumsCommand::DropTableIfExists) + ->setPattern($this->pattern) + ->setIncludes([ + 'database' => $this->database, + 'table' => $this->table, + ]); } } diff --git a/src/Command/Commands/DropTableCommand.php b/src/Command/Commands/DropTableCommand.php index b42f6e9..35febfc 100644 --- a/src/Command/Commands/DropTableCommand.php +++ b/src/Command/Commands/DropTableCommand.php @@ -1,14 +1,16 @@ table = $table; } - public function generate(): string + public function getCommand() :self { - return sprintf($this->pattern, $this->database, $this->table); + return $this + ->setName(EnumsCommand::Drop) + ->setPattern($this->pattern) + ->setIncludes([ + 'database' => $this->database, + 'table' => $this->table, + ]); } } diff --git a/src/Enums/Command.php b/src/Enums/Command.php new file mode 100644 index 0000000..53225b3 --- /dev/null +++ b/src/Enums/Command.php @@ -0,0 +1,10 @@ +database, $table))->generate(); + $sql = (new DropTableCommandTranslator((new DropTableCommand($this->database, $table))->getCommand()))->make(); $this->run($sql); } public function dropIfExists(string $table) { - $sql = (new DropIfExistsTableCommand($this->database, $table))->generate(); + $sql = (new DropIfExistsTableCommandTranslator((new DropIfExistsTableCommand($this->database, $table))->getCommand()))->make(); $this->run($sql); } @@ -45,10 +48,10 @@ public function migrate() $attribute = $data[1]; // Translate column objects to sql string - $commands = (new TranslateManager($this->translator))->translate($columns); - - // Generate `CREATE TABLE` sql command by `CommandGenerator` class - $sql = (new CreateTableCommand($this->database, $attribute['table'], $commands))->generate(); + $commands = (new ColumnTranslateManager($this->translator))->translate($columns); + + // Make `CREATE TABLE` sql command by `CreateTableCommand` object + $sql = (new CreateTableCommandTranslator((new CreateTableCommand($this->database, $attribute['table'], $commands))->getCommand()))->make(); // Run the sql string by PDO connection $this->run($sql); diff --git a/src/Translation/TranslateManager.php b/src/Translation/ColumnTranslator/ColumnTranslateManager.php similarity index 80% rename from src/Translation/TranslateManager.php rename to src/Translation/ColumnTranslator/ColumnTranslateManager.php index 8c3195e..18b2621 100644 --- a/src/Translation/TranslateManager.php +++ b/src/Translation/ColumnTranslator/ColumnTranslateManager.php @@ -1,8 +1,8 @@ translator = in_array($translator, $translatorObj->availableTranslators) ? new $translatorObj->availableTranslators[$translator] diff --git a/src/Translation/Translator.php b/src/Translation/ColumnTranslator/ColumnTranslator.php similarity index 63% rename from src/Translation/Translator.php rename to src/Translation/ColumnTranslator/ColumnTranslator.php index ca80235..b70ea37 100644 --- a/src/Translation/Translator.php +++ b/src/Translation/ColumnTranslator/ColumnTranslator.php @@ -1,11 +1,11 @@ command = $command; + } + + public function make():string + { + return sprintf( + $this->command->getPattern(), + $this->command->getName(), + $this->command->getIncludes()['database'], + $this->command->getIncludes()['table'], + implode(',', $this->command->getIncludes()['sqlCommands']), + ); + } + +} diff --git a/src/Translation/CommandTranslator/Translators/DropIfExistsTableCommandTranslator.php b/src/Translation/CommandTranslator/Translators/DropIfExistsTableCommandTranslator.php new file mode 100644 index 0000000..e2d1486 --- /dev/null +++ b/src/Translation/CommandTranslator/Translators/DropIfExistsTableCommandTranslator.php @@ -0,0 +1,25 @@ +command = $command; + } + + public function make(): string + { + return sprintf( + $this->command->getPattern(), + $this->command->getName(), + $this->command->getIncludes()['database'], + $this->command->getIncludes()['table'], + ); + } +} diff --git a/src/Translation/CommandTranslator/Translators/DropTableCommandTranslator.php b/src/Translation/CommandTranslator/Translators/DropTableCommandTranslator.php new file mode 100644 index 0000000..41f6f66 --- /dev/null +++ b/src/Translation/CommandTranslator/Translators/DropTableCommandTranslator.php @@ -0,0 +1,26 @@ +command = $command; + } + + public function make():string + { + return sprintf( + $this->command->getPattern(), + $this->command->getName(), + $this->command->getIncludes()['database'], + $this->command->getIncludes()['table'], + ); + } +} diff --git a/test/Structure/StructureBuilderTest.php b/test/Structure/StructureBuilderTest.php index 1ab24fb..0cb9f67 100644 --- a/test/Structure/StructureBuilderTest.php +++ b/test/Structure/StructureBuilderTest.php @@ -4,7 +4,7 @@ use Alirezasalehizadeh\QuickMigration\Enums\Attribute; use Alirezasalehizadeh\QuickMigration\Enums\Type; use Alirezasalehizadeh\QuickMigration\Structure\Structure; -use Alirezasalehizadeh\QuickMigration\Translation\TranslateManager; +use Alirezasalehizadeh\QuickMigration\Translation\ColumnTranslator\ColumnTranslateManager; use PHPUnit\Framework\TestCase; class StructureBuilderTest extends TestCase @@ -17,7 +17,7 @@ public function createNullableColumnTest() $column = $structure->string('foo', 100)->nullable(); - $sql = (new TranslateManager)->translate([$column]); + $sql = (new ColumnTranslateManager())->translate([$column]); $this->assertSame("`foo` VARCHAR(100) NULL", $sql[0]); } @@ -29,7 +29,7 @@ public function createColumnWithAutoIncrementTest() $column = $structure->number('foo', Type::Int)->autoIncrement(true); - $sql = (new TranslateManager)->translate([$column]); + $sql = (new ColumnTranslateManager())->translate([$column]); $this->assertSame("`foo` INT NOT NULL AUTO_INCREMENT", $sql[0]); } @@ -41,7 +41,7 @@ public function createColumnWithDefaultValueTest() $column = $structure->number('foo', Type::Tinyint)->default(1); - $sql = (new TranslateManager)->translate([$column]); + $sql = (new ColumnTranslateManager())->translate([$column]); $this->assertSame("`foo` TINYINT NOT NULL DEFAULT 1", $sql[0]); } @@ -53,7 +53,7 @@ public function createColumnWithUnsignedAttributeTest() $column = $structure->number('foo', Type::Bigint)->attribute(Attribute::Unsigned); - $sql = (new TranslateManager)->translate([$column]); + $sql = (new ColumnTranslateManager())->translate([$column]); $this->assertSame("`foo` BIGINT UNSIGNED NOT NULL", $sql[0]); } @@ -65,7 +65,7 @@ public function createNotNullUniqueVarcharTypeColumnTest() $column = $structure->string('foo', 100)->unique(); - $sql = (new TranslateManager)->translate([$column]); + $sql = (new ColumnTranslateManager())->translate([$column]); $this->assertSame("`foo` VARCHAR(100) NOT NULL UNIQUE", $sql[0]); } @@ -77,7 +77,7 @@ public function createTimestampTypeColumnTest() $column = $structure->timestamp('foo'); - $sql = (new TranslateManager)->translate([$column]); + $sql = (new ColumnTranslateManager())->translate([$column]); $this->assertSame("`foo` TIMESTAMP NOT NULL", $sql[0]); } @@ -89,7 +89,7 @@ public function createJsonTypeColumnTest() $column = $structure->json('foo'); - $sql = (new TranslateManager)->translate([$column]); + $sql = (new ColumnTranslateManager())->translate([$column]); $this->assertSame("`foo` JSON NOT NULL", $sql[0]); } @@ -101,7 +101,7 @@ public function createPrimaryColumnTest() $column = $structure->number('foo', Type::Int)->primary(); - $sql = (new TranslateManager)->translate([$column]); + $sql = (new ColumnTranslateManager())->translate([$column]); $this->assertSame("`foo` INT NOT NULL PRIMARY KEY", $sql[0]); } @@ -113,7 +113,7 @@ public function createNullableColumnHaveNullDefaultValueTest() $column = $structure->number('foo', Type::Int)->nullable()->default(1); - $sql = (new TranslateManager)->translate([$column]); + $sql = (new ColumnTranslateManager())->translate([$column]); $this->assertSame("`foo` INT NULL DEFAULT NULL", $sql[0]); } @@ -128,7 +128,7 @@ public function createNullableIntTypeColumnAndUniqueStringTypeColumnTest() $columns = $structure->done(); - $sql = (new TranslateManager)->translate($columns[0]); + $sql = (new ColumnTranslateManager())->translate($columns[0]); $this->assertSame("`foo` INT NULL , `bar` VARCHAR(100) NOT NULL UNIQUE", "{$sql[0]} , {$sql[1]}"); } diff --git a/test/Translator/CommandTranslator/CommandTranslatorTest.php b/test/Translator/CommandTranslator/CommandTranslatorTest.php new file mode 100644 index 0000000..32abc00 --- /dev/null +++ b/test/Translator/CommandTranslator/CommandTranslatorTest.php @@ -0,0 +1,50 @@ +database, $this->table, [ + '`id` INT NOT NULL' + ]))->getCommand(); + + $sql = (new CreateTableCommandTranslator($command))->make(); + + $this->assertSame("CREATE TABLE `foo`.`bar` (`id` INT NOT NULL)", $sql); + } + + /** @test */ + public function canMakeDropTableCommandTest() + { + $command = (new DropTableCommand($this->database, $this->table))->getCommand(); + + $sql = (new DropTableCommandTranslator($command))->make(); + + $this->assertSame("DROP TABLE `foo`.`bar`", $sql); + } + + /** @test */ + public function canMakeDropIfExistsTableCommandTest() + { + $command = (new DropIfExistsTableCommand($this->database, $this->table))->getCommand(); + + $sql = (new DropIfExistsTableCommandTranslator($command))->make(); + + $this->assertSame("DROP TABLE IF EXISTS `foo`.`bar`", $sql); + } +}