diff --git a/src/System/Database/MySchema/Table/Alter.php b/src/System/Database/MySchema/Table/Alter.php index 0474ca34..0fc00479 100644 --- a/src/System/Database/MySchema/Table/Alter.php +++ b/src/System/Database/MySchema/Table/Alter.php @@ -63,22 +63,11 @@ protected function builder(): string { $query = []; - // alter - $query = array_merge($query, $this->getModify()); + // merge alter, add, drop, rename + $query = array_merge($query, $this->getModify(), $this->getColumns(), $this->getDrops(), $this->getRename()); + $query = implode(', ', $query); - // add - $query = array_merge($query, $this->getColumns()); - - // drop - $drops = array_map(fn ($column) => "DROP COLUMN `{$column}`;", $this->drop_columns); - $query = array_merge($query, $drops); - - // rename - $query = array_merge($query, $this->getRename()); - - $query = implode(' ', $query); - - return "ALTER TABLE {$this->table_name} {$query}"; + return "ALTER TABLE {$this->table_name} {$query};"; } /** @return string[] */ @@ -87,7 +76,7 @@ private function getModify(): array $res = []; foreach ($this->alter_columns as $attribute) { - $res[] = "MODIFY COLUMN {$attribute->__toString()};"; + $res[] = "MODIFY COLUMN {$attribute->__toString()}"; } return $res; @@ -99,7 +88,7 @@ private function getRename(): array $res = []; foreach ($this->rename_columns as $old => $new) { - $res[] = "; RENAME COLUMN `{$old}` TO `{$new}`;"; + $res[] = "RENAME COLUMN `{$old}` TO `{$new}`"; } return $res; @@ -111,7 +100,19 @@ private function getColumns(): array $res = []; foreach ($this->add_columns as $attribute) { - $res[] = "ADD {$attribute->__toString()};"; + $res[] = "ADD {$attribute->__toString()}"; + } + + return $res; + } + + /** @return string[] */ + private function getDrops(): array + { + $res = []; + + foreach ($this->drop_columns as $drop) { + $res[] = "DROP COLUMN `{$drop}`"; } return $res; diff --git a/tests/DataBase/MySchemaTest.php b/tests/DataBase/MySchemaTest.php index 483a45d9..a855fec3 100644 --- a/tests/DataBase/MySchemaTest.php +++ b/tests/DataBase/MySchemaTest.php @@ -35,7 +35,7 @@ public function itCanUpdateDatabaseTable() public function itCanExecuteUsingRawQuery() { $schema = new MySchema($this->pdo_schema); - $raw = $schema->raw('ALTER TABLE testing_db.users MODIFY COLUMN `user` varchar(20); ADD `status` int(3); DROP COLUMN `stat`'); + $raw = $schema->raw('ALTER TABLE testing_db.users MODIFY COLUMN `user` varchar(20), ADD `status` int(3), DROP COLUMN `stat`'); $this->assertTrue($raw->execute()); } diff --git a/tests/DataBase/Query/Schema/Table/AlterTest.php b/tests/DataBase/Query/Schema/Table/AlterTest.php index dd8f22c5..82c3b244 100644 --- a/tests/DataBase/Query/Schema/Table/AlterTest.php +++ b/tests/DataBase/Query/Schema/Table/AlterTest.php @@ -16,7 +16,7 @@ public function itCanGenerateQueryUsingModifyColumn() $schema('update_add')->int(17); $this->assertEquals( - 'ALTER TABLE testing_db.test MODIFY COLUMN `create_add` int(17); MODIFY COLUMN `update_add` int(17);', + 'ALTER TABLE testing_db.test MODIFY COLUMN `create_add` int(17), MODIFY COLUMN `update_add` int(17);', $schema->__toString() ); } @@ -29,7 +29,7 @@ public function itCanGenerateQueryUsingAddColumn() $schema->add('LastName')->varchar(255); $this->assertEquals( - 'ALTER TABLE testing_db.test ADD `PersonID` int; ADD `LastName` varchar(255);', + 'ALTER TABLE testing_db.test ADD `PersonID` int, ADD `LastName` varchar(255);', $schema->__toString() ); } @@ -42,7 +42,7 @@ public function itCanGenerateQueryUsingDropColumn() $schema->drop('LastName'); $this->assertEquals( - 'ALTER TABLE testing_db.test DROP COLUMN `PersonID`; DROP COLUMN `LastName`;', + 'ALTER TABLE testing_db.test DROP COLUMN `PersonID`, DROP COLUMN `LastName`;', $schema->__toString() ); } @@ -54,9 +54,8 @@ public function itCanGenerateQueryUsingRenameColumn() $schema->rename('PersonID', 'person_id'); $this->assertEquals( - 'ALTER TABLE testing_db.test ; RENAME COLUMN `PersonID` TO `person_id`;', - $schema->__toString(), - 'use start ; to prevent sql error' + 'ALTER TABLE testing_db.test RENAME COLUMN `PersonID` TO `person_id`;', + $schema->__toString() ); } @@ -68,7 +67,7 @@ public function itCanGenerateQueryUsingRenameColumnMultyple() $schema->rename('PersonID', 'person_id'); $this->assertEquals( - 'ALTER TABLE testing_db.test ; RENAME COLUMN `PersonID` TO `person_id`;', + 'ALTER TABLE testing_db.test RENAME COLUMN `PersonID` TO `person_id`;', $schema->__toString(), 'multy rename column will use last rename' ); @@ -83,7 +82,7 @@ public function itCanGenerateQueryUsingAltersColumn() $schema->column('create_add')->int(17); $this->assertEquals( - 'ALTER TABLE testing_db.test MODIFY COLUMN `create_add` int(17); ADD `PersonID` int(4); DROP COLUMN `LastName`;', + 'ALTER TABLE testing_db.test MODIFY COLUMN `create_add` int(17), ADD `PersonID` int(4), DROP COLUMN `LastName`;', $schema->__toString() ); } @@ -96,7 +95,7 @@ public function itCanGenerateQueryUsingModifyColumnAndOrderit() $schema->column('create_add')->after('id'); $this->assertEquals( - 'ALTER TABLE testing_db.test MODIFY COLUMN `uuid` int(17) FIRST; MODIFY COLUMN `create_add` AFTER `id`;', + 'ALTER TABLE testing_db.test MODIFY COLUMN `uuid` int(17) FIRST, MODIFY COLUMN `create_add` AFTER `id`;', $schema->__toString() ); } @@ -109,7 +108,7 @@ public function itCanGenerateQueryUsingAddColumnAndOrderit() $schema->add('create_add')->int(17)->after('id'); $this->assertEquals( - 'ALTER TABLE testing_db.test ADD `uuid` int(17) FIRST; ADD `create_add` int(17) AFTER `id`;', + 'ALTER TABLE testing_db.test ADD `uuid` int(17) FIRST, ADD `create_add` int(17) AFTER `id`;', $schema->__toString() ); }