finish migration class

This commit is contained in:
Jan-Niclas Loosen
2025-02-12 16:55:03 +01:00
parent c4a7025978
commit fe9661d611
5 changed files with 196 additions and 148 deletions

214
Table.php
View File

@@ -2,165 +2,131 @@
namespace DatabaseHelper;
use DatabaseHelper\enums\Propagation;
use DatabaseHelper\enums\Charset;
use DatabaseHelper\enums\Collation;
use DatabaseHelper\enums\Type;
use DatabaseHelper\enums\Engine;
use InvalidArgumentException;
class Migration
class Table
{
protected Schema $schema;
protected array $columnsToAdd = [];
protected array $columnsToModify = [];
protected array $columnsToDrop = [];
protected ?array $primaryKey = null;
protected array $foreignKeysToAdd = [];
protected array $foreignKeysToDrop = [];
protected Schema $table;
public function __construct(Schema $table) {
$this->schema = $table->copy();
public function __construct(string $tableName) {
Database::standardizeTableNames($tableName);
$this->table = new Schema($tableName);
}
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,
public function column(string $name, Type $type, mixed $default = null, bool $isNullable = false, bool $isUnique = false): Table {
$this->table->columns[$name] = [
'name' => $name,
'default' => $default,
'type' => $type,
'isNullable' => $isNullable,
'isUnique' => $isUnique
'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);
$this->schema->columns[$name] = [
'name' => $name,
'type' => $type,
'default' => $default,
'isNullable' => $isNullable,
'isUnique' => $isUnique
public function primary(string $name, Type $type, bool $autoInc = false): Table {
if(isset($this->primaryKey))
throw new InvalidArgumentException('Primary column already exists.');
$this->table->primaryKey = [
'name' => $name,
'autoInc' => $autoInc
];
$this->columnsToModify[] = $name;
return $this;
return $this->column($name, $type);
}
public function delete(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 {
public function reference(Schema $foreignTable, Propagation $onDelete = Propagation::CASCADE, Propagation $onUpdate = Propagation::CASCADE): Table {
$name = $foreignTable->primaryKey();
if ($this->schema->existsColumn($name))
throw new InvalidArgumentException("Column '$name' already exists.");
if(isset($this->columns[$name]))
throw new InvalidArgumentException('Column name already exists.');
$this->schema->foreignKeys[$name] = [
'name' => $name,
'table' => $foreignTable->name,
$this->table->foreignKeys[$name] = [
'name' => $name,
'table' => $foreignTable->name,
'onDelete' => $onDelete,
'onUpdate' => $onUpdate
];
$this->schema->columns[$name] = $foreignTable->columns[$name];
$this->foreignKeysToAdd[] = $name;
$this->table->columns[$name] = $foreignTable->columns[$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->foreignKeys[$name]);
// Also remove the column and mark it for dropping.
if (isset($this->schema->columns[$name]))
$this->delete($name);
$this->foreignKeysToDrop[] = $name;
public function engine(Engine $engine): Table {
$this->table->engine = $engine;
return $this;
}
public function charset(Charset $charset): Table {
$this->table->charset = $charset;
return $this;
}
public function collation(Collation $collation): Table {
$this->table->collation = $collation;
return $this;
}
/**
* Generates the SQL statement.
* @return string SQL query.
* @throws InvalidArgumentException
*/
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 = [];
$primaryKey = $this->table->primaryKey();
// Process new columns.
foreach ($this->columnsToAdd as $columnName) {
$col = $this->schema->columns[$columnName];
$clause = "ADD COLUMN `{$col['name']}` " . $col['type']->toString();
if (!$col['isNullable']) {
$clause .= " NOT NULL";
$clause = "CREATE TABLE `{$this->table->name}` (\n";
$clause .= " PRIMARY KEY (`$primaryKey`),\n";
foreach ($this->table->columns as $col) {
if ($col['name'] !== $primaryKey) {
$clause .= " `{$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";
}
$clause .= ",\n";
}
if ($col['isUnique']) {
$clause .= " UNIQUE";
}
if ($col['default'] !== null) {
$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();
if (!$col['isNullable']) {
$clause .= " NOT NULL";
}
if ($col['isUnique']) {
$clause .= " UNIQUE";
}
if ($col['default'] !== null) {
$default = is_string($col['default']) ? "'{$col['default']}'" : $col['default'];
$clause .= " DEFAULT $default";
}
$clauses[] = $clause;
// Add foreign keys
foreach ($this->table->foreignKeys as $key) {
$clause .= " FOREIGN KEY (`{$key['name']}`)";
$clause .= " REFERENCES `{$key['table']}` (`{$key['name']}`)";
$clause .= " ON DELETE {$key['onDelete']->toString()}";
$clause .= " ON UPDATE {$key['onUpdate']->toString()},\n";
}
// Process dropped columns.
foreach ($this->columnsToDrop as $columnName) {
$clauses[] = "DROP COLUMN `{$columnName}`";
}
// Process foreign keys to add.
foreach ($this->foreignKeysToAdd as $fkName) {
$fk = $this->schema->foreignKeys[$fkName];
// Here we name the constraint “fk_{$name}”. (There are other acceptable naming schemes.)
$clause = "ADD CONSTRAINT `fk_{$fk['name']}` FOREIGN KEY (`{$fk['name']}`) REFERENCES `{$fk['table']}` (`{$fk['name']}`) ON DELETE " . $fk['onDelete']->toString() . " ON UPDATE " . $fk['onUpdate']->toString();
$clauses[] = $clause;
}
// Process foreign keys to drop.
foreach ($this->foreignKeysToDrop as $fkName) {
// Again, we assume the constraint was named “fk_{$name}”
$clauses[] = "DROP FOREIGN KEY `fk_{$fkName}`";
}
if (empty($clauses)) {
throw new InvalidArgumentException("No migration operations to perform.");
}
$sql = "ALTER TABLE `{$tableName}`\n" . implode(",\n", $clauses) . ";";
return esc_sql($sql);
}
public function drop(): null {
global $wpdb;
$tableName = $wpdb->prefix . $this->schema->name;
$sql = "DROP TABLE IF EXISTS `{$tableName}`;";
$wpdb->query(esc_sql($sql));
return null;
// Close the SQL string and add constraints
$clause = rtrim($clause, ",\n") . "\n) ";
$clause .= "ENGINE={$this->table->engine->toString()} ";
$clause .= "CHARSET={$this->table->charset->toString()} ";
$clause -= "COLLATE={$this->table->collation->toString()};";
return $clause;
}
public function migrate(): Schema {
public function create(): Schema {
global $wpdb;
$sql = $this->toSql();
$wpdb->query($sql);
return $this->schema;
if (empty($this->primaryKey))
throw new InvalidArgumentException('A primary key must be defined.');
$table_name = $wpdb->prefix . $this->table->name;
if ($wpdb->get_var("SHOW TABLES LIKE '$table_name'") !== $table_name) {
$sql = $this->toSql();
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
dbDelta($sql);
}
return $this->table;
}
}
}