Files
tcg-collectors-server/migrations/index.js
T

45 lines
1.2 KiB
JavaScript

import { Pool } from 'pg'
import * as fs from 'fs'
import path from 'path'
const migrate = require('node-pg-migrate')
// Database connection configuration
const { DB_USERNAME, DB_HOST, DB_DATABASE, DB_PASSWORD, DB_PORT } = process.env;
const dbConfig = {
user: DB_USERNAME || 'your_user', // Use the environment variable if it exists, otherwise use 'your_user'
host: DB_HOST || 'localhost',
database: DB_DATABASE || 'your_database',
password: DB_PASSWORD || 'your_password',
port: parseInt(DB_PORT, 10) || 5432, // Convert the environment variable to a number if it exists, otherwise use 5432
}
const pool = new Pool(dbConfig)
const migrations = []
fs.readdirSync(__dirname)
.filter((file) => {
// Filter out hidden files, index.js itself, and non-JS files
return (
file.indexOf('.') !== 0 && file !== basename && file.slice(-3) === '.js'
)
})
.forEach((file) => {
// Require each file and add its default export to the list
const migration = require(path.join(__dirname, file))
migrations.push(migration)
})
async function runMigrations() {
try {
await migrate.run(migrations, pool)
console.log('Migrations completed successfully!')
} catch (error) {
console.error('Migration failed:', error)
}
}
runMigrations()