47 أسطر
1.9 KiB
Python
47 أسطر
1.9 KiB
Python
# -*- coding: utf-8 -*-
|
|
import os, subprocess, json
|
|
|
|
def update_progress(status, message, percent=0):
|
|
with open("pg_progress.json", "w") as f:
|
|
json.dump({"status": status, "message": message, "percent": percent}, f)
|
|
|
|
def migrate():
|
|
# استلام البيانات
|
|
S_HOST, S_NAME, S_USER, S_PASS = os.getenv("DB_HOST"), os.getenv("DB_NAME"), os.getenv("DB_USER"), os.getenv("DB_PASS")
|
|
T_HOST, T_NAME, T_USER, T_PASS = os.getenv("DEST_HOST"), os.getenv("DEST_NAME"), os.getenv("DEST_USER"), os.getenv("DEST_PASS")
|
|
|
|
schemas = os.getenv("ONLY_SCHEMAS", "").strip()
|
|
tables = os.getenv("ONLY_TABLES", "").strip()
|
|
|
|
filter_args = ""
|
|
# إذا تم اختيار جداول محددة، نكتفي بها
|
|
if tables:
|
|
for t in tables.split(','):
|
|
filter_args += f" -t {t.strip()}"
|
|
# إذا لم تُحدد جداول ولكن حُددت Schemas، ننقل الـ Schemas بالكامل
|
|
elif schemas:
|
|
for s in schemas.split(','):
|
|
filter_args += f" -n {s.strip()}"
|
|
|
|
update_progress("running", "بدء ضخ البيانات المختارة...", 30)
|
|
os.environ['PGPASSWORD'] = S_PASS
|
|
|
|
migrate_cmd = (
|
|
f"pg_dump -h {S_HOST} -U {S_USER} {filter_args} --clean --if-exists --no-owner {S_NAME} | "
|
|
f"PGPASSWORD={T_PASS} psql -h {T_HOST} -U {T_USER} -d {T_NAME}"
|
|
)
|
|
|
|
try:
|
|
process = subprocess.Popen(migrate_cmd, shell=True, stderr=subprocess.PIPE)
|
|
update_progress("running", "جاري نقل الهياكل والبيانات...", 70)
|
|
_, stderr = process.communicate()
|
|
|
|
if process.returncode == 0:
|
|
update_progress("completed", "نجحت عملية النقل المخصصة! ✅", 100)
|
|
else:
|
|
update_progress("error", f"خطأ: {stderr.decode()}", 0)
|
|
except Exception as e:
|
|
update_progress("error", str(e), 0)
|
|
|
|
if __name__ == "__main__":
|
|
migrate() |