47 lines
1.8 KiB
Python
47 lines
1.8 KiB
Python
import sys
|
|
import os
|
|
|
|
# server 디렉토리를 path에 추가하여 database 패키지 임포트 허용
|
|
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
|
|
|
from database.connection import get_db_connection
|
|
|
|
def run_migration():
|
|
sql_script = """
|
|
SET FOREIGN_KEY_CHECKS = 0;
|
|
DROP TABLE IF EXISTS `machine_status`;
|
|
CREATE TABLE `machine_status` (
|
|
`equipment_id` int(11) NOT NULL,
|
|
`device_state` varchar(20) DEFAULT 'OFFLINE',
|
|
`m_area_raw` longtext DEFAULT NULL,
|
|
`cutting_motor_value` float DEFAULT 0.0,
|
|
`transfer_motor_value` float DEFAULT 0.0,
|
|
`chamber_temperature` float DEFAULT 0.0,
|
|
`d_area_raw` longtext DEFAULT NULL,
|
|
`timestamp` bigint(20) NOT NULL DEFAULT 0,
|
|
`last_updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
|
|
PRIMARY KEY (`equipment_id`),
|
|
CONSTRAINT `fk_machine_status_equipment` FOREIGN KEY (`equipment_id`) REFERENCES `equipment` (`id`) ON DELETE CASCADE
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
|
|
SET FOREIGN_KEY_CHECKS = 1;
|
|
"""
|
|
|
|
print("[MIGRATION] Connecting to MariaDB...")
|
|
try:
|
|
with get_db_connection() as conn:
|
|
with conn.cursor() as cursor:
|
|
# 다중 쿼리 실행을 위해 세미콜론 기준으로 분할하여 실행
|
|
queries = sql_script.split(';')
|
|
for query in queries:
|
|
clean_query = query.strip()
|
|
if clean_query:
|
|
print(f"[SQL] Executing: {clean_query[:60]}...")
|
|
cursor.execute(clean_query)
|
|
conn.commit()
|
|
print("[MIGRATION] Successfully updated machine_status schema in MariaDB!")
|
|
except Exception as e:
|
|
print(f"[ERROR] Migration failed: {e}")
|
|
|
|
if __name__ == "__main__":
|
|
run_migration()
|