163 lines
5.5 KiB
PHP
163 lines
5.5 KiB
PHP
<?php
|
|
namespace DatabaseHelper;
|
|
|
|
use DatabaseHelper\beans\Schema;
|
|
use DatabaseHelper\enums\Propagation;
|
|
use DatabaseHelper\enums\Type;
|
|
use InvalidArgumentException;
|
|
|
|
class Migration
|
|
{
|
|
protected Schema $schema;
|
|
protected array $columnsToAdd = [];
|
|
protected array $columnsToModify = [];
|
|
protected array $columnsToDrop = [];
|
|
protected array $foreignKeysToAdd = [];
|
|
protected array $foreignKeysToDrop = [];
|
|
|
|
public function __construct(Schema $table) {
|
|
$this->schema = $table->copy();
|
|
}
|
|
|
|
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.");
|
|
|
|
$this->schema->columns[$name] = [
|
|
'name' => $name,
|
|
'type' => $type,
|
|
'default' => $default,
|
|
'isNullable' => $isNullable,
|
|
'isUnique' => $isUnique
|
|
];
|
|
|
|
$this->columnsToAdd[] = $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->columns[$name] = [
|
|
'name' => $name,
|
|
'type' => $type,
|
|
'default' => $default,
|
|
'isNullable' => $isNullable,
|
|
'isUnique' => $isUnique
|
|
];
|
|
|
|
$this->columnsToModify[] = $name;
|
|
return $this;
|
|
}
|
|
|
|
public function drop(string $name): Migration {
|
|
$this->schema->requireColumn($name);
|
|
unset($this->schema->columns[$name]);
|
|
$this->columnsToDrop[] = $name;
|
|
return $this;
|
|
}
|
|
|
|
public function reference(Schema $foreignTable, Propagation $onDelete = Propagation::CASCADE, Propagation $onUpdate = Propagation::CASCADE): Migration {
|
|
$name = $foreignTable->primaryKey();
|
|
$this->schema->requireColumn($name);
|
|
|
|
$this->schema->references[$name] = [
|
|
'name' => $name,
|
|
'table' => $foreignTable->name,
|
|
'onDelete' => $onDelete,
|
|
'onUpdate' => $onUpdate
|
|
];
|
|
$this->schema->columns[$name] = $foreignTable->columns[$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.');
|
|
|
|
unset($this->schema->references[$name]);
|
|
$this->drop($name);
|
|
|
|
$this->foreignKeysToDrop[] = $name;
|
|
return $this;
|
|
}
|
|
|
|
public function toSql(): string {
|
|
global $wpdb;
|
|
// We assume that the table name in the schema is not prefixed; add the prefix here.
|
|
$tableName = $wpdb->prefix . $this->schema->name;
|
|
$clauses = [];
|
|
|
|
// Process new columns.
|
|
foreach ($this->columnsToAdd as $columnName) {
|
|
$col = $this->schema->columns[$columnName];
|
|
$clause = "ADD COLUMN `{$col['name']}` {$col['type']->toString()}";
|
|
|
|
$clause .= !$col['isNullable'] ? " NOT NULL" : "";
|
|
$clause .= $col['isUnique'] ? " UNIQUE" : "";
|
|
|
|
if (!is_null($col['default'])) {
|
|
$default = is_string($col['default']) ? "'{$col['default']}'" : $col['default'];
|
|
$clause .= " DEFAULT $default";
|
|
}
|
|
|
|
$clauses[] = $clause;
|
|
}
|
|
|
|
// Process modified columns.
|
|
foreach ($this->columnsToModify as $columnName) {
|
|
$col = $this->schema->columns[$columnName];
|
|
$clause = "MODIFY COLUMN `{$col['name']}` {$col['type']->toString()}";
|
|
|
|
$clause .= !$col['isNullable'] ? " NOT NULL" : "";
|
|
$clause .= $col['isUnique'] ? " UNIQUE" : "";
|
|
|
|
if (!is_null($col['default'])) {
|
|
$default = is_string($col['default']) ? "'{$col['default']}'" : $col['default'];
|
|
$clause .= " DEFAULT $default";
|
|
}
|
|
|
|
$clauses[] = $clause;
|
|
}
|
|
|
|
|
|
// Process dropped columns.
|
|
foreach ($this->columnsToDrop as $columnName)
|
|
$clauses[] = "DROP COLUMN `$columnName`";
|
|
|
|
// Process foreign keys to add.
|
|
foreach ($this->foreignKeysToAdd as $key) {
|
|
$foreignKey = $this->schema->references[$key];
|
|
|
|
$clause = " ADD CONSTRAINT `fk_{$foreignKey['name']}`";
|
|
$clause .= " FOREIGN KEY (`{$foreignKey['name']}`)";
|
|
$clause .= " REFERENCES `{$foreignKey['table']}` (`{$foreignKey['name']}`)";
|
|
$clause .= " ON DELETE {$foreignKey['onDelete']->toString()}";
|
|
$clause .= " ON UPDATE {$foreignKey['onUpdate']->toString()},\n";
|
|
|
|
$clauses[] = $clause;
|
|
}
|
|
|
|
// Process foreign keys to drop.
|
|
foreach ($this->foreignKeysToDrop as $fkName)
|
|
$clauses[] = "DROP FOREIGN KEY `fk_{$fkName}`";
|
|
|
|
if (empty($clauses))
|
|
throw new InvalidArgumentException("No migration operations to perform.");
|
|
|
|
return "ALTER TABLE `$tableName`\n" . implode(",\n", $clauses) . ";";
|
|
}
|
|
|
|
public function migrate(): Schema {
|
|
global $wpdb;
|
|
$sql = $this->toSql();
|
|
$wpdb->query($sql);
|
|
return $this->schema;
|
|
}
|
|
}
|