🐛 fix(manager.py): refactor create_db_and_tables method to check if database and tables already exist before creating them
This commit is contained in:
parent
a8f444656b
commit
dc559e61ae
1 changed files with 8 additions and 4 deletions
|
|
@ -154,6 +154,14 @@ class DatabaseService(Service):
|
||||||
return results
|
return results
|
||||||
|
|
||||||
def create_db_and_tables(self):
|
def create_db_and_tables(self):
|
||||||
|
from sqlalchemy import inspect
|
||||||
|
|
||||||
|
inspector = inspect(self.engine)
|
||||||
|
current_tables = ["flow", "user", "apikey"]
|
||||||
|
table_names = inspector.get_table_names()
|
||||||
|
if table_names and all(table in table_names for table in current_tables):
|
||||||
|
logger.debug("Database and tables already exist")
|
||||||
|
return
|
||||||
logger.debug("Creating database and tables")
|
logger.debug("Creating database and tables")
|
||||||
try:
|
try:
|
||||||
SQLModel.metadata.create_all(self.engine)
|
SQLModel.metadata.create_all(self.engine)
|
||||||
|
|
@ -163,10 +171,6 @@ class DatabaseService(Service):
|
||||||
|
|
||||||
# Now check if the table "flow" exists, if not, something went wrong
|
# Now check if the table "flow" exists, if not, something went wrong
|
||||||
# and we need to create the tables again.
|
# and we need to create the tables again.
|
||||||
from sqlalchemy import inspect
|
|
||||||
|
|
||||||
inspector = inspect(self.engine)
|
|
||||||
current_tables = ["flow", "user", "apikey"]
|
|
||||||
table_names = inspector.get_table_names()
|
table_names = inspector.get_table_names()
|
||||||
for table in current_tables:
|
for table in current_tables:
|
||||||
if table not in table_names:
|
if table not in table_names:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue