@@ -111,7 +111,7 @@ def _decode_kwargs(**kwargs: Any) -> dict[str, Any]:
111
111
def _cached_sessionmaker (
112
112
url : str , ** kwargs : Any
113
113
) -> sa .orm .sessionmaker [sa .orm .Session ]:
114
- engine = sa . create_engine (url , ** _decode_kwargs (** kwargs ))
114
+ engine = init_database (url , ** _decode_kwargs (** kwargs ))
115
115
Base .metadata .create_all (engine )
116
116
return sa .orm .sessionmaker (engine )
117
117
@@ -120,44 +120,57 @@ def cached_sessionmaker(url: str, **kwargs: Any) -> sa.orm.sessionmaker[sa.orm.S
120
120
return _cached_sessionmaker (url , ** _encode_kwargs (** kwargs ))
121
121
122
122
123
- def init_database (connection_string : str , force : bool = False ) -> sa .engine .Engine :
123
+ def init_database (
124
+ connection_string : str , force : bool = False , ** kwargs : Any
125
+ ) -> sa .engine .Engine :
124
126
"""
125
127
Make sure the db located at URI `connection_string` exists updated and return the engine object.
126
128
127
- :param connection_string: something like 'postgresql://user:password@netloc:port/dbname'
128
- :param force: if True, drop the database structure and build again from scratch
129
+ Parameters
130
+ ----------
131
+ connection_string: str
132
+ Something like 'postgresql://user:password@netloc:port/dbname'
133
+ force: bool
134
+ if True, drop the database structure and build again from scratch
135
+ kwargs: Any
136
+ Keyword arguments for create_engine
137
+
138
+ Returns
139
+ -------
140
+ engine: Engine
129
141
"""
130
- engine = sa .create_engine (connection_string )
142
+ engine = sa .create_engine (connection_string , ** kwargs )
131
143
migration_directory = os .path .abspath (os .path .join (__file__ , ".." ))
132
- os .chdir (migration_directory )
133
- alembic_config_path = os .path .join (migration_directory , "alembic.ini" )
134
- alembic_cfg = alembic .config .Config (alembic_config_path )
135
- for option in ["drivername" , "username" , "password" , "host" , "port" , "database" ]:
136
- value = getattr (engine .url , option )
137
- if value is None :
138
- value = ""
139
- alembic_cfg .set_main_option (option , str (value ))
140
- if not sqlalchemy_utils .database_exists (engine .url ):
141
- sqlalchemy_utils .create_database (engine .url )
142
- # cleanup and create the schema
143
- Base .metadata .drop_all (engine )
144
- Base .metadata .create_all (engine )
145
- alembic .command .stamp (alembic_cfg , "head" )
146
- else :
147
- # check the structure is empty or incomplete
148
- query = sa .text (
149
- "SELECT table_name FROM information_schema.tables WHERE table_schema='public'"
150
- )
151
- conn = engine .connect ()
152
- if "cache_entries" not in conn .execute (query ).scalars ().all ():
144
+ with utils .change_working_dir (migration_directory ):
145
+ alembic_config_path = os .path .join (migration_directory , "alembic.ini" )
146
+ alembic_cfg = alembic .config .Config (alembic_config_path )
147
+ for option in [
148
+ "drivername" ,
149
+ "username" ,
150
+ "password" ,
151
+ "host" ,
152
+ "port" ,
153
+ "database" ,
154
+ ]:
155
+ value = getattr (engine .url , option )
156
+ if value is None :
157
+ value = ""
158
+ alembic_cfg .set_main_option (option , str (value ))
159
+ if not sqlalchemy_utils .database_exists (engine .url ):
160
+ sqlalchemy_utils .create_database (engine .url )
161
+ # cleanup and create the schema
162
+ Base .metadata .drop_all (engine )
163
+ Base .metadata .create_all (engine )
164
+ alembic .command .stamp (alembic_cfg , "head" )
165
+ elif "cache_entries" not in sa .inspect (engine ).get_table_names ():
166
+ # db structure is empty or incomplete
153
167
force = True
154
- conn .close ()
155
- if force :
156
- # cleanup and create the schema
157
- Base .metadata .drop_all (engine )
158
- Base .metadata .create_all (engine )
159
- alembic .command .stamp (alembic_cfg , "head" )
160
- else :
161
- # update db structure
162
- alembic .command .upgrade (alembic_cfg , "head" )
168
+ if force :
169
+ # cleanup and create the schema
170
+ Base .metadata .drop_all (engine )
171
+ Base .metadata .create_all (engine )
172
+ alembic .command .stamp (alembic_cfg , "head" )
173
+ else :
174
+ # update db structure
175
+ alembic .command .upgrade (alembic_cfg , "head" )
163
176
return engine
0 commit comments