feat(backend): auto-create empty database if missing
This commit is contained in:
@@ -1 +1 @@
|
|||||||
3.12.4
|
3.13.7
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ class NimbusFlowCLI:
|
|||||||
self._init_repositories()
|
self._init_repositories()
|
||||||
|
|
||||||
def _get_most_recent_database(self) -> Path:
|
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()
|
versions = self.list_database_versions()
|
||||||
|
|
||||||
if versions:
|
if versions:
|
||||||
@@ -63,11 +63,31 @@ class NimbusFlowCLI:
|
|||||||
most_recent = versions[0] # Already sorted newest first
|
most_recent = versions[0] # Already sorted newest first
|
||||||
return most_recent
|
return most_recent
|
||||||
else:
|
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():
|
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
|
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:
|
def _create_versioned_database(self) -> Path:
|
||||||
"""Create a versioned copy from the most recent database."""
|
"""Create a versioned copy from the most recent database."""
|
||||||
source_db = self.db_path # Use the most recent database as source
|
source_db = self.db_path # Use the most recent database as source
|
||||||
|
|||||||
Reference in New Issue
Block a user