06/05 Optimize app: logs will be saved to db
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Migration: create audit_logs and app_logs tables.
|
||||
|
||||
Run once: python scripts/add_log_tables.py
|
||||
Safe to re-run — uses CREATE TABLE IF NOT EXISTS.
|
||||
"""
|
||||
import sys, os
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from app import create_app
|
||||
from app.extensions import db
|
||||
|
||||
app = create_app()
|
||||
|
||||
TABLES = [
|
||||
(
|
||||
'audit_logs',
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS audit_logs (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
timestamp DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
action VARCHAR(64) NOT NULL,
|
||||
description VARCHAR(255),
|
||||
ip_address VARCHAR(45),
|
||||
INDEX ix_audit_logs_timestamp (timestamp),
|
||||
INDEX ix_audit_logs_action (action)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
|
||||
"""
|
||||
),
|
||||
(
|
||||
'app_logs',
|
||||
"""
|
||||
CREATE TABLE IF NOT EXISTS app_logs (
|
||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||
timestamp DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
level VARCHAR(16) NOT NULL,
|
||||
module VARCHAR(128),
|
||||
message TEXT,
|
||||
INDEX ix_app_logs_timestamp (timestamp),
|
||||
INDEX ix_app_logs_level (level)
|
||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4
|
||||
"""
|
||||
),
|
||||
]
|
||||
|
||||
with app.app_context():
|
||||
with db.engine.connect() as conn:
|
||||
for name, sql in TABLES:
|
||||
conn.execute(db.text(sql))
|
||||
conn.commit()
|
||||
print(f' {name} — created (or already exists).')
|
||||
|
||||
print('Done.')
|
||||
Reference in New Issue
Block a user