fix: add automatic database migrations on startup and copy drizzle folder to Docker
- Auto-run migrations when db module loads - Copy drizzle migrations folder to Docker image - Generate missing color column migration - Fixes 500 errors on /api/candles and /api/annotations in Docker
This commit is contained in:
parent
4e13648ebf
commit
6f965c1cc9
2 changed files with 13 additions and 0 deletions
|
|
@ -28,6 +28,9 @@ COPY --from=builder --chown=nextjs:nodejs /app/.next/static ./.next/static
|
||||||
# Copy node_modules for native dependencies like better-sqlite3
|
# Copy node_modules for native dependencies like better-sqlite3
|
||||||
COPY --from=builder --chown=nextjs:nodejs /app/node_modules ./node_modules
|
COPY --from=builder --chown=nextjs:nodejs /app/node_modules ./node_modules
|
||||||
|
|
||||||
|
# Copy drizzle migrations
|
||||||
|
COPY --from=builder --chown=nextjs:nodejs /app/drizzle ./drizzle
|
||||||
|
|
||||||
RUN mkdir -p /app/public /app/data && chown -R nextjs:nodejs /app/public /app/data
|
RUN mkdir -p /app/public /app/data && chown -R nextjs:nodejs /app/public /app/data
|
||||||
|
|
||||||
ENV NODE_ENV=production PORT=3000 HOSTNAME=0.0.0.0
|
ENV NODE_ENV=production PORT=3000 HOSTNAME=0.0.0.0
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import Database from 'better-sqlite3';
|
import Database from 'better-sqlite3';
|
||||||
import { drizzle } from 'drizzle-orm/better-sqlite3';
|
import { drizzle } from 'drizzle-orm/better-sqlite3';
|
||||||
|
import { migrate } from 'drizzle-orm/better-sqlite3/migrator';
|
||||||
import * as schema from './schema';
|
import * as schema from './schema';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
|
@ -13,3 +14,12 @@ if (!fs.existsSync(dataDir)) {
|
||||||
const dbPath = path.join(dataDir, 'candles.db');
|
const dbPath = path.join(dataDir, 'candles.db');
|
||||||
const sqlite = new Database(dbPath);
|
const sqlite = new Database(dbPath);
|
||||||
export const db = drizzle(sqlite, { schema });
|
export const db = drizzle(sqlite, { schema });
|
||||||
|
|
||||||
|
// Run migrations on startup
|
||||||
|
try {
|
||||||
|
migrate(db, { migrationsFolder: path.join(process.cwd(), 'drizzle') });
|
||||||
|
console.log('✅ Database migrations completed');
|
||||||
|
} catch (error) {
|
||||||
|
console.error('❌ Migration failed:', error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue