almeira-web/apply_sql.py

39 lines
1.4 KiB
Python

import paramiko
import sys
def apply_sql(host, user, password, local_path):
with open(local_path, 'r') as f:
sql = f.read()
statements = [s.strip() for s in sql.split(';') if s.strip()]
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
try:
client.connect(hostname=host, username=user, password=password, timeout=10)
for stmt in statements:
if "COMMIT" in stmt or "START TRANSACTION" in stmt:
continue
# Escape single quotes in stmt for the bash command
stmt_escaped = stmt.replace('"', '\\"')
cmd = f'docker exec -i appwrite-mariadb-sss6crntvrcs54qbhvhy5728 mysql -u sYoHWwl0DGvEyt3s -psn9YkZqoLV06CQxAy2qsKi36S7POVShT appwrite -e "{stmt_escaped}"'
stdin, stdout, stderr = client.exec_command(cmd)
exit_status = stdout.channel.recv_exit_status()
if exit_status != 0:
err = stderr.read().decode()
# Ignore duplicate column errors
if "Duplicate column name" not in err:
print(f"Error on: {stmt[:50]}... -> {err.strip()}")
except Exception as e:
print(f"Exception: {e}", file=sys.stderr)
finally:
client.close()
if __name__ == "__main__":
apply_sql("161.97.93.156", "carlos", "Innova1978+*?", "/tmp/alter_system_only.sql")