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:
@@ -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;');
|
||||
});
|
||||
};
|
||||
Reference in New Issue
Block a user