From cf790c60df750371e41c2a159fc5d1844b2ba759 Mon Sep 17 00:00:00 2001 From: devilreef Date: Wed, 29 Apr 2026 12:14:17 +0600 Subject: [PATCH] feat: add drizzle schema for user_settings --- drizzle.config.ts | 9 +++ eslint.config.mjs | 2 +- src/db/migrate.ts | 6 ++ .../migrations/0000_equal_steel_serpent.sql | 6 ++ src/db/migrations/meta/0000_snapshot.json | 59 +++++++++++++++++++ src/db/migrations/meta/_journal.json | 13 ++++ src/db/schema.ts | 8 +++ src/lib/db.ts | 6 ++ 8 files changed, 108 insertions(+), 1 deletion(-) create mode 100644 drizzle.config.ts create mode 100644 src/db/migrate.ts create mode 100644 src/db/migrations/0000_equal_steel_serpent.sql create mode 100644 src/db/migrations/meta/0000_snapshot.json create mode 100644 src/db/migrations/meta/_journal.json create mode 100644 src/db/schema.ts create mode 100644 src/lib/db.ts diff --git a/drizzle.config.ts b/drizzle.config.ts new file mode 100644 index 0000000..0a2ce36 --- /dev/null +++ b/drizzle.config.ts @@ -0,0 +1,9 @@ +import process from 'node:process' +import { defineConfig } from 'drizzle-kit' + +export default defineConfig({ + schema: './src/db/schema.ts', + out: './src/db/migrations', + dialect: 'postgresql', + dbCredentials: { url: process.env.DATABASE_URL ?? '' }, +}) diff --git a/eslint.config.mjs b/eslint.config.mjs index f82c075..05fa42e 100644 --- a/eslint.config.mjs +++ b/eslint.config.mjs @@ -6,7 +6,7 @@ export default antfu({ quotes: 'single', }, typescript: true, - ignores: ['.omc'], + ignores: ['.omc', 'src/db/migrations'], rules: { 'no-console': 'off', 'antfu/no-top-level-await': 'off', diff --git a/src/db/migrate.ts b/src/db/migrate.ts new file mode 100644 index 0000000..0f67d6f --- /dev/null +++ b/src/db/migrate.ts @@ -0,0 +1,6 @@ +import { migrate } from 'drizzle-orm/node-postgres/migrator' +import { db, pool } from '@/lib/db.js' + +await migrate(db, { migrationsFolder: './src/db/migrations' }) +await pool.end() +console.log('migrations applied') diff --git a/src/db/migrations/0000_equal_steel_serpent.sql b/src/db/migrations/0000_equal_steel_serpent.sql new file mode 100644 index 0000000..d610d62 --- /dev/null +++ b/src/db/migrations/0000_equal_steel_serpent.sql @@ -0,0 +1,6 @@ +CREATE TABLE "user_settings" ( + "user_id" bigint PRIMARY KEY NOT NULL, + "version" integer DEFAULT 1 NOT NULL, + "data" jsonb DEFAULT '{}'::jsonb NOT NULL, + "updated_at" timestamp with time zone DEFAULT now() NOT NULL +); diff --git a/src/db/migrations/meta/0000_snapshot.json b/src/db/migrations/meta/0000_snapshot.json new file mode 100644 index 0000000..daafaa4 --- /dev/null +++ b/src/db/migrations/meta/0000_snapshot.json @@ -0,0 +1,59 @@ +{ + "id": "fe121b40-b985-49f0-a36c-b2955b766990", + "prevId": "00000000-0000-0000-0000-000000000000", + "version": "7", + "dialect": "postgresql", + "tables": { + "public.user_settings": { + "name": "user_settings", + "schema": "", + "columns": { + "user_id": { + "name": "user_id", + "type": "bigint", + "primaryKey": true, + "notNull": true + }, + "version": { + "name": "version", + "type": "integer", + "primaryKey": false, + "notNull": true, + "default": 1 + }, + "data": { + "name": "data", + "type": "jsonb", + "primaryKey": false, + "notNull": true, + "default": "'{}'::jsonb" + }, + "updated_at": { + "name": "updated_at", + "type": "timestamp with time zone", + "primaryKey": false, + "notNull": true, + "default": "now()" + } + }, + "indexes": {}, + "foreignKeys": {}, + "compositePrimaryKeys": {}, + "uniqueConstraints": {}, + "policies": {}, + "checkConstraints": {}, + "isRLSEnabled": false + } + }, + "enums": {}, + "schemas": {}, + "sequences": {}, + "roles": {}, + "policies": {}, + "views": {}, + "_meta": { + "columns": {}, + "schemas": {}, + "tables": {} + } +} diff --git a/src/db/migrations/meta/_journal.json b/src/db/migrations/meta/_journal.json new file mode 100644 index 0000000..e0df708 --- /dev/null +++ b/src/db/migrations/meta/_journal.json @@ -0,0 +1,13 @@ +{ + "version": "7", + "dialect": "postgresql", + "entries": [ + { + "idx": 0, + "version": "7", + "when": 1777423106134, + "tag": "0000_equal_steel_serpent", + "breakpoints": true + } + ] +} diff --git a/src/db/schema.ts b/src/db/schema.ts new file mode 100644 index 0000000..6dda4e5 --- /dev/null +++ b/src/db/schema.ts @@ -0,0 +1,8 @@ +import { bigint, integer, jsonb, pgTable, timestamp } from 'drizzle-orm/pg-core' + +export const userSettings = pgTable('user_settings', { + userId: bigint('user_id', { mode: 'bigint' }).primaryKey(), + version: integer('version').notNull().default(1), + data: jsonb('data').$type>().notNull().default({}), + updatedAt: timestamp('updated_at', { withTimezone: true }).notNull().defaultNow(), +}) diff --git a/src/lib/db.ts b/src/lib/db.ts new file mode 100644 index 0000000..4269258 --- /dev/null +++ b/src/lib/db.ts @@ -0,0 +1,6 @@ +import { drizzle } from 'drizzle-orm/node-postgres' +import { Pool } from 'pg' +import { config } from '@/shared/config.js' + +export const pool = new Pool({ connectionString: config.databaseUrl }) +export const db = drizzle(pool)