feat(14-01): rewrite database foundation from SQLite to PostgreSQL

- Replace all 13 sqliteTable definitions with pgTable (pg-core)
- Convert integer timestamps to native timestamp type with defaultNow()
- Convert real columns to doublePrecision, integer used to boolean
- Rewrite db connection to use postgres.js driver with DATABASE_URL
- Rewrite migrate.ts to use postgres-js migrator targeting drizzle-pg/
- Convert seed.ts to async
- Update drizzle.config.ts to postgresql dialect
- Install postgres and @electric-sql/pglite, remove better-sqlite3
This commit is contained in:
2026-04-04 12:17:05 +02:00
parent f7048a267a
commit 3724cf8348
7 changed files with 90 additions and 105 deletions

View File

@@ -1,9 +1,9 @@
import { Database } from "bun:sqlite";
import { drizzle } from "drizzle-orm/bun-sqlite";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";
import * as schema from "./schema.ts";
const sqlite = new Database(process.env.DATABASE_PATH || "gearbox.db");
sqlite.run("PRAGMA journal_mode = WAL");
sqlite.run("PRAGMA foreign_keys = ON");
export const db = drizzle(sqlite, { schema });
const connectionString =
process.env.DATABASE_URL ||
"postgresql://gearbox:gearbox@localhost:5432/gearbox";
const queryClient = postgres(connectionString);
export const db = drizzle(queryClient, { schema });

View File

@@ -1,13 +1,14 @@
import { Database } from "bun:sqlite";
import { drizzle } from "drizzle-orm/bun-sqlite";
import { migrate } from "drizzle-orm/bun-sqlite/migrator";
import { drizzle } from "drizzle-orm/postgres-js";
import { migrate } from "drizzle-orm/postgres-js/migrator";
import postgres from "postgres";
const sqlite = new Database(process.env.DATABASE_PATH || "gearbox.db");
sqlite.run("PRAGMA journal_mode = WAL");
sqlite.run("PRAGMA foreign_keys = ON");
const connectionString =
process.env.DATABASE_URL ||
"postgresql://gearbox:gearbox@localhost:5432/gearbox";
const migrationClient = postgres(connectionString, { max: 1 });
const db = drizzle(migrationClient);
const db = drizzle(sqlite);
migrate(db, { migrationsFolder: "./drizzle" });
await migrate(db, { migrationsFolder: "./drizzle-pg" });
await migrationClient.end();
sqlite.close();
console.log("Migrations applied successfully");

View File

@@ -1,18 +1,24 @@
import { integer, real, sqliteTable, text } from "drizzle-orm/sqlite-core";
import {
boolean,
doublePrecision,
integer,
pgTable,
serial,
text,
timestamp,
} from "drizzle-orm/pg-core";
export const categories = sqliteTable("categories", {
id: integer("id").primaryKey({ autoIncrement: true }),
export const categories = pgTable("categories", {
id: serial("id").primaryKey(),
name: text("name").notNull().unique(),
icon: text("icon").notNull().default("package"),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
createdAt: timestamp("created_at").notNull().defaultNow(),
});
export const items = sqliteTable("items", {
id: integer("id").primaryKey({ autoIncrement: true }),
export const items = pgTable("items", {
id: serial("id").primaryKey(),
name: text("name").notNull(),
weightGrams: real("weight_grams"),
weightGrams: doublePrecision("weight_grams"),
priceCents: integer("price_cents"),
categoryId: integer("category_id")
.notNull()
@@ -22,37 +28,29 @@ export const items = sqliteTable("items", {
imageFilename: text("image_filename"),
imageSourceUrl: text("image_source_url"),
quantity: integer("quantity").notNull().default(1),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
});
export const threads = sqliteTable("threads", {
id: integer("id").primaryKey({ autoIncrement: true }),
export const threads = pgTable("threads", {
id: serial("id").primaryKey(),
name: text("name").notNull(),
status: text("status").notNull().default("active"),
resolvedCandidateId: integer("resolved_candidate_id"),
categoryId: integer("category_id")
.notNull()
.references(() => categories.id),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
});
export const threadCandidates = sqliteTable("thread_candidates", {
id: integer("id").primaryKey({ autoIncrement: true }),
export const threadCandidates = pgTable("thread_candidates", {
id: serial("id").primaryKey(),
threadId: integer("thread_id")
.notNull()
.references(() => threads.id, { onDelete: "cascade" }),
name: text("name").notNull(),
weightGrams: real("weight_grams"),
weightGrams: doublePrecision("weight_grams"),
priceCents: integer("price_cents"),
categoryId: integer("category_id")
.notNull()
@@ -64,28 +62,20 @@ export const threadCandidates = sqliteTable("thread_candidates", {
status: text("status").notNull().default("researching"),
pros: text("pros"),
cons: text("cons"),
sortOrder: real("sort_order").notNull().default(0),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
sortOrder: doublePrecision("sort_order").notNull().default(0),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
});
export const setups = sqliteTable("setups", {
id: integer("id").primaryKey({ autoIncrement: true }),
export const setups = pgTable("setups", {
id: serial("id").primaryKey(),
name: text("name").notNull(),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").notNull().defaultNow(),
});
export const setupItems = sqliteTable("setup_items", {
id: integer("id").primaryKey({ autoIncrement: true }),
export const setupItems = pgTable("setup_items", {
id: serial("id").primaryKey(),
setupId: integer("setup_id")
.notNull()
.references(() => setups.id, { onDelete: "cascade" }),
@@ -95,69 +85,59 @@ export const setupItems = sqliteTable("setup_items", {
classification: text("classification").notNull().default("base"),
});
export const settings = sqliteTable("settings", {
export const settings = pgTable("settings", {
key: text("key").primaryKey(),
value: text("value").notNull(),
});
export const users = sqliteTable("users", {
id: integer("id").primaryKey({ autoIncrement: true }),
export const users = pgTable("users", {
id: serial("id").primaryKey(),
username: text("username").notNull().unique(),
passwordHash: text("password_hash").notNull(),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
createdAt: timestamp("created_at").notNull().defaultNow(),
});
export const sessions = sqliteTable("sessions", {
export const sessions = pgTable("sessions", {
id: text("id").primaryKey(),
userId: integer("user_id")
.notNull()
.references(() => users.id, { onDelete: "cascade" }),
expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(),
expiresAt: timestamp("expires_at").notNull(),
});
export const apiKeys = sqliteTable("api_keys", {
id: integer("id").primaryKey({ autoIncrement: true }),
export const apiKeys = pgTable("api_keys", {
id: serial("id").primaryKey(),
name: text("name").notNull(),
keyHash: text("key_hash").notNull(),
keyPrefix: text("key_prefix").notNull(),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
createdAt: timestamp("created_at").notNull().defaultNow(),
});
export const oauthClients = sqliteTable("oauth_clients", {
id: integer("id").primaryKey({ autoIncrement: true }),
export const oauthClients = pgTable("oauth_clients", {
id: serial("id").primaryKey(),
clientId: text("client_id").notNull().unique(),
clientName: text("client_name"),
redirectUris: text("redirect_uris").notNull(), // JSON array
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
createdAt: timestamp("created_at").notNull().defaultNow(),
});
export const oauthCodes = sqliteTable("oauth_codes", {
id: integer("id").primaryKey({ autoIncrement: true }),
export const oauthCodes = pgTable("oauth_codes", {
id: serial("id").primaryKey(),
code: text("code").notNull().unique(),
clientId: text("client_id").notNull(),
codeChallenge: text("code_challenge").notNull(),
codeChallengeMethod: text("code_challenge_method").notNull().default("S256"),
redirectUri: text("redirect_uri").notNull(),
expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(),
used: integer("used").notNull().default(0),
expiresAt: timestamp("expires_at").notNull(),
used: boolean("used").notNull().default(false),
});
export const oauthTokens = sqliteTable("oauth_tokens", {
id: integer("id").primaryKey({ autoIncrement: true }),
export const oauthTokens = pgTable("oauth_tokens", {
id: serial("id").primaryKey(),
accessTokenHash: text("access_token_hash").notNull().unique(),
refreshTokenHash: text("refresh_token_hash").notNull().unique(),
clientId: text("client_id").notNull(),
expiresAt: integer("expires_at", { mode: "timestamp" }).notNull(), // access token expiry
refreshExpiresAt: integer("refresh_expires_at", {
mode: "timestamp",
}).notNull(), // refresh token expiry
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.$defaultFn(() => new Date()),
expiresAt: timestamp("expires_at").notNull(), // access token expiry
refreshExpiresAt: timestamp("refresh_expires_at").notNull(), // refresh token expiry
createdAt: timestamp("created_at").notNull().defaultNow(),
});

View File

@@ -1,14 +1,12 @@
import { db } from "./index.ts";
import { categories } from "./schema.ts";
export function seedDefaults() {
const existing = db.select().from(categories).all();
export async function seedDefaults() {
const existing = await db.select().from(categories);
if (existing.length === 0) {
db.insert(categories)
.values({
name: "Uncategorized",
icon: "package",
})
.run();
await db.insert(categories).values({
name: "Uncategorized",
icon: "package",
});
}
}