Update SQL logic to update instead of delete and re-add
All checks were successful
HAProxy Manager Build and Push / Build-and-Push (push) Successful in 1m18s

This commit is contained in:
2025-12-18 12:23:06 -08:00
parent bff18d358b
commit 1fcb25bb88

View File

@@ -282,7 +282,41 @@ def add_domain():
with sqlite3.connect(DB_FILE) as conn:
cursor = conn.cursor()
# Add domain
# Check if domain already exists
cursor.execute('SELECT id, ssl_enabled, ssl_cert_path FROM domains WHERE domain = ?', (domain,))
existing_domain = cursor.fetchone()
if existing_domain:
# Domain exists - update it while preserving SSL settings
domain_id = existing_domain[0]
ssl_enabled = existing_domain[1]
ssl_cert_path = existing_domain[2]
cursor.execute('''
UPDATE domains
SET template_override = ?
WHERE id = ?
''', (template_override, domain_id))
# Update backend or create if doesn't exist
cursor.execute('SELECT id FROM backends WHERE domain_id = ?', (domain_id,))
backend_result = cursor.fetchone()
if backend_result:
backend_id = backend_result[0]
# Update existing backend name
cursor.execute('UPDATE backends SET name = ? WHERE id = ?', (backend_name, backend_id))
# Remove old servers
cursor.execute('DELETE FROM backend_servers WHERE backend_id = ?', (backend_id,))
else:
# Create new backend
cursor.execute('INSERT INTO backends (name, domain_id) VALUES (?, ?)',
(backend_name, domain_id))
backend_id = cursor.lastrowid
logger.info(f"Updated existing domain {domain} (preserved SSL: enabled={ssl_enabled}, cert={ssl_cert_path})")
else:
# New domain - insert it
cursor.execute('INSERT INTO domains (domain, template_override) VALUES (?, ?)', (domain, template_override))
domain_id = cursor.lastrowid
@@ -291,6 +325,9 @@ def add_domain():
(backend_name, domain_id))
backend_id = cursor.lastrowid
logger.info(f"Added new domain {domain}")
# Add/update backend servers
for server in servers:
cursor.execute('''
INSERT INTO backend_servers
@@ -298,11 +335,12 @@ def add_domain():
VALUES (?, ?, ?, ?, ?)
''', (backend_id, server['name'], server['address'],
server['port'], server.get('options')))
# Close cursor and connection
cursor.close()
conn.close()
generate_config()
log_operation('add_domain', True, f'Domain {domain} added successfully')
log_operation('add_domain', True, f'Domain {domain} configured successfully')
return jsonify({'status': 'success', 'domain_id': domain_id})
except Exception as e:
log_operation('add_domain', False, str(e))