-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDatabase.php
83 lines (72 loc) · 2.66 KB
/
Database.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
<?php
namespace cleveruz\phpmvc;
use PDO;
class Database
{
public PDO $pdo;
private string $path;
public function __construct(array $config)
{
$this->pdo = new PDO($config["dsn"], $config["user"], $config["password"]);
$this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$this->path = Application::$ROOT_DIR . "/migrations";
}
public function prepare(string $sql)
{
return $this->pdo->prepare($sql);
}
public function applyMigrations(): void
{
$this->createMigrationsTable();
$newMigrations = [];
$appliedMigrations = $this->getAppliedMigrations();
$toApplyingMigrations = $this->getFileMigrations();
$applyingMigrations = array_diff($toApplyingMigrations, $appliedMigrations);
foreach ($applyingMigrations as $migration) {
$this->consoleLog("Applying migration {$migration}");
require_once $this->path . "/" . $migration;
$className = pathinfo($migration, PATHINFO_FILENAME);
call_user_func([new $className(), "up"]);
$this->consoleLog("Applied migration {$migration}");
array_push($newMigrations, $migration);
}
if (empty($newMigrations)) {
$this->consoleLog("No migrations files");
} else {
$this->saveMigrations($newMigrations);
$this->consoleLog("All migrations are applied");
}
}
private function createMigrationsTable(): void
{
$sql = "CREATE TABLE IF NOT EXISTS migrations(
id INT AUTO_INCREMENT PRIMARY KEY,
migration VARCHAR(200) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP)";
$this->pdo->exec($sql);
}
private function getAppliedMigrations(): mixed
{
$statement = $this->pdo->prepare("SELECT migration FROM migrations");
$statement->execute();
return $statement->fetchAll(PDO::FETCH_COLUMN);
}
private function saveMigrations($migrations): void
{
$values = implode(",", array_map(fn ($m) => "('{$m}')", $migrations));
$sql = "INSERT INTO migrations (migration) VALUES $values";
$this->pdo->exec($sql);
}
private function getFileMigrations(): array
{
return array_filter(scandir($this->path), function ($file) {
if ($file === "." || $file === "..")
return false;
return true;
});
}
private function consoleLog($message): void
{
echo "[" . date("Y-m-d H:i:s") . "]: {$message}" . PHP_EOL;
}
}