From 3b9c074bc7f40cf57f01ecdca33cb850ee4eb7dc Mon Sep 17 00:00:00 2001 From: Giovani Date: Sat, 30 Aug 2025 18:12:33 -0400 Subject: [PATCH] feat(backend): auto-create empty database if missing --- .python-version | 2 +- backend/cli/base.py | 26 +++++++++++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/.python-version b/.python-version index 455808f..976544c 100644 --- a/.python-version +++ b/.python-version @@ -1 +1 @@ -3.12.4 +3.13.7 diff --git a/backend/cli/base.py b/backend/cli/base.py index 1358639..04fefdf 100644 --- a/backend/cli/base.py +++ b/backend/cli/base.py @@ -55,7 +55,7 @@ class NimbusFlowCLI: self._init_repositories() def _get_most_recent_database(self) -> Path: - """Get the most recent database version, or base database if no versions exist.""" + """Get the most recent database version, or create base database if none exist.""" versions = self.list_database_versions() if versions: @@ -63,11 +63,31 @@ class NimbusFlowCLI: most_recent = versions[0] # Already sorted newest first return most_recent else: - # No versions exist, use base database + # No versions exist, create base database if it doesn't exist if not self.base_db_path.exists(): - raise CLIError(f"Base database not found: {self.base_db_path}") + self._create_base_database() return self.base_db_path + def _create_base_database(self) -> None: + """Create the base database from schema.sql if it doesn't exist.""" + # Ensure the directory exists + self.db_dir.mkdir(parents=True, exist_ok=True) + + # Read the schema from the schema.sql file + schema_path = Path(__file__).parent.parent / "schema.sql" + if not schema_path.exists(): + raise CLIError(f"Schema file not found: {schema_path}") + + with open(schema_path, 'r') as f: + schema_sql = f.read() + + # Create the database and execute the schema + with DatabaseConnection(self.base_db_path) as db: + db.executescript(schema_sql) + + print(f"{Colors.SUCCESS}Created new database:{Colors.RESET} {Colors.CYAN}{self.base_db_path.name}{Colors.RESET}") + print(f"{Colors.DIM}Location: {self.base_db_path}{Colors.RESET}") + def _create_versioned_database(self) -> Path: """Create a versioned copy from the most recent database.""" source_db = self.db_path # Use the most recent database as source