-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #8 from DasunNethsara-04/main
Migrations implemented (make, run, rollback)
- Loading branch information
Showing
5 changed files
with
235 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace ZenithPHP\Core\Database; | ||
|
||
use PDO; | ||
use PDOException; | ||
|
||
class Database | ||
{ | ||
protected static ?PDO $connection = null; | ||
|
||
public static function connect(): PDO | ||
{ | ||
if (self::$connection === null) { | ||
try { | ||
// Load environment variables | ||
$host = DB_HOST; | ||
$dbname = DB_NAME; | ||
$username = DB_USER; | ||
$password = DB_PASS; | ||
|
||
$dsn = "mysql:host=$host;dbname=$dbname;charset=utf8mb4"; | ||
|
||
// Initialize the PDO connection | ||
self::$connection = new PDO($dsn, $username, $password); | ||
self::$connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); | ||
} catch (PDOException $e) { | ||
die("Database connection failed: " . $e->getMessage()); | ||
} | ||
} | ||
return self::$connection; | ||
} | ||
|
||
public static function execute($sql): void | ||
{ | ||
$connection = self::connect(); | ||
$connection->exec($sql); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
namespace ZenithPHP\Core\Database; | ||
|
||
abstract class Migration | ||
{ | ||
abstract public function up(); | ||
abstract public function down(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
<?php | ||
|
||
namespace ZenithPHP\Core\Database; | ||
|
||
class Schema | ||
{ | ||
protected string $tableName; | ||
protected array $columns = []; | ||
protected string $primaryKey = 'id'; | ||
|
||
public static function create($tableName, callable $callback): void | ||
{ | ||
$schema = new self(); | ||
$schema->tableName = $tableName; | ||
$callback($schema); | ||
$schema->executeCreate(); | ||
} | ||
|
||
public static function drop($tableName): void | ||
{ | ||
$sql = "DROP TABLE IF EXISTS `$tableName`;"; | ||
Database::execute($sql); | ||
} | ||
|
||
public function id(): void | ||
{ | ||
$this->columns[] = "`{$this->primaryKey}` INT AUTO_INCREMENT PRIMARY KEY"; | ||
} | ||
|
||
public function string($name, $length = 255): void | ||
{ | ||
$this->columns[] = "`$name` VARCHAR($length)"; | ||
} | ||
|
||
public function integer($name): void | ||
{ | ||
$this->columns[] = "`$name` INT"; | ||
} | ||
|
||
public function timestamps(): void | ||
{ | ||
$this->columns[] = "`created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP"; | ||
$this->columns[] = "`updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP"; | ||
} | ||
|
||
protected function executeCreate(): void | ||
{ | ||
$columnsSql = implode(", ", $this->columns); | ||
$sql = "CREATE TABLE IF NOT EXISTS `{$this->tableName}` ({$columnsSql});"; | ||
Database::execute($sql); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters