working on first setup of pg migrations
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
export default {
|
||||
name: 'create_collection_table',
|
||||
up: async () => {
|
||||
const sql = `
|
||||
CREATE TABLE collection (
|
||||
id UUID PRIMARY KEY,
|
||||
name TEXT
|
||||
);
|
||||
`
|
||||
await migrate.run([sql], pool)
|
||||
console.log('Collection table created successfully!')
|
||||
},
|
||||
down: async () => {
|
||||
const sql = `
|
||||
DROP TABLE collection;
|
||||
`
|
||||
await migrate.run([sql], pool)
|
||||
console.log('Collection table dropped successfully.')
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
export default {
|
||||
name: 'create_image_table',
|
||||
up: async () => {
|
||||
const sql = `
|
||||
CREATE TABLE image (
|
||||
id UUID PRIMARY KEY,
|
||||
file TEXT,
|
||||
image BYTEA
|
||||
);
|
||||
`
|
||||
await migrate.run([sql], pool)
|
||||
console.log('Image table created successfully!')
|
||||
},
|
||||
down: async () => {
|
||||
const sql = `
|
||||
DROP TABLE image;
|
||||
`
|
||||
await migrate.run([sql], pool)
|
||||
console.log('Image table dropped successfully.')
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
export default {
|
||||
up: async (pool) => {
|
||||
try {
|
||||
await pool.query(
|
||||
`
|
||||
CREATE TABLE item (
|
||||
id UUID PRIMARY KEY,
|
||||
collection_id UUID,
|
||||
image_id UUID,
|
||||
productId TEXT,
|
||||
name TEXT,
|
||||
cleanName TEXT,
|
||||
extCardText TEXTAREA,
|
||||
marketPrice TEXT,
|
||||
extRarity TEXT,
|
||||
FOREIGN KEY (collection_id) REFERENCES collection(id),
|
||||
FOREIGN KEY (image_id) REFERENCES image(id)
|
||||
);
|
||||
`
|
||||
)
|
||||
} catch (error) {
|
||||
console.error('Error creating item table:', error)
|
||||
throw error // Re-throw to signal failure to the migration runner
|
||||
}
|
||||
},
|
||||
down: async (pool) => {
|
||||
try {
|
||||
await pool.query(`
|
||||
DROP TABLE item;
|
||||
`)
|
||||
} catch (error) {
|
||||
console.error('Error dropping item table:', error)
|
||||
throw error
|
||||
}
|
||||
},
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
import { Pool } from 'pg'
|
||||
import * as fs from 'fs'
|
||||
import path from 'path'
|
||||
|
||||
const migrate = require('node-pg-migrate')
|
||||
|
||||
// Database connection configuration
|
||||
const dbConfig = {
|
||||
user: 'your_user',
|
||||
host: 'localhost',
|
||||
database: 'your_database',
|
||||
password: 'your_password',
|
||||
port: 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()
|
||||
+2
-1
@@ -25,7 +25,8 @@
|
||||
"dotenv": "^16.0.3",
|
||||
"express": "^4.18.2",
|
||||
"node-pg": "^1.0.1",
|
||||
"p-limit": "^7.3.0"
|
||||
"p-limit": "^7.3.0",
|
||||
"pg": "^8.20.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"eslint": "^10.3.0",
|
||||
|
||||
Reference in New Issue
Block a user