improve migration class
This commit is contained in:
		@@ -7,7 +7,7 @@ use InvalidArgumentException;
 | 
			
		||||
 | 
			
		||||
class Migration
 | 
			
		||||
{
 | 
			
		||||
    protected Schema $table;
 | 
			
		||||
    protected Schema $schema;
 | 
			
		||||
    protected array $columnsToAdd = [];
 | 
			
		||||
    protected array $columnsToModify = [];
 | 
			
		||||
    protected array $columnsToDrop = [];
 | 
			
		||||
@@ -16,11 +16,13 @@ class Migration
 | 
			
		||||
    protected array $foreignKeysToDrop = [];
 | 
			
		||||
 | 
			
		||||
    public function __construct(Schema $table) {
 | 
			
		||||
        $this->table = $table->copy();
 | 
			
		||||
        $this->schema = $table->copy();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function addColumn(string $name, Type $type, mixed $default = null, bool $isNullable = false, bool $isUnique = false): Migration {
 | 
			
		||||
        $this->table->columns[$name] = [
 | 
			
		||||
    public function add(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,
 | 
			
		||||
@@ -31,11 +33,9 @@ class Migration
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function modifyColumn(string $name, Type $type, mixed $default = null, bool $isNullable = false, bool $isUnique = false): Migration {
 | 
			
		||||
        if (!isset($this->table->columns[$name])) {
 | 
			
		||||
            throw new InvalidArgumentException("Column $name does not exist.");
 | 
			
		||||
        }
 | 
			
		||||
        $this->table->columns[$name] = [
 | 
			
		||||
    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,
 | 
			
		||||
@@ -46,87 +46,27 @@ class Migration
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function dropColumn(string $name): Migration {
 | 
			
		||||
        if (!isset($this->table->columns[$name])) {
 | 
			
		||||
            throw new InvalidArgumentException("Column $name does not exist.");
 | 
			
		||||
        }
 | 
			
		||||
        unset($this->table->columns[$name]);
 | 
			
		||||
    public function delete(string $name): Migration {
 | 
			
		||||
        $this->schema->requireColumn($name);
 | 
			
		||||
        unset($this->schema->columns[$name]);
 | 
			
		||||
        $this->columnsToDrop[] = $name;
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function changePrimaryKey(string $name, bool $autoInc = false): Migration {
 | 
			
		||||
        if (!isset($this->table->columns[$name])) {
 | 
			
		||||
            throw new InvalidArgumentException("Column $name does not exist.");
 | 
			
		||||
        }
 | 
			
		||||
        $this->table->primaryKey = [
 | 
			
		||||
            'name' => $name,
 | 
			
		||||
            'autoInc' => $autoInc
 | 
			
		||||
        ];
 | 
			
		||||
        $this->primaryKey = $name;
 | 
			
		||||
        return $this;
 | 
			
		||||
    public function reference(Schema $foreignTable, Propagation $onDelete = Propagation::CASCADE, Propagation $onUpdate = Propagation::CASCADE): Migration {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function addForeignKey(string $column, string $referencedTable, string $referencedColumn, Propagation $onDelete, Propagation $onUpdate): Migration {
 | 
			
		||||
        if (!isset($this->table->columns[$column])) {
 | 
			
		||||
            throw new InvalidArgumentException("Column $column does not exist.");
 | 
			
		||||
        }
 | 
			
		||||
        $this->table->foreignKeys[$column] = [
 | 
			
		||||
            'referencedTable' => $referencedTable,
 | 
			
		||||
            'referencedColumn' => $referencedColumn,
 | 
			
		||||
            'onDelete' => $onDelete,
 | 
			
		||||
            'onUpdate' => $onUpdate
 | 
			
		||||
        ];
 | 
			
		||||
        $this->foreignKeysToAdd[] = $column;
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
    public function dereference(Schema $foreignTable): Migration {
 | 
			
		||||
 | 
			
		||||
    public function dropForeignKey(string $name): Migration {
 | 
			
		||||
        if (!isset($this->table->foreignKeys[$name])) {
 | 
			
		||||
            throw new InvalidArgumentException("Foreign key $name does not exist.");
 | 
			
		||||
        }
 | 
			
		||||
        unset($this->table->foreignKeys[$name]);
 | 
			
		||||
        $this->foreignKeysToDrop[] = $name;
 | 
			
		||||
        return $this;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function toSql(): string {
 | 
			
		||||
        $sql = "ALTER TABLE `{$this->table->name}` ";
 | 
			
		||||
        $statements = [];
 | 
			
		||||
 | 
			
		||||
        foreach ($this->columnsToAdd as $name) {
 | 
			
		||||
            $col = $this->table->columns[$name];
 | 
			
		||||
            $statements[] = "ADD COLUMN `{$col['name']}` {$col['type']->toString()}" .
 | 
			
		||||
                ($col['isNullable'] ? "" : " NOT NULL") .
 | 
			
		||||
                ($col['isUnique'] ? " UNIQUE" : "") .
 | 
			
		||||
                ($col['default'] !== null ? " DEFAULT " . (is_string($col['default']) ? "'{$col['default']}'" : $col['default']) : "");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        foreach ($this->columnsToModify as $name) {
 | 
			
		||||
            $col = $this->table->columns[$name];
 | 
			
		||||
            $statements[] = "MODIFY COLUMN `{$col['name']}` {$col['type']->toString()}" .
 | 
			
		||||
                ($col['isNullable'] ? "" : " NOT NULL") .
 | 
			
		||||
                ($col['isUnique'] ? " UNIQUE" : "") .
 | 
			
		||||
                ($col['default'] !== null ? " DEFAULT " . (is_string($col['default']) ? "'{$col['default']}'" : $col['default']) : "");
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        foreach ($this->columnsToDrop as $name) {
 | 
			
		||||
            $statements[] = "DROP COLUMN `$name`";
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        foreach ($this->foreignKeysToDrop as $name) {
 | 
			
		||||
            $statements[] = "DROP FOREIGN KEY `$name`";
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        foreach ($this->foreignKeysToAdd as $column) {
 | 
			
		||||
            $fk = $this->table->foreignKeys[$column];
 | 
			
		||||
            $statements[] = "ADD CONSTRAINT `fk_{$column}` FOREIGN KEY (`$column`) REFERENCES `{$fk['referencedTable']}` (`{$fk['referencedColumn']}`) ON DELETE {$fk['onDelete']->toString()} ON UPDATE {$fk['onUpdate']->toString()}";
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        return $sql . implode(", ", $statements) . ";";
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    public function drop(): null {
 | 
			
		||||
        global $wpdb;
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -134,6 +74,6 @@ class Migration
 | 
			
		||||
        global $wpdb;
 | 
			
		||||
        $sql = $this->toSql();
 | 
			
		||||
        $wpdb->query($sql);
 | 
			
		||||
        return $this->table;
 | 
			
		||||
        return $this->schema;
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user