@@ -111,9 +111,7 @@ def _is_valid_drop_sql(self, sql: str) -> bool:
111111 def _qualify_drop_sql (self , sql : str ) -> str :
112112 up = sql .upper ()
113113 if up .startswith (SQL_DROP_TABLE ):
114- table_name = (
115- sql .split (f"{ SQL_DROP_TABLE_IF_EXISTS } " )[- 1 ].split ()[0 ].strip ()
116- )
114+ table_name = sql .split (f"{ SQL_DROP_TABLE_IF_EXISTS } " )[- 1 ].split ()[0 ].strip ()
117115 return sql .replace (
118116 f"{ SQL_DROP_TABLE_IF_EXISTS } { table_name } " ,
119117 (
@@ -125,17 +123,12 @@ def _qualify_drop_sql(self, sql: str) -> str:
125123 index_name = sql .split ("DROP INDEX IF EXISTS " )[- 1 ].split ()[0 ].strip ()
126124 return sql .replace (
127125 f"DROP INDEX IF EXISTS { index_name } " ,
128- (
129- f"DROP INDEX IF EXISTS "
130- f"{ self .schema_context .qualify_table (index_name )} "
131- ),
126+ (f"DROP INDEX IF EXISTS { self .schema_context .qualify_table (index_name )} " ),
132127 )
133128 if up .startswith ("DROP TRIGGER" ):
134129 return sql .replace (" ON " , f" ON { self .schema_context } ." )
135130 if up .startswith ("DROP FUNCTION" ):
136- function_name = (
137- sql .split ("IF EXISTS" )[- 1 ].split ("CASCADE" )[0 ].strip ()
138- )
131+ function_name = sql .split ("IF EXISTS" )[- 1 ].split ("CASCADE" )[0 ].strip ()
139132 return sql .replace (
140133 f"IF EXISTS { function_name } " ,
141134 f"IF EXISTS { self .schema_context } .{ function_name } " ,
@@ -359,9 +352,7 @@ async def _drop_tables(
359352
360353 modified_sql = self ._qualify_drop_sql (sql )
361354
362- if self ._is_redundant_drop (
363- modified_sql , dropped_tables
364- ):
355+ if self ._is_redundant_drop (modified_sql , dropped_tables ):
365356 LOGGER .debug (
366357 "Skipping redundant drop statement for "
367358 "category %s: %s" ,
@@ -384,7 +375,9 @@ async def _drop_tables(
384375 )
385376 if SQL_DROP_TABLE in modified_sql .upper ():
386377 table_name = (
387- modified_sql .split (SQL_DROP_TABLE_IF_EXISTS )[- 1 ]
378+ modified_sql .split (SQL_DROP_TABLE_IF_EXISTS )[
379+ - 1
380+ ]
388381 .split ()[0 ]
389382 .strip (";" )
390383 .split ("." )[- 1 ]
@@ -739,35 +732,27 @@ async def _setup_schema(self, cursor) -> None:
739732 await cursor .execute (
740733 f"GRANT ALL ON SCHEMA { self .schema_context } TO { self .schema_context } "
741734 )
742- LOGGER .debug (
743- "Created and granted permissions on schema %s" , self .schema_context
744- )
735+ LOGGER .debug ("Created and granted permissions on schema %s" , self .schema_context )
745736 await cursor .execute (f"SET search_path TO { self .schema_context } , public" )
746737
747738 async def _create_core_tables (self , cursor ) -> None :
748739 """Create the core database tables."""
749740 await cursor .execute (f"""
750- CREATE TABLE IF NOT EXISTS {
751- self .schema_context .qualify_table ("config" )
752- } (
741+ CREATE TABLE IF NOT EXISTS { self .schema_context .qualify_table ("config" )} (
753742 name TEXT PRIMARY KEY,
754743 value TEXT
755744 )
756745 """ )
757746 await cursor .execute (f"""
758- CREATE TABLE IF NOT EXISTS {
759- self .schema_context .qualify_table ("profiles" )
760- } (
747+ CREATE TABLE IF NOT EXISTS { self .schema_context .qualify_table ("profiles" )} (
761748 id SERIAL PRIMARY KEY,
762749 name TEXT UNIQUE,
763750 reference TEXT,
764751 profile_key TEXT
765752 )
766753 """ )
767754 await cursor .execute (f"""
768- CREATE TABLE IF NOT EXISTS {
769- self .schema_context .qualify_table ("items" )
770- } (
755+ CREATE TABLE IF NOT EXISTS { self .schema_context .qualify_table ("items" )} (
771756 id SERIAL PRIMARY KEY,
772757 profile_id INTEGER,
773758 kind INTEGER,
@@ -782,9 +767,7 @@ async def _create_core_tables(self, cursor) -> None:
782767 )
783768 """ )
784769 await cursor .execute (f"""
785- CREATE TABLE IF NOT EXISTS {
786- self .schema_context .qualify_table ("items_tags" )
787- } (
770+ CREATE TABLE IF NOT EXISTS { self .schema_context .qualify_table ("items_tags" )} (
788771 id SERIAL PRIMARY KEY,
789772 item_id INTEGER,
790773 name TEXT,
@@ -812,30 +795,33 @@ async def _apply_release_schemas(self, cursor, effective_release_number: str) ->
812795 async def _process_category_schema (self , cursor , category : str , schema ) -> None :
813796 """Process and apply schema for a specific category."""
814797 LOGGER .debug ("Processing category=%s with schema=%s" , category , schema )
815-
798+
816799 if schema is None :
817800 LOGGER .warning ("Skipping category %s: schema is None" , category )
818801 return
819-
802+
820803 if not isinstance (schema , dict ):
821804 LOGGER .error (
822805 "Invalid schema type for category %s: expected dict, got %s" ,
823- category , type (schema ),
806+ category ,
807+ type (schema ),
824808 )
825809 return
826-
810+
827811 if "postgresql" not in schema :
828812 LOGGER .warning (
829813 "Skipping category %s: no postgresql schema found in %s" ,
830- category , schema ,
814+ category ,
815+ schema ,
831816 )
832817 return
833818
834819 LOGGER .debug (
835820 "Applying PostgreSQL schema for category %s: %s" ,
836- category , schema ["postgresql" ],
821+ category ,
822+ schema ["postgresql" ],
837823 )
838-
824+
839825 for sql in schema ["postgresql" ]:
840826 await self ._execute_schema_sql (cursor , category , sql )
841827
@@ -848,7 +834,9 @@ async def _execute_schema_sql(self, cursor, category: str, sql: str) -> None:
848834 except Exception as e :
849835 LOGGER .error (
850836 "Failed to execute SQL for category %s: %s, SQL: %s" ,
851- category , str (e ), modified_sql ,
837+ category ,
838+ str (e ),
839+ modified_sql ,
852840 )
853841 raise DatabaseError (
854842 code = DatabaseErrorCode .PROVISION_ERROR ,
@@ -861,15 +849,15 @@ async def _insert_configuration_data(
861849 ) -> None :
862850 """Insert configuration data and create indexes."""
863851 await self ._ensure_core_indexes (cursor )
864-
852+
865853 config_data = [
866854 ("default_profile" , default_profile ),
867855 ("key" , None ),
868856 ("schema_release_number" , effective_release_number ),
869857 ("schema_release_type" , "postgresql" ),
870858 ("schema_config" , self .schema_config ),
871859 ]
872-
860+
873861 for name , value in config_data :
874862 await cursor .execute (
875863 (
@@ -878,7 +866,7 @@ async def _insert_configuration_data(
878866 ),
879867 (name , value ),
880868 )
881-
869+
882870 await cursor .execute (
883871 (
884872 f"INSERT INTO { self .schema_context .qualify_table ('profiles' )} "
@@ -931,9 +919,7 @@ def _qualify_create_statement(self, sql: str) -> str:
931919 return sql .replace (" ON " , f" ON { self .schema_context } ." )
932920 if up .startswith ("CREATE FUNCTION" ):
933921 function_name = (
934- sql .split ("CREATE OR REPLACE FUNCTION " )[- 1 ]
935- .split ("(" )[0 ]
936- .strip ()
922+ sql .split ("CREATE OR REPLACE FUNCTION " )[- 1 ].split ("(" )[0 ].strip ()
937923 )
938924 return sql .replace (
939925 f"CREATE OR REPLACE FUNCTION { function_name } " ,
0 commit comments