-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit_db.py
More file actions
130 lines (111 loc) · 5.83 KB
/
init_db.py
File metadata and controls
130 lines (111 loc) · 5.83 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os
import pyodbc
from werkzeug.security import generate_password_hash
from config import Config
def get_db_connection():
return pyodbc.connect(Config.DB_CONNECTION_STRING)
def init_db():
print("Initializing database...")
with get_db_connection() as conn:
cursor = conn.cursor()
# Check if users table has been created
cursor.execute("IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'users') SELECT 1 ELSE SELECT 0")
if cursor.fetchone()[0] == 0:
print("Creating users table...")
cursor.execute("""
CREATE TABLE users (
UserID INT IDENTITY(1,1) PRIMARY KEY,
Username VARCHAR(50) NOT NULL UNIQUE,
PasswordHash VARCHAR(255) NOT NULL,
Email VARCHAR(100) NOT NULL UNIQUE,
FirstName VARCHAR(50) NOT NULL,
LastName VARCHAR(50) NOT NULL,
IsActive BIT NOT NULL DEFAULT 1,
CreatedAt DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
)
""")
# Check if roles table exists
cursor.execute("IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'roles') SELECT 1 ELSE SELECT 0")
if cursor.fetchone()[0] == 0:
print("Creating roles table...")
cursor.execute("""
CREATE TABLE roles (
RoleID INT IDENTITY(1,1) PRIMARY KEY,
RoleName VARCHAR(50) NOT NULL UNIQUE,
Description VARCHAR(255) NULL
)
""")
# Insert default roles
default_roles = [
('Admin', 'Full access to all system features'),
('Staff', 'Can manage books, members, and loans'),
('Member', 'Regular library member'),
('Guest', 'Limited access')
]
for role in default_roles:
cursor.execute(
"INSERT INTO roles (RoleName, Description) VALUES (?, ?)",
role[0], role[1]
)
# Check if user_roles table exists
cursor.execute("IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'user_roles') SELECT 1 ELSE SELECT 0")
if cursor.fetchone()[0] == 0:
print("Creating user_roles table...")
cursor.execute("""
CREATE TABLE user_roles (
id INT IDENTITY(1,1) PRIMARY KEY,
user_id INT NOT NULL,
role_id INT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(UserID),
FOREIGN KEY (role_id) REFERENCES roles(RoleID),
CONSTRAINT UQ_UserRole UNIQUE (user_id, role_id)
)
""")
# Insert users if none exist
cursor.execute("SELECT COUNT(*) FROM users")
if cursor.fetchone()[0] == 0:
print("Inserting test users...")
# Define users and their roles
users_data = [
# [username, password, email, firstname, lastname, roles]
['admin', 'admin', 'admin@library.com', 'Admin', 'User', ['Admin']],
['jsmith', 'password123', 'john.smith@example.com', 'John', 'Smith', ['Staff']],
['mjones', 'password123', 'mary.jones@example.com', 'Mary', 'Jones', ['Staff']],
['dlee', 'password123', 'david.lee@example.com', 'David', 'Lee', ['Member']],
['swilson', 'password123', 'sarah.wilson@example.com', 'Sarah', 'Wilson', ['Member']],
['mjohnson', 'password123', 'michael.johnson@example.com', 'Michael', 'Johnson', ['Member']],
['rbrown', 'password123', 'robert.brown@example.com', 'Robert', 'Brown', ['Member']],
['pwilliams', 'password123', 'patricia.williams@example.com', 'Patricia', 'Williams', ['Member']],
['jgarcia', 'password123', 'james.garcia@example.com', 'James', 'Garcia', ['Guest']],
['lmartinez', 'password123', 'linda.martinez@example.com', 'Linda', 'Martinez', ['Guest']],
['rthomas', 'password123', 'richard.thomas@example.com', 'Richard', 'Thomas', ['Guest']],
['etaylor', 'password123', 'elizabeth.taylor@example.com', 'Elizabeth', 'Taylor', ['Guest']]
]
for user in users_data:
username, password, email, firstname, lastname, roles = user
# Hash the password
password_hash = generate_password_hash(password)
# Insert user
cursor.execute(
"INSERT INTO users (Username, PasswordHash, Email, FirstName, LastName, IsActive) "
"VALUES (?, ?, ?, ?, ?, ?)",
username, password_hash, email, firstname, lastname, 1
)
# Get the user ID
cursor.execute("SELECT UserID FROM users WHERE Username = ?", username)
user_id = cursor.fetchone()[0]
# Assign roles
for role_name in roles:
cursor.execute("SELECT RoleID FROM roles WHERE RoleName = ?", role_name)
role_id = cursor.fetchone()[0]
cursor.execute(
"INSERT INTO user_roles (user_id, role_id) VALUES (?, ?)",
user_id, role_id
)
conn.commit()
print("Users created successfully!")
else:
print("Users already exist, skipping user creation.")
print("Database initialization complete!")
if __name__ == "__main__":
init_db()