105 lines
3.8 KiB
Python
Executable File
105 lines
3.8 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 with settings:")
|
|
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')}")
|
|
print(f" Username: {settings.get('username', 'N/A')}")
|
|
|
|
# Create a database connection
|
|
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("=== Checking External MariaDB Database ===")
|
|
conn = get_external_db_connection()
|
|
cursor = conn.cursor()
|
|
|
|
# Create users table if it doesn't exist
|
|
print("\n1. Creating/verifying users table...")
|
|
cursor.execute('''
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
username VARCHAR(50) UNIQUE NOT NULL,
|
|
password VARCHAR(255) NOT NULL,
|
|
role VARCHAR(50) NOT NULL,
|
|
email VARCHAR(255)
|
|
)
|
|
''')
|
|
print(" ✓ Users table created/verified")
|
|
|
|
# Check existing users
|
|
print("\n2. Checking existing users...")
|
|
cursor.execute("SELECT id, username, role, email FROM users")
|
|
users = cursor.fetchall()
|
|
|
|
if users:
|
|
print(f" Found {len(users)} existing 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 in external database")
|
|
|
|
# Create some test users
|
|
print("\n3. Creating 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 Check Complete ===")
|
|
|
|
except Exception as e:
|
|
print(f"❌ Error: {e}")
|
|
return 1
|
|
|
|
return 0
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main()) |