Setting up db migrations

This commit is contained in:
Dominic Ferrando
2026-07-03 21:01:56 -04:00
parent f5a68f436b
commit 37ffaa282b
8 changed files with 142 additions and 35 deletions
+14 -2
View File
@@ -1,5 +1,5 @@
import { Kysely, PostgresDialect } from 'kysely';
import { type DB } from 'kysely-codegen';
import { CreateTableBuilder, Kysely, PostgresDialect, sql } from 'kysely';
import { type DB } from "./types";
import { Pool } from "pg";
import { env } from '../util';
@@ -10,3 +10,15 @@ export const db = new Kysely<DB>({
}),
})
});
export function addDefaultColumns(ctb: CreateTableBuilder<any, any>) {
return ctb
.addColumn("id", "bigint", (cb) => cb
.generatedAlwaysAsIdentity()
.primaryKey()
)
.addColumn("created_at", "timestamptz", (cb) => cb
.notNull()
.defaultTo(sql`now()`)
)
};
@@ -1,13 +1,8 @@
import { CreateTableBuilder, sql } from "kysely";
import { db } from "./db";
import { log } from "../util";
import { Kysely, sql } from 'kysely'
import { addDefaultColumns } from '../db';
// =====================
// PRODUCT SYNCS
// =====================
{
log.info({ name: "product_syncs" }, "Creating table");
await db.schema.dropTable("product_syncs").ifExists().execute()
export async function up(db: Kysely<any>): Promise<void> {
// TABLE: PRODUCT_SYNCS
await db.schema.createTable("product_syncs")
.$call(addDefaultColumns)
.addColumn("printful_product_id_filter", sql`integer[]`)
@@ -20,7 +15,7 @@ import { log } from "../util";
.addColumn("has_failed", "boolean", (cb) => cb.notNull().defaultTo(false))
.execute()
await db.schema.dropIndex("idx_one_active_product_sync").ifExists().execute();
// INDEX: ONE_ACTIVE_PRODUCT_SYNC
await db.schema.createIndex("idx_one_active_product_sync")
.on("product_syncs")
.unique()
@@ -31,14 +26,10 @@ import { log } from "../util";
.execute();
}
function addDefaultColumns(ctb: CreateTableBuilder<any, any>) {
return ctb
.addColumn("id", "bigint", (cb) => cb
.generatedAlwaysAsIdentity()
.primaryKey()
)
.addColumn("created_at", "timestamptz", (cb) => cb
.notNull()
.defaultTo(sql`now()`)
)
};
export async function down(db: Kysely<any>): Promise<void> {
// TABLE: PRODUCT_SYNCS
await db.schema.dropTable("product_syncs").ifExists().execute()
// INDEX: ONE_ACTIVE_PRODUCT_SYNC
await db.schema.dropIndex("idx_one_active_product_sync").ifExists().execute();
}
+28
View File
@@ -0,0 +1,28 @@
/**
* This file was generated by kysely-codegen.
* Please do not edit it manually.
*/
import type { ColumnType } from "kysely";
export type Generated<T> = T extends ColumnType<infer S, infer I, infer U>
? ColumnType<S, I | undefined, U>
: ColumnType<T, T | undefined, T>;
export type Int8 = ColumnType<string, bigint | number | string, bigint | number | string>;
export type Timestamp = ColumnType<Date, Date | string, Date | string>;
export interface ProductSyncs {
created_at: Generated<Timestamp>;
ended_at: Timestamp | null;
has_failed: Generated<boolean>;
id: Generated<Int8>;
printful_product_id_filter: number[] | null;
started_at: Timestamp | null;
syncing_printful_product_ids: Generated<number[]>;
}
export interface DB {
product_syncs: ProductSyncs;
}