module manager: switched config table to use entity

This commit is contained in:
dswbx
2024-12-05 09:23:34 +01:00
parent 6c2f7b32e5
commit 3757157a06
7 changed files with 219 additions and 172 deletions

View File

@@ -16,26 +16,8 @@ export type Migration = {
export const migrations: Migration[] = [
{
version: 1,
schema: true,
up: async (config, { db }) => {
//console.log("config given", config);
await db.schema
.createTable(TABLE_NAME)
.addColumn("id", "integer", (col) => col.primaryKey().notNull().autoIncrement())
.addColumn("version", "integer", (col) => col.notNull())
.addColumn("type", "text", (col) => col.notNull())
.addColumn("json", "text")
.addColumn("created_at", "datetime", (col) => col.defaultTo(sql`CURRENT_TIMESTAMP`))
.addColumn("updated_at", "datetime", (col) => col.defaultTo(sql`CURRENT_TIMESTAMP`))
.execute();
await db
.insertInto(TABLE_NAME)
.values({ version: 1, type: "config", json: null })
.execute();
return config;
}
//schema: true,
up: async (config) => config
},
{
version: 2,
@@ -45,12 +27,8 @@ export const migrations: Migration[] = [
},
{
version: 3,
schema: true,
up: async (config, { db }) => {
await db.schema.alterTable(TABLE_NAME).addColumn("deleted_at", "datetime").execute();
return config;
}
//schema: true,
up: async (config) => config
},
{
version: 4,
@@ -127,28 +105,6 @@ export async function migrateTo(
return [version, updated];
}
export async function migrateSchema(to: number, ctx: MigrationContext, current: number = 0) {
console.log("migrating SCHEMA to", to, "from", current);
const todo = migrations.filter((m) => m.version > current && m.version <= to && m.schema);
console.log("todo", todo.length);
let i = 0;
let version = 0;
for (const migration of todo) {
console.log("-- running migration", i + 1, "of", todo.length);
try {
await migration.up({}, ctx);
version = migration.version;
i++;
} catch (e: any) {
console.error(e);
throw new Error(`Migration ${migration.version} failed: ${e.message}`);
}
}
return version;
}
export async function migrate(
current: number,
config: GenericConfigObject,