sql = $sql; $this->object = $object; $this->table = $table; $this->name = $name; $this->expect = $expect; } /** * Запрос, который выполняется всегда: перенос данных, CREATE TABLE IF NOT EXISTS, * DROP TABLE IF EXISTS — всё, что идемпотентно само по себе. */ public static function raw(string $sql): self { return new self($sql); } public static function addColumn(string $sql, string $table, string $column): self { return new self($sql, self::COLUMN, $table, $column, false); } public static function dropColumn(string $sql, string $table, string $column): self { return new self($sql, self::COLUMN, $table, $column, true); } public static function createIndex(string $sql, string $table, string $index): self { return new self($sql, self::INDEX, $table, $index, false); } public static function dropIndex(string $sql, string $table, string $index): self { return new self($sql, self::INDEX, $table, $index, true); } public static function createTable(string $sql, string $table): self { return new self($sql, self::TABLE, $table, $table, false); } public function sql(): string { return $this->sql; } /** * Нужно ли выполнять шаг. */ public function needed(Connection $db): bool { if ($this->object === null) { return true; } $exists = match ($this->object) { self::TABLE => $db->hasTable($this->table), self::COLUMN => $db->hasColumn($this->table, $this->name), self::INDEX => $db->hasIndex($this->table, $this->name), }; return $exists === $this->expect; } }