Skip to content

Commit

Permalink
Fix compatibility with postgres
Browse files Browse the repository at this point in the history
  • Loading branch information
Shadow243 committed Dec 9, 2024
1 parent 191a947 commit 9da4405
Show file tree
Hide file tree
Showing 5 changed files with 171 additions and 232 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/Test-Build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ jobs:
strategy:
matrix:
php-versions: ['8.1']
database: ['mysql', 'sqlite']
database: ['mysql', 'postgres', 'sqlite']

env:
PHP_V: ${{ matrix.php-versions }}
Expand Down
10 changes: 10 additions & 0 deletions database/Core/Blueprint.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ public function dropColumn($name)
{
$this->droppedColumns[] = $name;
}
public function dropColumnIf($name, $condition = true)
{
if (is_callable($condition)) {
$condition = $condition();
}
if ($condition) {
$this->droppedColumns[] = $name;
}
return $this;
}

/**
* Create an auto-incrementing "id" column.
Expand Down
51 changes: 42 additions & 9 deletions database/Core/MigrationRunner.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,48 @@ public function __construct(PDO $pdo)
*/
private function ensureMigrationsTableExists()
{
$this->pdo->exec("
CREATE TABLE IF NOT EXISTS {$this->migrationsTable} (
id INT AUTO_INCREMENT PRIMARY KEY,
migration VARCHAR(255) NOT NULL,
batch INT NOT NULL,
applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
");
}
$createTableSql = "";

switch (Schema::getDriver()) {
case 'mysql':
$createTableSql = "
CREATE TABLE IF NOT EXISTS {$this->migrationsTable} (
id INT AUTO_INCREMENT PRIMARY KEY,
migration VARCHAR(255) NOT NULL,
batch INT NOT NULL,
applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
";
break;

case 'pgsql':
$createTableSql = "
CREATE TABLE IF NOT EXISTS {$this->migrationsTable} (
id SERIAL PRIMARY KEY,
migration VARCHAR(255) NOT NULL,
batch INT NOT NULL,
applied_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP
);
";
break;

case 'sqlite':
$createTableSql = "
CREATE TABLE IF NOT EXISTS {$this->migrationsTable} (
id INTEGER PRIMARY KEY AUTOINCREMENT,
migration TEXT NOT NULL,
batch INTEGER NOT NULL,
applied_at TEXT DEFAULT CURRENT_TIMESTAMP
);
";
break;

default:
throw new \Exception("Unsupported database driver: " . self::$driver);
}

$this->pdo->exec($createTableSql);
}
/**
* Run the migration runner.
*
Expand Down
Loading

0 comments on commit 9da4405

Please sign in to comment.