Skip to content

Commit

Permalink
Fixed Postgres edit migration
Browse files Browse the repository at this point in the history
  • Loading branch information
JulianFun123 committed Jul 27, 2022
1 parent 6159d29 commit deed648
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "interaapps/uloleorm",
"version": "3.2.0",
"version": "3.2.1",
"type": "library",
"authors": [
{
Expand Down
2 changes: 1 addition & 1 deletion de/interaapps/ulole/orm/ModelInformation.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ public function autoMigrate(array|null $databases = null): ModelInformation {
$col->ai()->primary();

if ($field->getColumnAttribute()->unique)
$col->unqiue();
$col->unique();

$col->nullable($field->getType()->allowsNull());
}
Expand Down
2 changes: 1 addition & 1 deletion de/interaapps/ulole/orm/Query.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ public function whereNotExists(string $table, callable $callable): Query {
* @return $this
*/
public function set($field, $value): Query {
$this->queries[] = ['type' => 'SET', 'query' => $this->modelInformation->getFieldName($field) . ' = ?', 'val' => $value];
$this->queries[] = ['type' => 'SET', 'query' => $this->modelInformation->getFieldName($field) . ' = ?', 'val' => (is_bool($value) ? ($value ? 'true' : 'false') : $value)];
return $this;
}

Expand Down
28 changes: 22 additions & 6 deletions de/interaapps/ulole/orm/drivers/PostgresDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ public function getTables(): array {
}

public function getColumns(string $name): array {
return array_map(fn($f) => $f[0], $this->query("SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" . $name . "';")->fetchAll());
return array_map(fn($f) => $f[0], $this->query("SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '$name';")->fetchAll());
}

public function getIndexes(string $name): array {
return array_map(fn($f) => $f[0], $this->query("SELECT indexname FROM pg_indexes WHERE tablename = '" . $name . "';")->fetchAll());
return array_map(fn($f) => $f[0], $this->query("SELECT indexname FROM pg_indexes WHERE tablename = '$name';")->fetchAll());
}

public function getConstraints(string $name): array {
return $this->query("select constraint_type, constraint_name from information_schema.table_constraints where table_name='$name';")->fetchAll();
}

protected function getColumnQuery(Column $column, bool $new = false): string {
Expand All @@ -32,12 +36,13 @@ protected function getColumnQuery(Column $column, bool $new = false): string {

public function edit(string $name, callable $callable): bool {
$existingColumns = $this->query("SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = '" . $name . "';")->fetchAll();
$existingColumnsNames = array_map(fn($f) => $f[3], $existingColumns);
$existingIndexes = $this->getIndexes($name);

$existingConstraints = $this->getConstraints($name);
$uniqueKeys = array_map(fn ($f) => $f["constraint_name"], array_filter($existingConstraints, fn($f) => $f["constraint_type"] == "UNIQUE"));
var_dump($existingColumns);
$blueprint = new Blueprint();
$callable($blueprint);
$sql = "ALTER TABLE " . $name . " ";
$sql = "ALTER TABLE $name ";
$changes = [];

foreach ($blueprint->getColumns() as $column) {
Expand All @@ -51,12 +56,23 @@ public function edit(string $name, callable $callable): bool {
}

if ($columnFromDB !== null) {
$changes[] = "ALTER COLUMN {$column->getName()} TYPE " . $column->getType() . ($column->getSize() !== null ? "(" . $column->getSize() . ")" : "");

if (($columnFromDB[6] == 'YES') != $column->isNullable())
$changes[] = "ALTER COLUMN {$column->getName()} SET " . ($column->isNullable() ? " SET NOT NULL" : " DROP NOT NULL");
$changes[] = "ALTER COLUMN {$column->getName()} " . ($column->isNullable() ? " SET NOT NULL" : " DROP NOT NULL");

if ($column->isDefaultDefined())
$changes[] = "ALTER COLUMN {$column->getName()} SET DEFAULT '{$column->getDefault()}'";

if (!in_array($column->getName() . '_unique', $uniqueKeys) && $column->isUnique())
$changes[] = "ADD CONSTRAINT {$column->getName()}_unique UNIQUE ({$column->getName()})";

if (in_array($column->getName() . '_unique', $uniqueKeys) && !$column->isUnique())
$changes[] = "DROP CONSTRAINT {$column->getName()}_unique";

if ($column->isUnique())
$changes[] = "ADD UNIQUE ({$column->getName()})";

if ($column->isDrop())
$changes[] = "DROP COLUMN {$column->getName()}";
} else {
Expand Down
2 changes: 1 addition & 1 deletion de/interaapps/ulole/orm/drivers/SQLDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public function insert(string $table, array $fields, array $values): string|fals

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

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

Expand Down
2 changes: 1 addition & 1 deletion uppm.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "uloleorm",
"version": "3.2.0",
"version": "3.2.1",
"repositories": [],
"run": {},
"build": {},
Expand Down

0 comments on commit deed648

Please sign in to comment.