63 lines
2.2 KiB
TypeScript
63 lines
2.2 KiB
TypeScript
import "dotenv/config";
|
|
|
|
import { sql } from "kysely";
|
|
import { db } from "../src/db";
|
|
|
|
const migrations = [
|
|
db.schema
|
|
.createTable("letters")
|
|
.addColumn("id", "integer", (c) => c.primaryKey())
|
|
.addColumn("created", "datetime", (c) => c.defaultTo(sql`CURRENT_TIMESTAMP`).notNull())
|
|
.addColumn("updated", "datetime", (c) => c.defaultTo(sql`CURRENT_TIMESTAMP`).notNull())
|
|
.addColumn("firstName", "text", (c) => c.notNull())
|
|
.addColumn("lastName", "text", (c) => c.notNull())
|
|
.addColumn("language", "text", (c) => c.notNull())
|
|
.addColumn("email", "text", (c) => c.notNull())
|
|
.addColumn("phone", "text")
|
|
.addColumn("variant", "text", (c) => c.notNull())
|
|
.addColumn("gender", "text")
|
|
.addColumn("branch", "text", (c) => c.notNull())
|
|
.addColumn("isClient", "boolean", (c) => c.notNull())
|
|
.addColumn("message", "text", (c) => c.notNull())
|
|
.addColumn("subscribed", "boolean", (c) => c.notNull())
|
|
.addColumn("confirmationToken", "text", (c) => c.notNull())
|
|
.addColumn("confirmed", "boolean", (c) => c.notNull().defaultTo(false)),
|
|
db.schema
|
|
.createTable("messages")
|
|
.addColumn("id", "integer", (c) => c.primaryKey())
|
|
.addColumn("letterId", "integer", (c) => c.notNull())
|
|
.addColumn("from", "text", (c) => c.notNull())
|
|
.addColumn("to", "text", (c) => c.notNull())
|
|
.addColumn("status", "text", (c) => c.notNull()),
|
|
];
|
|
|
|
async function initMigrationsTable() {
|
|
await db.schema
|
|
.createTable("migrations")
|
|
.ifNotExists()
|
|
.addColumn("id", "integer", (c) => c.primaryKey())
|
|
.addColumn("executedAt", "datetime", (c) => c.defaultTo(sql`CURRENT_TIMESTAMP`).notNull())
|
|
.execute();
|
|
}
|
|
|
|
async function runMigrations() {
|
|
initMigrationsTable();
|
|
|
|
for (const [id, migration] of migrations.entries()) {
|
|
const migrationRecord = await db
|
|
.selectFrom("migrations")
|
|
.where("id", "=", id)
|
|
.select(["id", "executedAt"])
|
|
.executeTakeFirst();
|
|
|
|
if (migrationRecord) {
|
|
console.log(id, "|", "at", migrationRecord.executedAt);
|
|
} else {
|
|
await migration.execute();
|
|
await db.insertInto("migrations").values({ id }).execute();
|
|
console.log(id, "|", "now");
|
|
}
|
|
}
|
|
}
|
|
|
|
runMigrations();
|