diff --git a/migrations/001_create-collection-table.js b/migrations/001_create-collection-table.js new file mode 100644 index 0000000..873423a --- /dev/null +++ b/migrations/001_create-collection-table.js @@ -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.') + }, +} diff --git a/migrations/002_create-image-table.js b/migrations/002_create-image-table.js new file mode 100644 index 0000000..e5cebe7 --- /dev/null +++ b/migrations/002_create-image-table.js @@ -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.') + }, +} diff --git a/migrations/003_create-item-table.js b/migrations/003_create-item-table.js new file mode 100644 index 0000000..7425c9d --- /dev/null +++ b/migrations/003_create-item-table.js @@ -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 + } + }, +} diff --git a/migrations/index.js b/migrations/index.js new file mode 100644 index 0000000..4fb9ce3 --- /dev/null +++ b/migrations/index.js @@ -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() diff --git a/package.json b/package.json index d7b0fde..09edf49 100644 --- a/package.json +++ b/package.json @@ -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",