diff --git a/docker-compose.yml b/docker-compose.yml index 7bab0ba..77d1b0b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -14,7 +14,7 @@ services: ports: - "${DB_PORT:-3306}:3306" volumes: - - mariadb_data:/var/lib/mysql + - /srv/docker-test/mariadb:/var/lib/mysql - ./init-db.sql:/docker-entrypoint-initdb.d/01-init.sql networks: - recticel-network @@ -54,9 +54,12 @@ services: - "${APP_PORT:-8781}:8781" volumes: # Mount logs directory for persistence - - ./logs:/srv/quality_recticel/logs - # Mount instance directory for config persistence (optional) - - ./instance:/app/instance + - /srv/docker-test/logs:/srv/quality_recticel/logs + # Mount instance directory for config persistence + - /srv/docker-test/instance:/app/instance + # Mount app code for easy updates (DISABLED - causes config issues) + # Uncomment only for development, not production + # - /srv/docker-test/app:/app networks: - recticel-network healthcheck: @@ -70,6 +73,5 @@ networks: recticel-network: driver: bridge -volumes: - mariadb_data: - driver: local +# Note: Using bind mounts to /srv/docker-test/ instead of named volumes +# This allows easier access and management of persistent data diff --git a/py_app/app/db_create_scripts/setup_complete_database.py b/py_app/app/db_create_scripts/setup_complete_database.py index a508a95..7063dc3 100755 --- a/py_app/app/db_create_scripts/setup_complete_database.py +++ b/py_app/app/db_create_scripts/setup_complete_database.py @@ -266,9 +266,72 @@ def create_permissions_tables(): print_error(f"Failed to create permissions tables: {e}") return False +def create_users_table_mariadb(): + """Create users and roles tables in MariaDB and seed superadmin""" + print_step(6, "Creating MariaDB Users and Roles Tables") + + try: + conn = mariadb.connect(**DB_CONFIG) + cursor = conn.cursor() + + # Create users table in MariaDB + users_table_query = """ + CREATE TABLE IF NOT EXISTS users ( + id INT AUTO_INCREMENT PRIMARY KEY, + username VARCHAR(100) UNIQUE NOT NULL, + password VARCHAR(255) NOT NULL, + role VARCHAR(50) NOT NULL, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ); + """ + cursor.execute(users_table_query) + print_success("Table 'users' created successfully") + + # Create roles table in MariaDB + roles_table_query = """ + CREATE TABLE IF NOT EXISTS roles ( + id INT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(100) UNIQUE NOT NULL, + access_level VARCHAR(50) NOT NULL, + description TEXT, + created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP + ); + """ + cursor.execute(roles_table_query) + print_success("Table 'roles' created successfully") + + # Insert superadmin role if not exists + cursor.execute("SELECT COUNT(*) FROM roles WHERE name = %s", ('superadmin',)) + if cursor.fetchone()[0] == 0: + cursor.execute(""" + INSERT INTO roles (name, access_level, description) + VALUES (%s, %s, %s) + """, ('superadmin', 'full', 'Full access to all app areas and functions')) + print_success("Superadmin role created") + + # Insert superadmin user if not exists + cursor.execute("SELECT COUNT(*) FROM users WHERE username = %s", ('superadmin',)) + if cursor.fetchone()[0] == 0: + cursor.execute(""" + INSERT INTO users (username, password, role) + VALUES (%s, %s, %s) + """, ('superadmin', 'superadmin123', 'superadmin')) + print_success("Superadmin user created (username: superadmin, password: superadmin123)") + else: + print_success("Superadmin user already exists") + + conn.commit() + cursor.close() + conn.close() + return True + + except Exception as e: + print_error(f"Failed to create users tables in MariaDB: {e}") + return False + def create_sqlite_tables(): - """Create SQLite tables for users and roles""" - print_step(6, "Creating SQLite User and Role Tables") + """Create SQLite tables for users and roles (legacy/backup)""" + print_step(7, "Creating SQLite User and Role Tables (Backup)") try: # Create instance folder if it doesn't exist @@ -325,7 +388,7 @@ def create_sqlite_tables(): def create_database_triggers(): """Create database triggers for automatic quantity calculations""" - print_step(7, "Creating Database Triggers") + print_step(8, "Creating Database Triggers") try: conn = mariadb.connect(**DB_CONFIG) @@ -393,7 +456,7 @@ def create_database_triggers(): def populate_permissions_data(): """Populate permissions and roles with default data""" - print_step(8, "Populating Permissions and Roles Data") + print_step(9, "Populating Permissions and Roles Data") try: conn = mariadb.connect(**DB_CONFIG) @@ -514,16 +577,23 @@ def populate_permissions_data(): def update_external_config(): """Update external_server.conf with correct database settings""" - print_step(9, "Updating External Server Configuration") + print_step(10, "Updating External Server Configuration") try: config_path = os.path.join(os.path.dirname(__file__), '../../instance/external_server.conf') - config_content = """server_domain=localhost -port=3306 -database_name=trasabilitate -username=trasabilitate -password=Initial01! + # Use environment variables if available (Docker), otherwise use defaults + db_host = os.getenv('DB_HOST', 'localhost') + db_port = os.getenv('DB_PORT', '3306') + db_name = os.getenv('DB_NAME', 'trasabilitate') + db_user = os.getenv('DB_USER', 'trasabilitate') + db_password = os.getenv('DB_PASSWORD', 'Initial01!') + + config_content = f"""server_domain={db_host} +port={db_port} +database_name={db_name} +username={db_user} +password={db_password} """ # Create instance directory if it doesn't exist @@ -532,7 +602,7 @@ password=Initial01! with open(config_path, 'w') as f: f.write(config_content) - print_success("External server configuration updated") + print_success(f"External server configuration updated (host: {db_host})") return True except Exception as e: @@ -541,7 +611,7 @@ password=Initial01! def verify_database_setup(): """Verify that all tables were created successfully""" - print_step(10, "Verifying Database Setup") + print_step(11, "Verifying Database Setup") try: conn = mariadb.connect(**DB_CONFIG) @@ -559,7 +629,9 @@ def verify_database_setup(): 'permissions', 'role_permissions', 'role_hierarchy', - 'permission_audit_log' + 'permission_audit_log', + 'users', + 'roles' ] print("\nšŸ“Š MariaDB Tables Status:") @@ -614,6 +686,7 @@ def main(): create_order_for_labels_table, create_warehouse_locations_table, create_permissions_tables, + create_users_table_mariadb, create_sqlite_tables, create_database_triggers, populate_permissions_data,