-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_database.py
executable file
·118 lines (95 loc) · 3.87 KB
/
build_database.py
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
import os
from datetime import datetime, timezone
from werkzeug.security import generate_password_hash
import toml
from sqlalchemy import create_engine, text, inspect
from sqlalchemy.orm import sessionmaker
from sqlalchemy.exc import SQLAlchemyError
# Set the environment variable to determine the app configuration
os.environ['CONFIG'] = 'DEVELOPMENT'
from configuration.config import app, db
from application.models import User, Note, Friend, Role, Permission
def drop_all(engine):
inspector = inspect(engine)
# Disable foreign key checks
with engine.connect() as connection:
connection.execute(text('SET foreign_key_checks = 0;'))
try:
# Get all tables
tables = inspector.get_table_names()
# Drop all tables
for table in tables:
with engine.connect() as connection:
connection.execute(text(f'DROP TABLE IF EXISTS `{table}`;'))
except Exception as e:
print(f"Error dropping tables: {e}")
finally:
# Re-enable foreign key checks
with engine.connect() as connection:
connection.execute(text('SET foreign_key_checks = 1;'))
def create_role_if_not_exists(session, role_name):
role = session.query(Role).filter_by(name=role_name).first()
if not role:
role = Role(name=role_name)
session.add(role)
session.commit()
return role
def create_permission_if_not_exists(session, permission_name):
permission = session.query(Permission).filter_by(name=permission_name).first()
if not permission:
permission = Permission(name=permission_name)
session.add(permission)
session.commit()
return permission
def assign_permission_to_role(session, permission_name, role_name):
role = session.query(Role).filter_by(name=role_name).first()
permission = session.query(Permission).filter_by(name=permission_name).first()
if role and permission:
if not any(p.id == permission.id for p in role.permissions):
role.add_permission(permission)
session.commit()
def assign_role_to_user(session, username, role_name):
user = session.query(User).filter_by(username=username).first()
role = session.query(Role).filter_by(name=role_name).first()
if user and role:
if role not in user.roles:
user.roles.append(role)
session.commit()
def create_admin_users(session):
try:
# Load config from config file
with open('configuration/elevated_users.toml', 'r') as file:
config = toml.load(file)
# Initialize roles and permissions from config
for role_name in config['users']['elevated_users']:
create_role_if_not_exists(session, role_name)
for role_name, permissions in config['permissions'].items():
for permission_name in permissions:
create_permission_if_not_exists(session, permission_name)
assign_permission_to_role(session, permission_name, role_name)
# Create privileged admins from config
elevated_users = config['users']['elevated_users']
for i, username in enumerate(elevated_users, start=1):
admin_user = User(
id=i,
username=username,
password=generate_password_hash(username)
)
session.add(admin_user)
session.commit()
assign_role_to_user(session, username=username, role_name=username)
except SQLAlchemyError as e:
print(f"Error creating admin users: {e}")
# Main
with app.app_context():
engine = db.engine
Session = sessionmaker(bind=engine)
# Drop all tables
drop_all(engine)
# Create all tables
db.metadata.create_all(engine)
session = Session()
# Create and add admin users dynamically
create_admin_users(session)
session.close()
print("Database initialized successfully")