almeira-web/sync_collection_attributes.py

87 lines
2.9 KiB
Python

import paramiko
import json
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)
def 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 = query("SELECT _id, _uid FROM _1_database_2;")
print("Collections in DB 2:", cols)
raw_attrs = query("SELECT _id, _uid, collectionId, collectionInternalId, `key`, type, size, required, signed, array FROM _1_attributes WHERE databaseInternalId = 2;")
print(f"Total attributes: {len(raw_attrs)}")
attrs_by_col = {}
for a in raw_attrs:
col_uid = a[2]
key_name = a[4]
attr_type = a[5]
size_val = int(a[6]) if len(a) > 6 and a[6] and a[6] != 'NULL' else 255
req_val = bool(int(a[7])) if len(a) > 7 and a[7] and a[7] != 'NULL' else False
signed_val = bool(int(a[8])) if len(a) > 8 and a[8] and a[8] != 'NULL' else True
array_val = bool(int(a[9])) if len(a) > 9 and a[9] and a[9] != 'NULL' else False
attr_obj = {
"$id": key_name,
"$collection": "$attributes",
"key": key_name,
"type": attr_type,
"status": "available",
"size": size_val,
"required": req_val,
"default": None,
"signed": signed_val,
"array": array_val,
"filters": []
}
attrs_by_col.setdefault(col_uid, []).append(attr_obj)
update_sqls = []
for col_id, col_uid in cols:
attrs_list = attrs_by_col.get(col_uid, [])
json_str = json.dumps(attrs_list).replace("'", "\\'")
sql = f"UPDATE _1_database_2 SET attributes = '{json_str}' WHERE _id = {col_id};"
update_sqls.append(sql)
sftp = client.open_sftp()
with sftp.file('/tmp/sync_col_attrs.sql', 'w') as f:
f.write("\n".join(update_sqls))
sftp.close()
cmd = f'docker exec -i {MARIADB_CONTAINER} mysql -u {DB_USER} -p{DB_PASS} appwrite < /tmp/sync_col_attrs.sql'
stdin, stdout, stderr = client.exec_command(cmd)
for line in stdout:
print(line.strip())
for line in stderr:
print("[ERR]:", line.strip())
# Flush redis to ensure cache reload
client.exec_command('docker exec appwrite-redis-sss6crntvrcs54qbhvhy5728 redis-cli flushall')
print("Redis flushed and collections updated successfully!")
client.close()