@@ -17,11 +17,15 @@ public function getTables(): array {
1717 }
1818
1919 public function getColumns (string $ name ): array {
20- return array_map (fn ($ f ) => $ f [0 ], $ this ->query ("SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ' " . $ name . " '; " )->fetchAll ());
20+ return array_map (fn ($ f ) => $ f [0 ], $ this ->query ("SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ' $ name'; " )->fetchAll ());
2121 }
2222
2323 public function getIndexes (string $ name ): array {
24- return array_map (fn ($ f ) => $ f [0 ], $ this ->query ("SELECT indexname FROM pg_indexes WHERE tablename = ' " . $ name . "'; " )->fetchAll ());
24+ return array_map (fn ($ f ) => $ f [0 ], $ this ->query ("SELECT indexname FROM pg_indexes WHERE tablename = ' $ name'; " )->fetchAll ());
25+ }
26+
27+ public function getConstraints (string $ name ): array {
28+ return $ this ->query ("select constraint_type, constraint_name from information_schema.table_constraints where table_name=' $ name'; " )->fetchAll ();
2529 }
2630
2731 protected function getColumnQuery (Column $ column , bool $ new = false ): string {
@@ -32,12 +36,13 @@ protected function getColumnQuery(Column $column, bool $new = false): string {
3236
3337 public function edit (string $ name , callable $ callable ): bool {
3438 $ existingColumns = $ this ->query ("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ' " . $ name . "'; " )->fetchAll ();
35- $ existingColumnsNames = array_map (fn ($ f ) => $ f [3 ], $ existingColumns );
3639 $ existingIndexes = $ this ->getIndexes ($ name );
37-
40+ $ existingConstraints = $ this ->getConstraints ($ name );
41+ $ uniqueKeys = array_map (fn ($ f ) => $ f ["constraint_name " ], array_filter ($ existingConstraints , fn ($ f ) => $ f ["constraint_type " ] == "UNIQUE " ));
42+ var_dump ($ existingColumns );
3843 $ blueprint = new Blueprint ();
3944 $ callable ($ blueprint );
40- $ sql = "ALTER TABLE " . $ name . " " ;
45+ $ sql = "ALTER TABLE $ name " ;
4146 $ changes = [];
4247
4348 foreach ($ blueprint ->getColumns () as $ column ) {
@@ -51,12 +56,23 @@ public function edit(string $name, callable $callable): bool {
5156 }
5257
5358 if ($ columnFromDB !== null ) {
59+ $ changes [] = "ALTER COLUMN {$ column ->getName ()} TYPE " . $ column ->getType () . ($ column ->getSize () !== null ? "( " . $ column ->getSize () . ") " : "" );
60+
5461 if (($ columnFromDB [6 ] == 'YES ' ) != $ column ->isNullable ())
55- $ changes [] = "ALTER COLUMN {$ column ->getName ()} SET " . ($ column ->isNullable () ? " SET NOT NULL " : " DROP NOT NULL " );
62+ $ changes [] = "ALTER COLUMN {$ column ->getName ()} " . ($ column ->isNullable () ? " SET NOT NULL " : " DROP NOT NULL " );
5663
5764 if ($ column ->isDefaultDefined ())
5865 $ changes [] = "ALTER COLUMN {$ column ->getName ()} SET DEFAULT ' {$ column ->getDefault ()}' " ;
5966
67+ if (!in_array ($ column ->getName () . '_unique ' , $ uniqueKeys ) && $ column ->isUnique ())
68+ $ changes [] = "ADD CONSTRAINT {$ column ->getName ()}_unique UNIQUE ( {$ column ->getName ()}) " ;
69+
70+ if (in_array ($ column ->getName () . '_unique ' , $ uniqueKeys ) && !$ column ->isUnique ())
71+ $ changes [] = "DROP CONSTRAINT {$ column ->getName ()}_unique " ;
72+
73+ if ($ column ->isUnique ())
74+ $ changes [] = "ADD UNIQUE ( {$ column ->getName ()}) " ;
75+
6076 if ($ column ->isDrop ())
6177 $ changes [] = "DROP COLUMN {$ column ->getName ()}" ;
6278 } else {
0 commit comments