mirror of
https://github.com/R0m1k3/CollectFlow.git
synced 2026-10-21 14:08:08 +02:00
feat: Add db-init.js script to initialize PostgreSQL database with ai_supplier_context and session_snapshots tables.
This commit is contained in:
1 parent
20aa71f71a
commit
e50335a8c2
1 file changed
+7
-15
+7
-15
@@ -1,16 +1,15 @@
|
|||||||
import { migrate } from "drizzle-orm/node-postgres/migrator";
|
const { Pool } = require('pg');
|
||||||
import { drizzle } from "drizzle-orm/node-postgres";
|
const fs = require('fs');
|
||||||
import { Pool } from "pg";
|
const path = require('path');
|
||||||
import * as schema from "../src/db/schema";
|
|
||||||
import fs from "fs";
|
const cwd = path.resolve(__dirname, '..'); // get app root when inside /scripts
|
||||||
import path from "path";
|
|
||||||
|
|
||||||
async function main() {
|
async function main() {
|
||||||
console.log("[DB Init] Starting database migration & initialization...");
|
console.log("[DB Init] Starting database migration & initialization...");
|
||||||
let connectionString = process.env.DATABASE_URL;
|
let connectionString = process.env.DATABASE_URL;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const CONFIG_PATH = path.join(process.cwd(), "data", ".db-config.json");
|
const CONFIG_PATH = path.join(cwd, "data", ".db-config.json");
|
||||||
if (fs.existsSync(CONFIG_PATH)) {
|
if (fs.existsSync(CONFIG_PATH)) {
|
||||||
const config = JSON.parse(fs.readFileSync(CONFIG_PATH, "utf-8"));
|
const config = JSON.parse(fs.readFileSync(CONFIG_PATH, "utf-8"));
|
||||||
if (config.url) {
|
if (config.url) {
|
||||||
@@ -29,15 +28,8 @@ async function main() {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const tempPool = new Pool({ connectionString, max: 1 });
|
const tempPool = new Pool({ connectionString, max: 1 });
|
||||||
const tempDb = drizzle(tempPool, { schema });
|
|
||||||
|
|
||||||
console.log("[DB Init] Connection established. Running migrations...");
|
console.log("[DB Init] Connection established. Running migrations via raw SQL...");
|
||||||
|
|
||||||
// Note: For Drizzle without pre-generated SQL migrations (using drizzle-kit push in dev),
|
|
||||||
// running migrations programmatically requires the generated sql files.
|
|
||||||
// Since we want to auto-create tables without requiring dev dependencies in production,
|
|
||||||
// we will create the specific missing table `ai_supplier_context` directly if it doesn't exist to ensure the app works.
|
|
||||||
// A full robust migration system would use `npx drizzle-kit push` but that is not available in the standalone build easily.
|
|
||||||
|
|
||||||
await tempPool.query(`
|
await tempPool.query(`
|
||||||
CREATE TABLE IF NOT EXISTS "ai_supplier_context" (
|
CREATE TABLE IF NOT EXISTS "ai_supplier_context" (
|
||||||
|
|||||||
Reference in new issue
Block a user