finish migration class
This commit is contained in:
112
Migration.php
112
Migration.php
@@ -11,7 +11,6 @@ class Migration
|
||||
protected array $columnsToAdd = [];
|
||||
protected array $columnsToModify = [];
|
||||
protected array $columnsToDrop = [];
|
||||
protected array $primaryKey = null;
|
||||
protected array $foreignKeysToAdd = [];
|
||||
protected array $foreignKeysToDrop = [];
|
||||
|
||||
@@ -19,34 +18,40 @@ class Migration
|
||||
$this->schema = $table->copy();
|
||||
}
|
||||
|
||||
public function add(string $name, Type $type, mixed $default = null, bool $isNullable = false, bool $isUnique = false): Migration {
|
||||
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,
|
||||
'name' => $name,
|
||||
'type' => $type,
|
||||
'default' => $default,
|
||||
'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);
|
||||
if (isset($this->schema->foreignKeys[$name]))
|
||||
throw new InvalidArgumentException('Referencing columns cannot be modified.');
|
||||
|
||||
$this->schema->columns[$name] = [
|
||||
'name' => $name,
|
||||
'type' => $type,
|
||||
'default' => $default,
|
||||
'name' => $name,
|
||||
'type' => $type,
|
||||
'default' => $default,
|
||||
'isNullable' => $isNullable,
|
||||
'isUnique' => $isUnique
|
||||
'isUnique' => $isUnique
|
||||
];
|
||||
|
||||
$this->columnsToModify[] = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function delete(string $name): Migration {
|
||||
public function drop(string $name): Migration {
|
||||
$this->schema->requireColumn($name);
|
||||
unset($this->schema->columns[$name]);
|
||||
$this->columnsToDrop[] = $name;
|
||||
@@ -54,20 +59,97 @@ class Migration
|
||||
}
|
||||
|
||||
public function reference(Schema $foreignTable, Propagation $onDelete = Propagation::CASCADE, Propagation $onUpdate = Propagation::CASCADE): Migration {
|
||||
$name = $foreignTable->primaryKey();
|
||||
$this->schema->requireColumn($name);
|
||||
|
||||
$this->schema->foreignKeys[$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->foreignKeys[$name]);
|
||||
$this->drop($name);
|
||||
|
||||
$this->foreignKeysToDrop[] = $name;
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function toSql(): string {
|
||||
|
||||
}
|
||||
|
||||
public function drop(): null {
|
||||
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->foreignKeys[$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 {
|
||||
|
Reference in New Issue
Block a user