Skip to content

Commit deed648

Browse files
committed
Fixed Postgres edit migration
1 parent 6159d29 commit deed648

File tree

6 files changed

+27
-11
lines changed

6 files changed

+27
-11
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "interaapps/uloleorm",
3-
"version": "3.2.0",
3+
"version": "3.2.1",
44
"type": "library",
55
"authors": [
66
{

de/interaapps/ulole/orm/ModelInformation.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ public function autoMigrate(array|null $databases = null): ModelInformation {
162162
$col->ai()->primary();
163163

164164
if ($field->getColumnAttribute()->unique)
165-
$col->unqiue();
165+
$col->unique();
166166

167167
$col->nullable($field->getType()->allowsNull());
168168
}

de/interaapps/ulole/orm/Query.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public function whereNotExists(string $table, callable $callable): Query {
235235
* @return $this
236236
*/
237237
public function set($field, $value): Query {
238-
$this->queries[] = ['type' => 'SET', 'query' => $this->modelInformation->getFieldName($field) . ' = ?', 'val' => $value];
238+
$this->queries[] = ['type' => 'SET', 'query' => $this->modelInformation->getFieldName($field) . ' = ?', 'val' => (is_bool($value) ? ($value ? 'true' : 'false') : $value)];
239239
return $this;
240240
}
241241

de/interaapps/ulole/orm/drivers/PostgresDriver.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {

de/interaapps/ulole/orm/drivers/SQLDriver.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public function insert(string $table, array $fields, array $values): string|fals
103103

104104
$statement = $this->connection->prepare($query);
105105

106-
$result = $statement->execute($values);
106+
$result = $statement->execute(array_map(fn($f) => is_bool($f) ? ($f ? "true" : "false") : $f, $values));
107107
if (!$result)
108108
return false;
109109

uppm.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "uloleorm",
3-
"version": "3.2.0",
3+
"version": "3.2.1",
44
"repositories": [],
55
"run": {},
66
"build": {},

0 commit comments

Comments
 (0)