110 lines
3.9 KiB
Python
Executable File
110 lines
3.9 KiB
Python
Executable File
#!/usr/bin/env python3
|
||
|
||
import mariadb
|
||
import os
|
||
import sys
|
||
|
||
def get_external_db_connection():
|
||
"""Reads the external_server.conf file and returns a MariaDB database connection."""
|
||
# Get the instance folder path
|
||
current_dir = os.path.dirname(os.path.abspath(__file__))
|
||
instance_folder = os.path.join(current_dir, '../../instance')
|
||
settings_file = os.path.join(instance_folder, 'external_server.conf')
|
||
|
||
if not os.path.exists(settings_file):
|
||
raise FileNotFoundError(f"The external_server.conf file is missing: {settings_file}")
|
||
|
||
# Read settings from the configuration file
|
||
settings = {}
|
||
with open(settings_file, 'r') as f:
|
||
for line in f:
|
||
line = line.strip()
|
||
if line and '=' in line:
|
||
key, value = line.split('=', 1)
|
||
settings[key] = value
|
||
|
||
print(f"Connecting to MariaDB:")
|
||
print(f" Host: {settings.get('server_domain', 'N/A')}")
|
||
print(f" Port: {settings.get('port', 'N/A')}")
|
||
print(f" Database: {settings.get('database_name', 'N/A')}")
|
||
|
||
return mariadb.connect(
|
||
user=settings['username'],
|
||
password=settings['password'],
|
||
host=settings['server_domain'],
|
||
port=int(settings['port']),
|
||
database=settings['database_name']
|
||
)
|
||
|
||
def main():
|
||
try:
|
||
print("=== Adding Email Column to Users Table ===")
|
||
conn = get_external_db_connection()
|
||
cursor = conn.cursor()
|
||
|
||
# First, check the current table structure
|
||
print("\n1. Checking current table structure...")
|
||
cursor.execute("DESCRIBE users")
|
||
columns = cursor.fetchall()
|
||
|
||
has_email = False
|
||
for column in columns:
|
||
print(f" Column: {column[0]} ({column[1]})")
|
||
if column[0] == 'email':
|
||
has_email = True
|
||
|
||
if not has_email:
|
||
print("\n2. Adding email column...")
|
||
cursor.execute("ALTER TABLE users ADD COLUMN email VARCHAR(255)")
|
||
conn.commit()
|
||
print(" ✓ Email column added successfully")
|
||
else:
|
||
print("\n2. Email column already exists")
|
||
|
||
# Now check and display all users
|
||
print("\n3. Current users in database:")
|
||
cursor.execute("SELECT id, username, role, email FROM users")
|
||
users = cursor.fetchall()
|
||
|
||
if users:
|
||
print(f" Found {len(users)} users:")
|
||
for user in users:
|
||
email = user[3] if user[3] else "No email"
|
||
print(f" - ID: {user[0]}, Username: {user[1]}, Role: {user[2]}, Email: {email}")
|
||
else:
|
||
print(" No users found - creating test users...")
|
||
|
||
# Create some test users
|
||
test_users = [
|
||
('admin_user', 'admin123', 'admin', 'admin@company.com'),
|
||
('manager_user', 'manager123', 'manager', 'manager@company.com'),
|
||
('warehouse_user', 'warehouse123', 'warehouse_manager', 'warehouse@company.com'),
|
||
('quality_user', 'quality123', 'quality_manager', 'quality@company.com')
|
||
]
|
||
|
||
for username, password, role, email in test_users:
|
||
try:
|
||
cursor.execute("""
|
||
INSERT INTO users (username, password, role, email)
|
||
VALUES (%s, %s, %s, %s)
|
||
""", (username, password, role, email))
|
||
print(f" ✓ Created user: {username} ({role})")
|
||
except mariadb.IntegrityError as e:
|
||
print(f" âš User {username} already exists: {e}")
|
||
|
||
conn.commit()
|
||
print(" ✓ Test users created successfully")
|
||
|
||
conn.close()
|
||
print("\n=== Database Update Complete ===")
|
||
|
||
except Exception as e:
|
||
print(f"â<EFBFBD>Œ Error: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|
||
return 1
|
||
|
||
return 0
|
||
|
||
if __name__ == "__main__":
|
||
sys.exit(main()) |