proceed further with beans

This commit is contained in:
Jan-Niclas Loosen
2025-02-24 10:54:51 +01:00
parent 024e6e73cb
commit 239b81bf0e
10 changed files with 156 additions and 197 deletions

View File

@@ -1,6 +1,8 @@
<?php
namespace DatabaseHelper;
use DatabaseHelper\beans\Column;
use DatabaseHelper\beans\Reference;
use DatabaseHelper\beans\Schema;
use DatabaseHelper\enums\Propagation;
use DatabaseHelper\enums\Type;
@@ -9,81 +11,76 @@ use InvalidArgumentException;
class Migration
{
protected Schema $schema;
protected array $columnsToAdd = [];
protected array $columnsToModify = [];
protected array $columnsToDrop = [];
protected array $foreignKeysToAdd = [];
protected array $foreignKeysToDrop = [];
protected array $newCols = [];
protected array $editCols = [];
protected array $dropCols = [];
protected array $addRefs = [];
protected array $dropRefs = [];
public function __construct(Schema $table) {
$this->schema = $table->copy();
$this->schema = $table;
}
public function column(string $name, Type $type, mixed $default = null, bool $isNullable = false, bool $isUnique = false): Migration {
if ($this->schema->existsColumn($name))
throw new InvalidArgumentException("Column '$name' already exists.");
$table = $this->schema->name;
if ($this->schema->existsCol($name))
throw new InvalidArgumentException("Column '$name' already exists in $table.");
$this->schema->columns[$name] = [
'name' => $name,
'type' => $type,
'default' => $default,
'isNullable' => $isNullable,
'isUnique' => $isUnique
];
$col = new Column;
$col->name = $name;
$col->type = $type;
$col->default = $default;
$col->isUnique = $isUnique;
$col->isNullable = $isNullable;
$this->columnsToAdd[] = $name;
$this->schema->cols[$name] = $col;
$this->newCols[] = $name;
return $this;
}
public function modify(string $name, Type $type, mixed $default = null, bool $isNullable = false, bool $isUnique = false): Migration {
$this->schema->requireColumn($name);
if (isset($this->schema->references[$name]))
throw new InvalidArgumentException('Referencing columns cannot be modified.');
$this->schema->reqCol($name);
$col = $this->schema->getCol($name);
$this->schema->columns[$name] = [
'name' => $name,
'type' => $type,
'default' => $default,
'isNullable' => $isNullable,
'isUnique' => $isUnique
];
$col->name = $name;
$col->type = $type;
$col->default = $default;
$col->isNullable = $isNullable;
$col->isUnique = $isUnique;#
$this->columnsToModify[] = $name;
$this->editCols[$name] = $col;
return $this;
}
public function drop(string $name): Migration {
$this->schema->requireColumn($name);
unset($this->schema->columns[$name]);
$this->columnsToDrop[] = $name;
unset($this->schema->cols[$name]);
$this->dropCols[] = $name;
return $this;
}
public function reference(Schema $foreignTable, Propagation $onDelete = Propagation::CASCADE, Propagation $onUpdate = Propagation::CASCADE): Migration {
$name = $foreignTable->primaryKey();
$this->schema->requireColumn($name);
if($this->schema->existsRef($foreignTable))
throw new InvalidArgumentException("Foreign table '$foreignTable->name' is already referenced in '{$this->schema->name}'.");
$name = $foreignTable->id->name;
$this->schema->references[$name] = [
'name' => $name,
'table' => $foreignTable->name,
'onDelete' => $onDelete,
'onUpdate' => $onUpdate
];
$this->schema->columns[$name] = $foreignTable->columns[$name];
$ref = new Reference;
$ref->name = $name;
$ref->onUpdate = $onUpdate;
$ref->onDelete = $onDelete;
$this->schema->refs[$name] = $ref;
$this->addRefs[] = $name;
$this->foreignKeysToAdd[] = $name;
return $this;
}
public function dereference(Schema $foreignTable): Migration {
$name = $foreignTable->primaryKey();
if ($this->schema->existsReference($foreignTable))
throw new InvalidArgumentException('Foreign table is not referenced.');
$this->schema->reqRef($foreignTable);
$name = $foreignTable->id->name;
unset($this->schema->references[$name]);
$this->drop($name);
unset($this->schema->refs[$name]);
$this->dropRefs[] = $name;
$this->foreignKeysToDrop[] = $name;
return $this;
}
@@ -94,8 +91,8 @@ class Migration
$clauses = [];
// Process new columns.
foreach ($this->columnsToAdd as $columnName) {
$col = $this->schema->columns[$columnName];
foreach ($this->newCols as $columnName) {
$col = $this->schema->cols[$columnName];
$clause = "ADD COLUMN `{$col['name']}` {$col['type']->toString()}";
$clause .= !$col['isNullable'] ? " NOT NULL" : "";
@@ -110,8 +107,8 @@ class Migration
}
// Process modified columns.
foreach ($this->columnsToModify as $columnName) {
$col = $this->schema->columns[$columnName];
foreach ($this->editCols as $columnName) {
$col = $this->schema->cols[$columnName];
$clause = "MODIFY COLUMN `{$col['name']}` {$col['type']->toString()}";
$clause .= !$col['isNullable'] ? " NOT NULL" : "";
@@ -127,12 +124,12 @@ class Migration
// Process dropped columns.
foreach ($this->columnsToDrop as $columnName)
foreach ($this->dropCols as $columnName)
$clauses[] = "DROP COLUMN `$columnName`";
// Process foreign keys to add.
foreach ($this->foreignKeysToAdd as $key) {
$foreignKey = $this->schema->references[$key];
foreach ($this->addRefs as $key) {
$foreignKey = $this->schema->refs[$key];
$clause = " ADD CONSTRAINT `fk_{$foreignKey['name']}`";
$clause .= " FOREIGN KEY (`{$foreignKey['name']}`)";
@@ -144,7 +141,7 @@ class Migration
}
// Process foreign keys to drop.
foreach ($this->foreignKeysToDrop as $fkName)
foreach ($this->dropRefs as $fkName)
$clauses[] = "DROP FOREIGN KEY `fk_{$fkName}`";
if (empty($clauses))