import pg from 'pg' import { migrate } from 'postgres-schema-migrations'; import path, { dirname } from 'node:path'; import { fileURLToPath } from 'url'; import 'dotenv/config'; const { Client } = pg const __dirname = dirname(fileURLToPath(import.meta.url)); if (!process.env.DATABASE_PASSWORD) throw new Error('DATABASE_PASSWORD is missing in env'); /* * Here we set up a Foreign Data Wrapper which connects us to the old strapi database. * From this Strapi db futureporn-old, we migrate data to futureporn database. */ async function setupForeignDataWrapper(client) { // Run the SQL commands const sql = ` BEGIN; CREATE EXTENSION IF NOT EXISTS postgres_fdw; CREATE SERVER IF NOT EXISTS futureporn_old FOREIGN DATA WRAPPER postgres_fdw OPTIONS (dbname 'futureporn_old'); CREATE USER MAPPING IF NOT EXISTS FOR postgres SERVER futureporn_old OPTIONS (password_required 'true', password '${process.env.DATABASE_PASSWORD}'); COMMIT; `; await client.query(sql); console.log('Foreign Data Wrapper setup completed successfully.'); } async function main() { const dbConfig = { database: "futureporn", user: "postgres", password: process.env.DATABASE_PASSWORD, host: 'postgresql-primary.futureporn.svc.cluster.local', port: 5432, } const client = new Client(dbConfig) await client.connect() const migrateConfig = { client, ensureDatabaseExists: false, defaultDatabase: 'postgres' } try { await setupForeignDataWrapper(client) await migrate(migrateConfig, path.join(__dirname, "./migrations/"), { logger: console.log, schema: 'migrations_data' }) } finally { await client.end() } } await main()