Geoeasy commited on
Commit
0da2ae2
·
verified ·
1 Parent(s): bddaf57

Delete 00_criar_bd_e_user_teste.py

Browse files
Files changed (1) hide show
  1. 00_criar_bd_e_user_teste.py +0 -120
00_criar_bd_e_user_teste.py DELETED
@@ -1,120 +0,0 @@
1
- #!/usr/bin/env python3
2
- """
3
- Script de inicialização da base de dados IA Mining
4
- Versão com USERNAME em vez de user_id
5
- """
6
-
7
- import sqlite3
8
- import bcrypt
9
- from pathlib import Path
10
-
11
- # Paths
12
- BASE_DIR = Path(__file__).parent
13
- DB_PATH = BASE_DIR / "db" / "app.db"
14
- DB_PATH.parent.mkdir(parents=True, exist_ok=True)
15
-
16
- print("=" * 60)
17
- print("IA MINING - Inicialização da Base de Dados")
18
- print("Versão: USERNAME (em vez de user_id)")
19
- print("=" * 60)
20
-
21
- # Conectar à BD
22
- conn = sqlite3.connect(DB_PATH)
23
- conn.execute("PRAGMA foreign_keys = ON") # Ativar foreign keys
24
- cur = conn.cursor()
25
-
26
- # Criar tabela users
27
- print("\n[1/5] Criando tabela 'users'...")
28
- cur.execute("""
29
- CREATE TABLE IF NOT EXISTS users (
30
- id INTEGER PRIMARY KEY AUTOINCREMENT,
31
- username TEXT UNIQUE NOT NULL,
32
- password_hash TEXT NOT NULL,
33
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
34
- )
35
- """)
36
- print("✅ Tabela 'users' criada")
37
-
38
- # Criar tabela tasks (com username em vez de user_id)
39
- print("\n[2/5] Criando tabela 'tasks' (com username)...")
40
- cur.execute("""
41
- CREATE TABLE IF NOT EXISTS tasks (
42
- id INTEGER PRIMARY KEY AUTOINCREMENT,
43
- username TEXT NOT NULL,
44
- task_code TEXT UNIQUE NOT NULL,
45
- task_name TEXT NOT NULL,
46
- task_date DATE NOT NULL,
47
- start_time TIMESTAMP NOT NULL,
48
- end_time TIMESTAMP NOT NULL,
49
- planned_hours REAL NOT NULL,
50
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
51
- FOREIGN KEY (username) REFERENCES users(username) ON DELETE CASCADE
52
- )
53
- """)
54
- print("✅ Tabela 'tasks' criada (FK: username)")
55
-
56
- # Criar tabela subtasks
57
- print("\n[3/5] Criando tabela 'subtasks'...")
58
- cur.execute("""
59
- CREATE TABLE IF NOT EXISTS subtasks (
60
- id INTEGER PRIMARY KEY AUTOINCREMENT,
61
- task_id INTEGER NOT NULL,
62
- subtask_code TEXT UNIQUE NOT NULL,
63
- subtask_name TEXT NOT NULL,
64
- start_time TIMESTAMP NOT NULL,
65
- end_time TIMESTAMP NOT NULL,
66
- planned_hours REAL NOT NULL,
67
- created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
68
- FOREIGN KEY (task_id) REFERENCES tasks(id) ON DELETE CASCADE
69
- )
70
- """)
71
- print("✅ Tabela 'subtasks' criada")
72
-
73
- # Criar tabela task_logs (com username)
74
- print("\n[4/5] Criando tabela 'task_logs' (com username)...")
75
- cur.execute("""
76
- CREATE TABLE IF NOT EXISTS task_logs (
77
- id INTEGER PRIMARY KEY AUTOINCREMENT,
78
- username TEXT NOT NULL,
79
- task_id INTEGER,
80
- activity TEXT NOT NULL,
81
- event_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
82
- FOREIGN KEY (username) REFERENCES users(username) ON DELETE CASCADE,
83
- FOREIGN KEY (task_id) REFERENCES tasks(id) ON DELETE CASCADE
84
- )
85
- """)
86
- print("✅ Tabela 'task_logs' criada")
87
-
88
- # Criar utilizador de teste
89
- print("\n[5/5] Criando utilizador de teste...")
90
- username = "admin"
91
- password = "admin123"
92
- pw_hash = bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
93
-
94
- try:
95
- cur.execute(
96
- "INSERT INTO users (username, password_hash) VALUES (?, ?)",
97
- (username, pw_hash)
98
- )
99
- print(f"✅ Utilizador de teste criado:")
100
- print(f" Username: {username}")
101
- print(f" Password: {password}")
102
- except sqlite3.IntegrityError:
103
- print(f"⚠️ Utilizador '{username}' já existe")
104
-
105
- # Commit e fechar
106
- conn.commit()
107
- conn.close()
108
-
109
- print("\n" + "=" * 60)
110
- print("✅ BASE DE DADOS INICIALIZADA COM SUCESSO!")
111
- print("=" * 60)
112
- print("\n📋 Estrutura:")
113
- print(" - tasks.username → FK para users.username")
114
- print(" - task_logs.username → FK para users.username")
115
- print(" - Códigos de tarefa: TAR_{username}_{seq}")
116
- print("\nPróximos passos:")
117
- print("1. Execute: python app.py")
118
- print("2. Aceda: http://localhost:7860")
119
- print(f"3. Faça login com: {username} / {password}")
120
- print("=" * 60)