Skip to content

Commit 6e0c51b

Browse files
committed
Refactor import_to_supabase function to enhance logging, error handling, and parameter support
1 parent 295facf commit 6e0c51b

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

cloudsql_to_supabase/import_.py

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,50 @@
1-
from . import config, utils
21
import os
2+
import logging
3+
from pathlib import Path
4+
from typing import Optional
5+
from . import config, utils
6+
7+
8+
9+
logger = logging.getLogger('cloudsql_to_supabase.import')
310

411

512

6-
def import_to_supabase():
7-
print("\n Importing into Supabase")
13+
def import_to_supabase(input_file: Optional[Path] = None, password: Optional[str] = None) -> None:
14+
"""
15+
import cleaned sql dump into supabase
16+
17+
Args:
18+
input_file: Path to the sql dump file to import. if None, uses the default.
19+
password: supabase database password. if none uses from config
20+
21+
"""
22+
config.validate_config()
23+
dump_file = input_file or Path(config.CLEANED_DUMP)
24+
if not dump_file.exists():
25+
raise FileNotFoundError(f"Dump not found: {dump_file}")
26+
27+
logger.info(f'importing data into supabase database: {config.SUPABASE_DB}')
28+
829
env = os.environ.copy()
9-
env["PGPASSWORD"] = config.SUPABASE_PASSWORD
30+
env['PGPASSWORD'] = password or config.SUPABASE_PASSWORD
31+
32+
1033
cmd = (
11-
f"psql -h {config.SUPABASE_HOST} -p {config.SUPABASE_PORT} -U {config.SUPABASE_USER} "
12-
f"-d {config.SUPABASE_DB} -f {config.CLEANED_DUMP} \"sslmode=require\""
34+
f"psql -h {config.SUPABASE_HOST} "
35+
f"-p {config.SUPABASE_PORT} "
36+
f"-U {config.SUPABASE_USER} "
37+
f"-d {config.SUPABASE_DB} "
38+
f"--set ON_ERROR_STOP=on " # Stop on first error
39+
f"--single-transaction " # Run as a single transaction
40+
f"--sslmode={config.SUPABASE_SSL_MODE} "
41+
f"-f {dump_file}"
1342
)
14-
utils.run_command(cmd, env=env)
43+
44+
45+
try:
46+
utils.run_command(cmd, env)
47+
logger.info("import completed successfully")
48+
except Exception as e:
49+
logger.error(f'import failed: {e}')
50+
raise

0 commit comments

Comments
 (0)