11from . import config , utils
22import getpass
33import os
4+ from pathlib import Path
5+ import getpass
6+ import logging
7+ from . import config , utils
8+
9+
10+ logger = logging .getLogger ('cloud_to_supabase.export' )
411
512
6- def export_cloudsql ():
7- cmd = (
8- f"pg_dump -U { config .CLOUDSQL_USER } -h { config .CLOUDSQL_HOST } "
9- f"-d { config .CLOUDSQL_DB } --no-owner --no-privileges --no-comments "
10- f"--no-acl --data-only --column-inserts > { config .OUTPUT_DUMP } "
11- )
13+ def export_cloudsql (password : str = None , schema_only : bool = False ):
14+
15+ """
16+ Export a CloudSQL PostgreSQL database to a dump file.
17+
18+ Args:
19+ password: CloudSQL database password. If None, will prompt.
20+ schema_only: If True, only export schema, not data
21+
22+ Returns:
23+ Path to the created dump file
24+ """
25+
26+ config .validate_config ()
27+ logger .info (f'starting export from cloudsql database: { config .CLOUDSQL_DB } ' )
28+
29+
30+ cmd_parts = [
31+ f"pg_dump -U { config .CLOUDSQL_USER } " ,
32+ f"-h { config .CLOUDSQL_HOST } " ,
33+ f"-p { config .CLOUDSQL_PORT } " ,
34+ f"-d { config .CLOUDSQL_DB } " ,
35+ f"--sslmode={ config .CLOUDSQL_SSL_MODE } " ,
36+ "-F p" , # Plain text format
37+ f"-f { config .OUTPUT_DUMP } "
38+ ]
39+
40+
41+ if schema_only :
42+ cmd_parts .append ("--schema-ony" )
43+
44+
45+ cmd = " " .join (cmd_parts )
46+
1247 env = os .environ .copy ()
13- env ["PGPASSWORD" ] = getpass .getpass (prompt = "Enter Cloud SQL password: " )
14- utils .run_command (cmd , env = env )
48+ if password is None :
49+ env ['PGPASSWORD' ] = getpass .getpass ('enter cloud sql password: ' )
50+ else :
51+ env ['PGPASSWORD' ] = password
52+
53+
54+ try :
55+ utils .run_command (cmd , env )
56+ logger .info (f'export completed succesfully, saved to { config .OUTPUT_DUMP } ' )
57+ return Path (config .OUTPUT_DUMP )
58+ except Exception as e :
59+ logger .error (f"export failed: { e } " )
60+ raise
0 commit comments