fix: INSERT IGNORE in migration script, frontend port 9527

This commit is contained in:
2026-07-06 16:37:34 +08:00
parent 72f9a58838
commit 6c700c1c04

View File

@@ -17,7 +17,7 @@ FROM dorm_billing.rooms r
WHERE r.status != 'archived';
-- ── 3. students → students ──
INSERT INTO gongxue.students (id, name, phone, id_number, student_no, gender, ethnicity,
INSERT IGNORE INTO gongxue.students (id, name, phone, id_number, student_no, gender, ethnicity,
emergency_contact, emergency_phone, supervisor, status, tenant_id, department_id, created_at, updated_at)
SELECT s.id, s.name, NULLIF(s.phone, ''), NULLIF(s.id_number, ''),
NULL, -- 旧 dump 无 student_no
@@ -32,7 +32,7 @@ FROM dorm_billing.students s
WHERE s.status != 'archived';
-- ── 4. occupancies → occupancies ──
INSERT INTO gongxue.occupancies (id, student_id, room_id, check_in_date, check_out_date, billing_start_date, billing_end_date, check_out_reason, notes, rental_type, department_id, created_at)
INSERT IGNORE INTO gongxue.occupancies (id, student_id, room_id, check_in_date, check_out_date, billing_start_date, billing_end_date, check_out_reason, notes, rental_type, department_id, created_at)
SELECT o.id, o.student_id, o.room_id, o.check_in_date, o.check_out_date,
o.billing_start_date, o.billing_end_date, o.check_out_reason, o.notes,
'short', 1, o.created_at
@@ -41,7 +41,7 @@ WHERE EXISTS (SELECT 1 FROM gongxue.students WHERE id = o.student_id)
AND EXISTS (SELECT 1 FROM gongxue.rooms WHERE id = o.room_id);
-- ── 5. bills → bills ──
INSERT INTO gongxue.bills (id, student_id, period_start, period_end, shared_amount, personal_amount, total_amount, status, department_id, generated_at)
INSERT IGNORE INTO gongxue.bills (id, student_id, period_start, period_end, shared_amount, personal_amount, total_amount, status, department_id, generated_at)
SELECT b.id, b.student_id, b.period_start, b.period_end,
b.shared_amount, b.personal_amount, b.total_amount, b.status,
1, b.generated_at
@@ -49,39 +49,39 @@ FROM dorm_billing.bills b
WHERE EXISTS (SELECT 1 FROM gongxue.students WHERE id = b.student_id);
-- ── 6. bill_items → bill_items ──
INSERT INTO gongxue.bill_items (id, bill_id, room_id, expense_type, description, days, total_room_days, room_total_amount, student_amount)
INSERT IGNORE INTO gongxue.bill_items (id, bill_id, room_id, expense_type, description, days, total_room_days, room_total_amount, student_amount)
SELECT bi.id, bi.bill_id, bi.room_id, bi.expense_type, bi.description, bi.days, bi.total_room_days, bi.room_total_amount, bi.student_amount
FROM dorm_billing.bill_items bi
WHERE EXISTS (SELECT 1 FROM gongxue.bills WHERE id = bi.bill_id);
-- ── 7. deposits → deposits ──
INSERT INTO gongxue.deposits (id, student_id, amount, status, paid_date, refund_date, refund_amount, deduction_amount, deduction_reason, notes, recorded_by, department_id, created_at)
INSERT IGNORE INTO gongxue.deposits (id, student_id, amount, status, paid_date, refund_date, refund_amount, deduction_amount, deduction_reason, notes, recorded_by, department_id, created_at)
SELECT d.id, d.student_id, d.amount, d.status, d.paid_date, d.refund_date, d.refund_amount, d.deduction_amount, d.deduction_reason, d.notes, d.recorded_by, 1, d.created_at
FROM dorm_billing.deposits d
WHERE EXISTS (SELECT 1 FROM gongxue.students WHERE id = d.student_id);
-- ── 8. classrooms / classroom_rentals ──
INSERT INTO gongxue.classrooms (id, name, building, floor, capacity, room_type, course_type, supervisor, status, notes, department_id, created_at)
INSERT IGNORE INTO gongxue.classrooms (id, name, building, floor, capacity, room_type, course_type, supervisor, status, notes, department_id, created_at)
SELECT id, name, building, floor, capacity, room_type, course_type, supervisor, status, notes, 1, created_at
FROM dorm_billing.classrooms;
INSERT INTO gongxue.classroom_rentals (id, classroom_id, tenant_id, start_date, end_date, contract_path, contract_original_name, daily_rate, total_amount, status, notes, created_by, department_id, created_at, updated_at)
INSERT IGNORE INTO gongxue.classroom_rentals (id, classroom_id, tenant_id, start_date, end_date, contract_path, contract_original_name, daily_rate, total_amount, status, notes, created_by, department_id, created_at, updated_at)
SELECT id, classroom_id, tenant_id, start_date, end_date, contract_path, contract_original_name, daily_rate, total_amount, status, notes, created_by, 1, created_at, updated_at
FROM dorm_billing.classroom_rentals;
-- ── 9. room_expenses / personal_expenses ──
INSERT INTO gongxue.room_expenses (id, room_id, expense_type, amount, period_start, period_end, description, recorded_by, department_id, created_at)
INSERT IGNORE INTO gongxue.room_expenses (id, room_id, expense_type, amount, period_start, period_end, description, recorded_by, department_id, created_at)
SELECT id, room_id, expense_type, amount, period_start, period_end, description, recorded_by, 1, created_at
FROM dorm_billing.room_expenses
WHERE EXISTS (SELECT 1 FROM gongxue.rooms WHERE id = room_id);
INSERT INTO gongxue.personal_expenses (id, student_id, room_id, expense_type, amount, expense_date, description, recorded_by, department_id, created_at)
INSERT IGNORE INTO gongxue.personal_expenses (id, student_id, room_id, expense_type, amount, expense_date, description, recorded_by, department_id, created_at)
SELECT id, student_id, room_id, expense_type, amount, expense_date, description, recorded_by, 1, created_at
FROM dorm_billing.personal_expenses
WHERE EXISTS (SELECT 1 FROM gongxue.students WHERE id = student_id);
-- ── 10. users → users保留原始 bcrypt hash ──
INSERT INTO gongxue.users (id, username, password_hash, name, is_active, last_login_at, created_at, updated_at)
INSERT IGNORE INTO gongxue.users (id, username, password_hash, name, is_active, last_login_at, created_at, updated_at)
SELECT u.id, u.username, u.password_hash,
COALESCE(NULLIF(u.name, ''), u.username),
u.is_active, u.last_login_at,
@@ -93,7 +93,7 @@ INSERT IGNORE INTO gongxue.user_departments (user_id, department_id, is_default)
SELECT u.id, 1, 1 FROM gongxue.users u;
-- ── 12. operation_logs → operation_logs ──
INSERT INTO gongxue.operation_logs (id, user_id, username, module, action, target_id, target_type, detail, ip_address, user_agent, status, created_at)
INSERT IGNORE INTO gongxue.operation_logs (id, user_id, username, module, action, target_id, target_type, detail, ip_address, user_agent, status, created_at)
SELECT id, user_id, username, module, action, target_id, target_type, detail, ip_address, user_agent, status, created_at
FROM dorm_billing.operation_logs;