Skip to content

Commit 0bf9d58

Browse files
committed
[IMP] Do not change database structure at every startup
Instead, only load table definitions and constraints when the table is created. This only impacts Odev's database, used for internal operations. Performance gains: - Framework loaded: 5ms (1.3%) - Framework started: 48ms (6.4%) - Execution completed: 46ms (5.5%)
1 parent 0359310 commit 0bf9d58

2 files changed

Lines changed: 16 additions & 11 deletions

File tree

odev/common/postgres.py

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -158,25 +158,24 @@ def __init__(self, database: PostgresDatabase, name: str | None = None):
158158
self.name: str = name or self.name
159159
"""Name of the table in which data is stored, must be set in subclass."""
160160

161-
with self.database:
162-
self.prepare_database_table()
163-
164161
self.database.tables[self.name] = self
165162

166163
def prepare_database_table(self):
167164
"""Prepare the table and ensures it has the correct definition and constraints applied."""
168165
if self._columns is not None:
169-
if not self.database.table_exists(self.name):
170-
logger.debug(f"Creating table {self.name!r} in database {self.database!r}")
171-
self.database.create_table(self.name, self._columns)
172-
elif missing_columns := self.database.columns_exist(self.name, list(self._columns.keys())):
166+
logger.debug(f"Creating table {self.name!r} in database {self.database!r}")
167+
self.database.create_table(self.name, self._columns)
168+
169+
if missing_columns := self.database.columns_exist(self.name, list(self._columns.keys())):
173170
for column in missing_columns:
174171
self.__add_missing_column(column)
175172

176-
if self._constraints is not None:
177-
for name, definition in self._constraints.items():
178-
logger.debug(f"Creating constraint {name!r} on table {self.name!r} in database {self.database.name!r}")
179-
self.database.constraint(self.name, name, definition)
173+
if self._constraints is not None:
174+
for name, definition in self._constraints.items():
175+
logger.debug(
176+
f"Creating constraint {name!r} on table {self.name!r} in database {self.database.name!r}"
177+
)
178+
self.database.constraint(self.name, name, definition)
180179

181180
def clear(self):
182181
"""Clear the table."""

odev/common/store/datastore.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,14 @@ def __init__(self, name: str = "odev"):
2727
self.databases = DatabaseStore(self)
2828
self.history = HistoryStore(self)
2929
self.secrets = SecretStore(self)
30+
self.__load_tables()
3031
self.__load_plugins_tables()
3132

33+
def __load_tables(self):
34+
for table in self.tables.values():
35+
if not self.table_exists(table.name):
36+
table.prepare_database_table()
37+
3238
def __load_plugins_tables(self):
3339
odev_path = Path(__file__).parents[2]
3440
plugins = [path for path in (odev_path / "plugins").glob("*/datastore") if path.is_dir()]

0 commit comments

Comments
 (0)