Files
folio/database/migrate-init.php
T

48 lines
1.4 KiB
PHP

<?php
// À exécuter UNE SEULE FOIS pour enregistrer les migrations déjà appliquées.
// php database/migrate-init.php
declare(strict_types=1);
$baseDir = dirname(__DIR__);
$envFile = $baseDir . '/.env';
$env = [];
foreach (file($envFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES) as $line) {
$line = trim($line);
if ($line === '' || str_starts_with($line, '#')) {
continue;
}
[$k, $v] = array_pad(explode('=', $line, 2), 2, '');
$env[trim($k)] = trim($v, '"\'');
}
$pdo = new PDO($env['DB_DSN'], $env['DB_USER'] ?? '', $env['DB_PASS'] ?? '', [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
]);
$pdo->exec('
CREATE TABLE IF NOT EXISTS schema_migrations (
name TEXT NOT NULL PRIMARY KEY,
applied_at TIMESTAMP NOT NULL DEFAULT NOW()
)
');
// Migrations déjà appliquées avant la mise en place de ce système
$alreadyApplied = [
'migration_001_roles_ratings.sql',
'migration_002_profile_url.sql',
'migration_003_profile_slug.sql',
'migration_004_profile_bio.sql',
'migration_005_rss_feeds.sql',
'migration_006_profile_links.sql',
];
$stmt = $pdo->prepare('INSERT INTO schema_migrations (name) VALUES (?) ON CONFLICT DO NOTHING');
foreach ($alreadyApplied as $name) {
$stmt->execute([$name]);
echo " ✓ marquée : $name\n";
}
echo "\nInitialisation terminée. Vous pouvez supprimer ce fichier.\n";