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
+31
View File
@@ -0,0 +1,31 @@
import {
DummyDriver,
PostgresAdapter,
PostgresIntrospector,
PostgresQueryCompiler,
} from 'kysely'
import { defineConfig } from 'kysely-ctl'
export default defineConfig({
dialect: {
createAdapter() {
return new PostgresAdapter()
},
createDriver() {
return new DummyDriver()
},
createIntrospector(db) {
return new PostgresIntrospector(db)
},
createQueryCompiler() {
return new PostgresQueryCompiler()
},
},
migrations: {
migrationFolder: "src/database/migrations",
},
// plugins: [],
// seeds: {
// seedFolder: "seeds",
// }
})
+5 -3
View File
@@ -11,7 +11,8 @@
"scripts": {
"dev": "bun run --hot src/index.ts",
"start": "bun run src/index.ts",
"build": "bun build --compile --minify-whitespace --minify-syntax --target bun-linux-x64 --outfile dist/server src/index.ts"
"build": "bun build --compile --minify-whitespace --minify-syntax --target bun-linux-x64 --outfile dist/server src/index.ts",
"db:codegen": "bunx kysely-codegen --env-file .env.development --out-file ./src/database/types.d.ts"
},
"dependencies": {
"@blade-and-brawn/calculator": "workspace:*",
@@ -29,7 +30,8 @@
"zipcodes-us": "^1.1.3"
},
"devDependencies": {
"pino-pretty": "^13.1.3",
"kysely-codegen": "^0.20.0"
"kysely-codegen": "^0.20.0",
"kysely-ctl": "^0.21.0",
"pino-pretty": "^13.1.3"
}
}
+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;
}
+2 -2
View File
@@ -3,7 +3,7 @@
"type": "module",
"devDependencies": {
"@blade-and-brawn/api": "workspace:*",
"@sveltejs/kit": "^2.69.0",
"@sveltejs/kit": "^2.69.1",
"@sveltejs/vite-plugin-svelte": "^6.2.4",
"@types/bun": "^1.3.14",
"elysia": "^1.4.29",
@@ -26,7 +26,7 @@
"@blade-and-brawn/domain": "workspace:*",
"@elysia/eden": "^1.4.10",
"@tailwindcss/vite": "^4.3.2",
"daisyui": "^5.6.7",
"daisyui": "^5.6.10",
"jose": "^6.2.3",
"tailwindcss": "^4.3.2"
}