127 lines
4.1 KiB
Python
Executable file
127 lines
4.1 KiB
Python
Executable file
#!/usr/bin/python3
|
|
# Führt Datenbank-Migrationen für die Produktion aus.
|
|
#
|
|
# python3 tools/migrate_motorradig.py
|
|
#
|
|
# Die Zugangsdaten werden - wie bei dump_motorradig.py - aus
|
|
# ../motorradig/.credentials gelesen (KEY=VALUE):
|
|
# DB_HOST (Fallback: FTP_URL)
|
|
# DB_USERNAME
|
|
# DB_PASSWORD
|
|
# DB_NAME (Fallback: DB_DATABASE, dann DB_USERNAME)
|
|
#
|
|
# Alle Anweisungen sind idempotent (IF NOT EXISTS / EXISTS-Prüfungen),
|
|
# können also nach jedem Deployment gefahrlos erneut ausgeführt werden.
|
|
# WICHTIG: neue Schemaänderungen hier UND in bin/migrate.php pflegen.
|
|
|
|
import os
|
|
import subprocess
|
|
import sys
|
|
|
|
CRED_FILE = os.path.join(
|
|
os.path.dirname(os.path.abspath(__file__)),
|
|
'..', 'motorradig', '.credentials'
|
|
)
|
|
|
|
|
|
def load_credentials(path: str) -> dict:
|
|
creds = {}
|
|
if not os.path.isfile(path):
|
|
print(f'Fehler: Credentials-Datei nicht gefunden: {path}', file=sys.stderr)
|
|
sys.exit(1)
|
|
|
|
with open(path, 'r', encoding='utf-8') as fh:
|
|
for line in fh:
|
|
line = line.strip()
|
|
if not line or line.startswith('#') or '=' not in line:
|
|
continue
|
|
key, value = line.split('=', 1)
|
|
creds[key.strip()] = value.strip()
|
|
|
|
return creds
|
|
|
|
|
|
# Idempotente Migrationen in Ausführungsreihenfolge.
|
|
# Eine Anweisung pro Listeneintrag (kann mehrere SQL-Statements enthalten).
|
|
MIGRATIONS = [
|
|
# Tabelle für den Online-Mitgliedsantrag
|
|
'''CREATE TABLE IF NOT EXISTS `membership_applications` (
|
|
`id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
|
|
`user_id` bigint(20) unsigned NULL DEFAULT NULL,
|
|
`nachname` varchar(255) NOT NULL,
|
|
`vorname` varchar(255) NOT NULL,
|
|
`strasse` varchar(255) DEFAULT NULL,
|
|
`plz_ort` varchar(255) DEFAULT NULL,
|
|
`telefon` varchar(255) DEFAULT NULL,
|
|
`email` varchar(255) DEFAULT NULL,
|
|
`geburtsdatum` date DEFAULT NULL,
|
|
`motorrad` varchar(255) DEFAULT NULL,
|
|
`aufnahme_ab` date DEFAULT NULL,
|
|
`ort_datum_1` varchar(255) DEFAULT NULL,
|
|
`unterschrift_1` varchar(255) DEFAULT NULL,
|
|
`mandatsreferenz` varchar(255) DEFAULT NULL,
|
|
`glaeubiger_id` varchar(255) DEFAULT NULL,
|
|
`kontoinhaber` varchar(255) DEFAULT NULL,
|
|
`kreditinstitut` varchar(255) DEFAULT NULL,
|
|
`bic` varchar(255) DEFAULT NULL,
|
|
`iban` varchar(255) DEFAULT NULL,
|
|
`ort_datum_2` varchar(255) DEFAULT NULL,
|
|
`unterschrift_2` varchar(255) DEFAULT NULL,
|
|
`created_at` timestamp NULL DEFAULT NULL,
|
|
`updated_at` timestamp NULL DEFAULT NULL,
|
|
PRIMARY KEY (`id`)
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci''',
|
|
]
|
|
|
|
|
|
def main() -> int:
|
|
creds = load_credentials(CRED_FILE)
|
|
|
|
db_host = creds.get('DB_HOST', creds.get('FTP_URL', ''))
|
|
db_user = creds.get('DB_USERNAME', '')
|
|
db_password = creds.get('DB_PASSWORD', '')
|
|
db_name = creds.get('DB_NAME', creds.get('DB_DATABASE', db_user))
|
|
|
|
missing = []
|
|
if not db_host:
|
|
missing.append('DB_HOST (oder FTP_URL)')
|
|
if not db_user:
|
|
missing.append('DB_USERNAME')
|
|
if not db_password:
|
|
missing.append('DB_PASSWORD')
|
|
if not db_name:
|
|
missing.append('DB_NAME (oder DB_USERNAME)')
|
|
|
|
if missing:
|
|
print('Fehler: Fehlende Credentials: ' + ', '.join(missing), file=sys.stderr)
|
|
return 1
|
|
|
|
print(f'Migriere {db_name} (Host: {db_host})')
|
|
|
|
base_cmd = [
|
|
'mysql',
|
|
'--user=' + db_user,
|
|
'--password=' + db_password,
|
|
'--host=' + db_host,
|
|
'--default-character-set=utf8mb4',
|
|
db_name,
|
|
]
|
|
|
|
for index, sql in enumerate(MIGRATIONS, start=1):
|
|
result = subprocess.run(
|
|
base_cmd,
|
|
input=sql.encode('utf-8'),
|
|
stdout=subprocess.PIPE,
|
|
stderr=subprocess.PIPE,
|
|
)
|
|
if result.returncode != 0:
|
|
print(f'Migration {index} fehlgeschlagen:', file=sys.stderr)
|
|
print(result.stderr.decode('utf-8', 'replace'), file=sys.stderr)
|
|
return result.returncode
|
|
|
|
print('Fertig.')
|
|
return 0
|
|
|
|
|
|
if __name__ == '__main__':
|
|
sys.exit(main())
|