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.
This commit is contained in:
Hyungi Ahn
2025-12-19 09:43:09 +09:00
parent 9206672b63
commit b67362a733
28 changed files with 5665 additions and 9 deletions

View File

@@ -0,0 +1,49 @@
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;');
});
};