File size: 1,580 Bytes
dafba6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
```python
import sqlite3
from werkzeug.security import generate_password_hash

# Create SQLite database and tables
def init_db():
    conn = sqlite3.connect('reportninja.db')
    c = conn.cursor()
    
    # Users table
    c.execute('''CREATE TABLE IF NOT EXISTS users
                 (id INTEGER PRIMARY KEY AUTOINCREMENT,
                  email TEXT UNIQUE NOT NULL,
                  password TEXT NOT NULL,
                  name TEXT,
                  role TEXT DEFAULT 'user',
                  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)''')
    
    # Reports table
    c.execute('''CREATE TABLE IF NOT EXISTS reports
                 (id INTEGER PRIMARY KEY AUTOINCREMENT,
                  title TEXT NOT NULL,
                  description TEXT,
                  category TEXT,
                  status TEXT DEFAULT 'draft',
                  created_by INTEGER,
                  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
                  updated_at TIMESTAMP,
                  FOREIGN KEY(created_by) REFERENCES users(id))''')
    
    # Add admin user if not exists
    admin_email = "admin@reportninja.com"
    c.execute("SELECT id FROM users WHERE email=?", (admin_email,))
    if not c.fetchone():
        admin_password = generate_password_hash("admin123")
        c.execute("INSERT INTO users (email, password, name, role) VALUES (?, ?, ?, ?)",
                 (admin_email, admin_password, "Admin", "admin"))
    
    conn.commit()
    conn.close()

if __name__ == '__main__':
    init_db()
    print("Database initialized successfully!")
```