almeira-web/apply_sneakers_columns.py

83 lines
2.8 KiB
Python

import paramiko
import sys
import io
if sys.platform == 'win32':
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8', errors='replace')
sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding='utf-8', errors='replace')
HOST = "161.97.93.156"
USER = "carlos"
PASSWORD = "Innova1978+*?"
MARIADB_CONTAINER = "appwrite-mariadb-sss6crntvrcs54qbhvhy5728"
DB_PASS = "sn9YkZqoLV06CQxAy2qsKi36S7POVShT"
DB_USER = "sYoHWwl0DGvEyt3s"
client = paramiko.SSHClient()
client.set_missing_host_key_policy(paramiko.AutoAddPolicy())
client.connect(hostname=HOST, username=USER, password=PASSWORD, timeout=30)
# 1. Get collections mapping for DB 2
def get_query(q):
sftp = client.open_sftp()
with sftp.file('/tmp/q.sql', 'w') as f:
f.write(q)
sftp.close()
cmd = f'docker exec -i {MARIADB_CONTAINER} mysql -u {DB_USER} -p{DB_PASS} appwrite -N -B < /tmp/q.sql'
stdin, stdout, stderr = client.exec_command(cmd)
lines = [line.strip().split('\t') for line in stdout if line.strip()]
return lines
cols = get_query("SELECT _id, _uid FROM _1_database_2;")
print("Collections in DB 2:", cols)
col_map = {row[1]: f"_1_database_2_collection_{row[0]}" for row in cols}
# 2. Get attributes for DB 2
attrs = get_query("SELECT collectionId, `key`, type, size, required FROM _1_attributes WHERE databaseInternalId = 2;")
print(f"Total attributes in DB 2: {len(attrs)}")
sql_commands = []
for col_uid, key, attr_type, size, req in attrs:
tbl = col_map.get(col_uid)
if not tbl:
continue
col_def = "VARCHAR(255) DEFAULT NULL"
if attr_type == 'string':
s = int(size) if size and size != 'NULL' else 255
if s > 65535:
col_def = "MEDIUMTEXT DEFAULT NULL"
elif s > 2000:
col_def = "TEXT DEFAULT NULL"
else:
col_def = f"VARCHAR({min(s, 4000)}) DEFAULT NULL"
elif attr_type == 'integer':
col_def = "BIGINT DEFAULT NULL"
elif attr_type == 'boolean':
col_def = "TINYINT(1) DEFAULT NULL"
elif attr_type == 'float' or attr_type == 'double':
col_def = "DOUBLE DEFAULT NULL"
sql_commands.append(f"ALTER TABLE {tbl} ADD COLUMN `{key}` {col_def};")
sql_commands.append("UPDATE _1_attributes SET status = 'available', error = '' WHERE databaseInternalId = 2;")
full_sql = "\n".join([f"{cmd} -- error ignored" for cmd in sql_commands])
sftp = client.open_sftp()
with sftp.file('/tmp/apply_attrs.sql', 'w') as f:
f.write("\n".join(sql_commands))
sftp.close()
print("Applying attribute migrations to MariaDB...")
cmd = f'docker exec -i {MARIADB_CONTAINER} mysql -u {DB_USER} -p{DB_PASS} -f appwrite < /tmp/apply_attrs.sql'
stdin, stdout, stderr = client.exec_command(cmd)
for line in stdout:
print(line.strip())
for line in stderr:
print("[LOG]:", line.strip())
client.close()
print("Done!")