From 569a41f0539a7c0d9fcc7dfe4490d0b70e16fff6 Mon Sep 17 00:00:00 2001 From: Joseph Nelson Date: Tue, 12 May 2026 23:27:49 -0700 Subject: [PATCH] fixed things to be more inline with how migrations work --- .gitea/workflows/build.yaml | 6 +--- Dockerfile | 6 +--- migrations/001_create-collection-table.js | 24 +++++-------- migrations/002_create-image-table.js | 24 +++++-------- migrations/003_create-item-table.js | 27 ++++---------- migrations/004_create-location-table.js | 26 ++++---------- migrations/005_create-set-table.js | 26 ++++---------- migrations/006_create-user-table.js | 27 ++++---------- migrations/007_create-role-table.js | 27 ++++---------- migrations/index.js | 44 ----------------------- package.json | 2 +- 11 files changed, 54 insertions(+), 185 deletions(-) delete mode 100644 migrations/index.js diff --git a/.gitea/workflows/build.yaml b/.gitea/workflows/build.yaml index 89c0e96..e3d8804 100644 --- a/.gitea/workflows/build.yaml +++ b/.gitea/workflows/build.yaml @@ -25,11 +25,7 @@ jobs: - name: Build and Push Image run: | docker build \ - --build-arg DB_HOST=${{ secrets.DB_HOST }} \ - --build-arg DB_USERNAME=${{ secrets.DB_USERNAME }} \ - --build-arg DB_PASSWORD=${{ secrets.DB_PASSWORD }} \ - --build-arg DB_PORT=${{ secrets.DB_PORT }} \ - --build-arg DB_DATABASE=${{ secrets.DB_DATABASE }} \ + --build-arg DATABASE_URL=${{ secrets.DATABASE_URL }} \ -t gitea.nelson-household.com/hard-at-work/tcg-collectors-server/tcg-collectors-server:${{ github.sha }} . docker push gitea.nelson-household.com/hard-at-work/tcg-collectors-server/tcg-collectors-server:${{ github.sha }} diff --git a/Dockerfile b/Dockerfile index e302c85..6d9543a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,6 @@ FROM node:24-alpine -ENV DB_HOST= -ENV DB_USERNAME= -ENV DB_PASSWORD= -ENV DB_PORT= -ENV DB_DATABASE= +ENV DATABASE_URL= WORKDIR /app COPY . . diff --git a/migrations/001_create-collection-table.js b/migrations/001_create-collection-table.js index 873423a..9e001e2 100644 --- a/migrations/001_create-collection-table.js +++ b/migrations/001_create-collection-table.js @@ -1,20 +1,14 @@ -export default { - name: 'create_collection_table', - up: async () => { - const sql = ` +export const up = (pgm) => { + pgm.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.') - }, + `) +} + +export const down = (pgm) => { + pgm.sql(` + DROP TABLE collection; + `) } diff --git a/migrations/002_create-image-table.js b/migrations/002_create-image-table.js index e5cebe7..012dba8 100644 --- a/migrations/002_create-image-table.js +++ b/migrations/002_create-image-table.js @@ -1,21 +1,15 @@ -export default { - name: 'create_image_table', - up: async () => { - const sql = ` +export const up = (pgm) => { + pgm.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.') - }, + `) +} + +export const down = (pgm) => { + pgm.sql(` + DROP TABLE image; + `) } diff --git a/migrations/003_create-item-table.js b/migrations/003_create-item-table.js index 7425c9d..fc764f2 100644 --- a/migrations/003_create-item-table.js +++ b/migrations/003_create-item-table.js @@ -1,8 +1,5 @@ -export default { - up: async (pool) => { - try { - await pool.query( - ` +export const up = (pgm) => { + pgm.sql(` CREATE TABLE item ( id UUID PRIMARY KEY, collection_id UUID, @@ -16,21 +13,11 @@ export default { 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(` + `) +} + +export const down = (pgm) => { + pgm.sql(` DROP TABLE item; `) - } catch (error) { - console.error('Error dropping item table:', error) - throw error - } - }, } diff --git a/migrations/004_create-location-table.js b/migrations/004_create-location-table.js index ffdfaaf..f341d51 100644 --- a/migrations/004_create-location-table.js +++ b/migrations/004_create-location-table.js @@ -1,28 +1,14 @@ -export default { - up: async (pool) => { - try { - await pool.query( - ` +export const up = (pgm) => { + pgm.sql(` CREATE TABLE location ( id UUID PRIMARY KEY, name TEXT UNIQUE NOT NULL ); - ` - ) - } catch (error) { - console.error('Error creating location table:', error) - throw error - } - }, + `) +} - down: async (pool) => { - try { - await pool.query(` +export const down = (pgm) => { + pgm.sql(` DROP TABLE location; `) - } catch (error) { - console.error('Error dropping location table:', error) - throw error - } - }, } diff --git a/migrations/005_create-set-table.js b/migrations/005_create-set-table.js index 8d36b43..30a6b93 100644 --- a/migrations/005_create-set-table.js +++ b/migrations/005_create-set-table.js @@ -1,8 +1,5 @@ -export default { - up: async (pool) => { - try { - await pool.query( - ` +export const up = (pgm) => { + pgm.sql(` CREATE TABLE set ( id UUID PRIMARY KEY, collection_id UUID NOT NULL, @@ -13,22 +10,11 @@ export default { FOREIGN KEY (collection_id) REFERENCES collection(id), FOREIGN KEY (location_id) REFERENCES location(id) ); - ` - ) - } catch (error) { - console.error('Error creating set table:', error) - throw error - } - }, + `) +} - down: async (pool) => { - try { - await pool.query(` +export const down = (pgm) => { + pgm.sql(` DROP TABLE set; `) - } catch (error) { - console.error('Error dropping set table:', error) - throw error - } - }, } diff --git a/migrations/006_create-user-table.js b/migrations/006_create-user-table.js index a4a346c..8d0c1f6 100644 --- a/migrations/006_create-user-table.js +++ b/migrations/006_create-user-table.js @@ -1,8 +1,5 @@ -export default { - up: async (pool) => { - try { - await pool.query( - ` +export const up = (pgm) => { + pgm.sql(` CREATE TABLE user ( id UUID PRIMARY KEY, username TEXT UNIQUE NOT NULL, @@ -13,21 +10,11 @@ export default { updatedAt TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (role_id) REFERENCES role(id) ); - ` - ) - } catch (error) { - console.error('Error creating user table:', error) - throw error - } - }, - down: async (pool) => { - try { - await pool.query(` + `) +} + +export const down = (pgm) => { + pgm.sql(` DROP TABLE user; `) - } catch (error) { - console.error('Error dropping user table:', error) - throw error - } - }, } diff --git a/migrations/007_create-role-table.js b/migrations/007_create-role-table.js index fd49ccd..1cf4128 100644 --- a/migrations/007_create-role-table.js +++ b/migrations/007_create-role-table.js @@ -1,27 +1,14 @@ -export default { - up: async (pool) => { - try { - await pool.query( - ` +export const up = (pgm) => { + pgm.sql(` CREATE TABLE role ( id UUID PRIMARY KEY, name TEXT UNIQUE NOT NULL ); - ` - ) - } catch (error) { - console.error('Error creating role table:', error) - throw error - } - }, - down: async (pool) => { - try { - await pool.query(` + `) +} + +export const down = (pmg) => { + pgm.sql(` DROP TABLE role; `) - } catch (error) { - console.error('Error dropping role table:', error) - throw error - } - }, } diff --git a/migrations/index.js b/migrations/index.js deleted file mode 100644 index c895109..0000000 --- a/migrations/index.js +++ /dev/null @@ -1,44 +0,0 @@ -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() diff --git a/package.json b/package.json index 8f82aa3..b661d5b 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ "lint": "eslint .", "lint:fix": "eslint . --fix", "format": "prettier --config prettier.config.js --write .", - "migrate": "node ./migrations/index.js" + "migrate": "node-pg-migrate" }, "type": "module", "keywords": [