migrationsscript erstellt
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
const db = require("./db");
|
||||
const migrations = require("./migrations");
|
||||
|
||||
async function ensureMigrationsTable() {
|
||||
await db.query(`
|
||||
CREATE TABLE IF NOT EXISTS schema_migrations (
|
||||
id VARCHAR(255) PRIMARY KEY,
|
||||
applied_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
`);
|
||||
}
|
||||
|
||||
async function getAppliedMigrationIds() {
|
||||
const [rows] = await db.query(`
|
||||
SELECT id
|
||||
FROM schema_migrations
|
||||
ORDER BY id ASC
|
||||
`);
|
||||
|
||||
return new Set(rows.map((row) => row.id));
|
||||
}
|
||||
|
||||
async function runMigrations() {
|
||||
await ensureMigrationsTable();
|
||||
|
||||
const appliedIds = await getAppliedMigrationIds();
|
||||
|
||||
for (const migration of migrations) {
|
||||
if (appliedIds.has(migration.id)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
console.log(`Running migration ${migration.id}`);
|
||||
|
||||
await migration.up(db);
|
||||
|
||||
await db.query(
|
||||
"INSERT INTO schema_migrations (id) VALUES (?)",
|
||||
[migration.id]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
runMigrations,
|
||||
};
|
||||
Reference in New Issue
Block a user