-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_database_struct.py
More file actions
47 lines (40 loc) · 1.82 KB
/
Copy pathcreate_database_struct.py
File metadata and controls
47 lines (40 loc) · 1.82 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
"""Database Structure Creation Script.
This script creates a database and sets up user permissions using MySQL.
It prompts for admin credentials and uses configuration settings to:
1. Create a new database (if it doesn't exist)
2. Create a database user (if it doesn't exist)
3. Grant necessary permissions to the created user
Requirements:
- MySQL server must be running and accessible
- Admin credentials with sufficient privileges
- Configuration file with database settings
Configuration needed:
- database_host: MySQL server host address
- database_name: Name of the database to create
- database_user: Username to create/configure
- database_pass: Password for the new user
Usage:
python create_database_struct.py
Note:
This script requires admin-level MySQL credentials to execute successfully.
It will prompt for username and password during execution.
"""
from configuration import DATABASE_SECTION, get_configuration
from getpass import getpass
from databases import MariaDb
if __name__ == "__main__":
config = get_configuration()
config = config[DATABASE_SECTION]
username = input("Enter username: ")
password = getpass()
with MariaDb(host=config['database_host'], user=username, password=password) as db:
db.create_database(config['database_name'], if_not_exists=True)
db.create_user(config['database_user'], config['database_pass'], if_not_exists=True)
if db.execute(
f"""
GRANT
SELECT, INSERT, UPDATE, DELETE,
CREATE, INDEX, DROP, ALTER, CREATE TEMPORARY TABLES, CREATE VIEW,
LOCK TABLES ON `{config['database_name']}`.* TO '{config['database_user']}'@'%'
""") is None:
raise RuntimeError