Files
TK-FB-Project/api.hyungi.net/db/migrations/20251219003210_20251219_initial_schema.js
Hyungi Ahn b67362a733 feat: Introduce knex for database migrations
- Adds knex.js to manage database schema changes systematically.
- Creates an initial migration file based on `hyungi_schema_v2.sql` to represent the current database state.
- Adds npm scripts (`db:migrate`, `db:rollback`, etc.) for easy execution of migration tasks.
- Archives legacy SQL files and old migration scripts into the `db_archive/` directory to prevent confusion and clean up the project structure.
2025-12-19 09:43:09 +09:00

49 lines
1.3 KiB
JavaScript

const fs = require('fs');
const path = require('path');
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function(knex) {
const schemaSql = fs.readFileSync(path.join(__dirname, '../../hyungi_schema_v2.sql'), 'utf8');
return knex.raw(schemaSql);
};
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function(knex) {
// down 마이그레이션은 모든 테이블을 역순으로 삭제하도록 구현합니다.
const tables = [
'cutting_plans',
'daily_issue_reports',
'daily_work_reports',
'codes',
'code_types',
'factory_info',
'equipment_list',
'pipe_specs',
'tasks',
'worker_groups',
'workers',
'projects',
'password_change_logs',
'login_logs',
'users'
];
// 외래 키 제약 조건을 먼저 비활성화합니다.
return knex.raw('SET FOREIGN_KEY_CHECKS = 0;')
.then(() => {
// 각 테이블을 순회하며 drop table if exists를 실행합니다.
return tables.reduce((promise, tableName) => {
return promise.then(() => knex.schema.dropTableIfExists(tableName));
}, Promise.resolve());
})
.finally(() => {
// 외래 키 제약 조건을 다시 활성화합니다.
return knex.raw('SET FOREIGN_KEY_CHECKS = 1;');
});
};