File size: 1,616 Bytes
6ea020b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""Migration script to add user profile fields and userpermission table."""
import sqlite3

def migrate():
    conn = sqlite3.connect("lab_database.db")
    cursor = conn.cursor()
    
    # Check and add new columns to user table
    cursor.execute("PRAGMA table_info(user)")
    columns = [col[1] for col in cursor.fetchall()]
    
    new_columns = [
        ("date_of_birth", "TEXT"),
        ("phone_number", "TEXT"),
        ("address", "TEXT"),
        ("edited_by", "INTEGER"),
        ("edited_at", "TEXT"),
    ]
    
    for col_name, col_type in new_columns:
        if col_name not in columns:
            cursor.execute(f"ALTER TABLE user ADD COLUMN {col_name} {col_type}")
            print(f"  [+] Added column user.{col_name}")
        else:
            print(f"  [i] Column user.{col_name} already exists")
    
    # Create userpermission table
    cursor.execute("""
        CREATE TABLE IF NOT EXISTS userpermission (
            id INTEGER PRIMARY KEY AUTOINCREMENT,
            user_id INTEGER NOT NULL REFERENCES user(id),
            page_key TEXT NOT NULL,
            allowed_buttons TEXT
        )
    """)
    print("  [+] userpermission table created/verified")
    
    # Create index on user_id
    cursor.execute("CREATE INDEX IF NOT EXISTS idx_userpermission_user_id ON userpermission(user_id)")
    cursor.execute("CREATE INDEX IF NOT EXISTS idx_userpermission_page_key ON userpermission(page_key)")
    print("  [+] Indexes created/verified")
    
    conn.commit()
    conn.close()
    print("\n[+] User management migration complete!")

if __name__ == "__main__":
    migrate()