UNPKG

exclaim-recovery-database

Version:

Shared database schemas for Exclaim Recovery platform

173 lines (153 loc) 5.43 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); require("dotenv/config"); const path_1 = require("path"); const promises_1 = require("fs/promises"); async function main() { // Get connection string from environment const connectionString = process.env.VERCEL_POSTGRES_URL; if (!connectionString) { console.error('No VERCEL_POSTGRES_URL found in environment'); process.exit(1); } // Define the migrations folder path const migrationsFolder = (0, path_1.resolve)(__dirname, './migrations'); try { // Ensure the migrations folder exists await (0, promises_1.mkdir)(migrationsFolder, { recursive: true }); // Generate a timestamp for the migration const timestamp = new Date().toISOString().replace(/[-:.TZ]/g, ''); const filename = `${timestamp}_create_vector_tables.sql`; const filepath = (0, path_1.resolve)(migrationsFolder, filename); // Create migration SQL content const migrationContent = ` -- Create pgvector extension CREATE EXTENSION IF NOT EXISTS vector; -- Create SMARTRecoveryDocument table CREATE TABLE IF NOT EXISTS "SMARTRecoveryDocument" ( "id" UUID PRIMARY KEY DEFAULT gen_random_uuid(), "content" TEXT NOT NULL, "metadata" JSONB, "createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP, "vector" vector(1536) ); -- Create ScientificStudyDocument table CREATE TABLE IF NOT EXISTS "ScientificStudyDocument" ( "id" UUID PRIMARY KEY DEFAULT gen_random_uuid(), "title" TEXT, "abstract" TEXT, "content" TEXT NOT NULL, "authors" TEXT[], "publicationYear" TEXT, "journal" TEXT, "doi" TEXT, "metadata" JSONB, "createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP, "vector" vector(1536) ); -- Create Document table CREATE TABLE IF NOT EXISTS "Document" ( "id" UUID PRIMARY KEY DEFAULT gen_random_uuid(), "content" TEXT NOT NULL, "metadata" JSONB, "createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP, "vector" vector(1536) ); -- Create UserDocument table CREATE TABLE IF NOT EXISTS "UserDocument" ( "id" UUID PRIMARY KEY DEFAULT gen_random_uuid(), "userId" TEXT NOT NULL, "documentId" TEXT NOT NULL, "metadata" JSONB, "createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP, "vector" vector(1536) ); -- Create GAStepDocument table CREATE TABLE IF NOT EXISTS "GAStepDocument" ( "id" UUID PRIMARY KEY DEFAULT gen_random_uuid(), "stepNumber" INTEGER NOT NULL, "title" TEXT NOT NULL, "description" TEXT NOT NULL, "detailedContent" TEXT NOT NULL, "reflectionPrompts" TEXT[], "commonChallenges" TEXT[], "milestoneNames" TEXT[], "metadata" JSONB, "createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP, "updatedAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP, "vector" vector(1536) ); -- Create GAResourceDocument table CREATE TABLE IF NOT EXISTS "GAResourceDocument" ( "id" UUID PRIMARY KEY DEFAULT gen_random_uuid(), "stepNumber" INTEGER, "title" TEXT NOT NULL, "type" TEXT NOT NULL, "url" TEXT, "content" TEXT, "metadata" JSONB, "createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP, "vector" vector(1536) ); -- Create GAReflectionDocument table CREATE TABLE IF NOT EXISTS "GAReflectionDocument" ( "id" UUID PRIMARY KEY DEFAULT gen_random_uuid(), "userId" TEXT NOT NULL, "stepNumber" INTEGER NOT NULL, "content" TEXT NOT NULL, "isPrivate" BOOLEAN DEFAULT FALSE, "tags" TEXT[], "createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP, "updatedAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP, "vector" vector(1536) ); -- Create vector indexes CREATE INDEX IF NOT EXISTS "smart_recovery_vector_idx" ON "SMARTRecoveryDocument" USING hnsw ("vector" vector_cosine_ops); CREATE INDEX IF NOT EXISTS "scientific_study_vector_idx" ON "ScientificStudyDocument" USING hnsw ("vector" vector_cosine_ops); CREATE INDEX IF NOT EXISTS "document_vector_idx" ON "Document" USING hnsw ("vector" vector_cosine_ops); CREATE INDEX IF NOT EXISTS "user_document_vector_idx" ON "UserDocument" USING hnsw ("vector" vector_cosine_ops); CREATE INDEX IF NOT EXISTS "ga_step_vector_idx" ON "GAStepDocument" USING hnsw ("vector" vector_cosine_ops); CREATE INDEX IF NOT EXISTS "ga_resource_vector_idx" ON "GAResourceDocument" USING hnsw ("vector" vector_cosine_ops); CREATE INDEX IF NOT EXISTS "ga_reflection_vector_idx" ON "GAReflectionDocument" USING hnsw ("vector" vector_cosine_ops); -- Create additional indexes for faster filtering CREATE INDEX IF NOT EXISTS "ga_step_number_idx" ON "GAStepDocument" ("stepNumber"); CREATE INDEX IF NOT EXISTS "ga_resource_step_idx" ON "GAResourceDocument" ("stepNumber"); CREATE INDEX IF NOT EXISTS "ga_resource_type_idx" ON "GAResourceDocument" ("type"); CREATE INDEX IF NOT EXISTS "ga_reflection_user_idx" ON "GAReflectionDocument" ("userId"); CREATE INDEX IF NOT EXISTS "ga_reflection_step_idx" ON "GAReflectionDocument" ("stepNumber"); CREATE INDEX IF NOT EXISTS "user_document_userId_idx" ON "UserDocument" ("userId"); `; // Write the migration file await (0, promises_1.writeFile)(filepath, migrationContent); console.log(`Generated migration file: ${filepath}`); console.log('To apply this migration, run: npm run migrate'); } catch (error) { console.error('Error generating migration:', error); } } main().catch(err => { console.error('Error in generate-migrations script:', err); process.exit(1); }); //# sourceMappingURL=generate-migrations.js.map