77logger = logging .getLogger ('cloudsql_to_supabase.clean' )
88
99class DumpCleaner :
10- def __init__ (self , input_file : Path = None , output_file : Path = None ) -> None :
10+ def __init__ (self , input_file : Path = None , output_file : Path = None , target_schema : str = None ) -> None :
1111 self .input_file = input_file or Path (config .OUTPUT_DUMP )
1212 self .output_file = output_file or Path (config .CLEANED_DUMP )
13+ self .target_schema = target_schema or config .SUPABASE_SCHEMA
14+
1315 self .skip_patterns = [
1416 r'^(CREATE|ALTER) ROLE' , # Skip role creation/alteration
1517 r'^COMMENT ON EXTENSION' , # Skip extension comments
1618 ]
19+
20+ # Regular ownership replacement
21+ owner_replacement = 'OWNER TO public;'
22+ # If using non-public schema and we have permissions, keep the target schema as owner
23+ if self .target_schema != "public" :
24+ owner_replacement = f'OWNER TO { self .target_schema } ;'
25+
1726 self .replacement_rules = [
18- (r'OWNER TO .*?;' , 'OWNER TO public;' ), # Change ownership to public
27+ (r'OWNER TO .*?;' , owner_replacement ),
1928 (r'CREATE SCHEMA .*?;' , '-- Schema creation removed' ), # Comment out schema creation
2029 ]
2130
31+ # If we're targeting a non-public schema, add schema modifications
32+ if self .target_schema != "public" :
33+ # Add rules to update schema references in the dump
34+ self .replacement_rules .extend ([
35+ # Update SET search_path statements
36+ (r'SET search_path = public' , f'SET search_path = { self .target_schema } ' ),
37+ # Update schema references in table names
38+ (r'CREATE TABLE public\.' , f'CREATE TABLE { self .target_schema } .' ),
39+ # Update schema references in sequence names
40+ (r'CREATE SEQUENCE public\.' , f'CREATE SEQUENCE { self .target_schema } .' ),
41+ # Update schema references in views
42+ (r'CREATE VIEW public\.' , f'CREATE VIEW { self .target_schema } .' ),
43+ # Update schema references in functions
44+ (r'CREATE FUNCTION public\.' , f'CREATE FUNCTION { self .target_schema } .' ),
45+ ])
46+
2247 def clean_dump_file (self ) -> Path :
2348 """
2449 Clean the SQL dump file for Supabase import by removing/modifying
@@ -57,9 +82,14 @@ def clean_dump_file(self) -> Path:
5782 logger .info (f"Cleaned dump saved as { self .output_file } " )
5883 return self .output_file
5984
60- def clean_dump_file (input_file : Optional [Path ] = None , output_file : Optional [Path ] = None ) -> Path :
85+ def clean_dump_file (input_file : Optional [Path ] = None , output_file : Optional [Path ] = None , target_schema : Optional [ str ] = None ) -> Path :
6186 """
6287 Convenience function to clean a SQL dump file
88+
89+ Args:
90+ input_file: Path to the input SQL dump file
91+ output_file: Path to the output cleaned SQL file
92+ target_schema: Schema to use in Supabase. If None, uses the one from config
6393 """
64- cleaner = DumpCleaner (input_file , output_file )
65- return cleaner .clean_dump_file ()
94+ cleaner = DumpCleaner (input_file , output_file , target_schema )
95+ return cleaner .clean_dump_file ()
0 commit comments