Working base server with migrations and build steps ironed out #1
@@ -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 }}
|
||||
|
||||
|
||||
+1
-5
@@ -1,10 +1,6 @@
|
||||
FROM node:24-alpine
|
||||
|
||||
ENV DB_HOST=<your_db_host>
|
||||
ENV DB_USERNAME=<your_db_username>
|
||||
ENV DB_PASSWORD=<your_db_password>
|
||||
ENV DB_PORT=<your_db_port>
|
||||
ENV DB_DATABASE=<your_db_database>
|
||||
ENV DATABASE_URL=<the database url>
|
||||
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
|
||||
@@ -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;
|
||||
`)
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
`)
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
@@ -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
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
+1
-1
@@ -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": [
|
||||
|
||||
Reference in New Issue
Block a user