function check_database_version
Queries a Neo4j graph database to retrieve and return the current schema version stored in CDocs nodes.
/tf/active/vicechatdev/CDocs single class/db/schema_manager.py
802 - 827
simple
Purpose
This function checks the database schema version by querying Neo4j for CDocs nodes and extracting their version property. It's used for version management, migration tracking, and ensuring compatibility between application code and database schema. Returns '0.0.0' as a default when no version is found or when errors occur, making it safe for initialization scenarios.
Source Code
def check_database_version() -> str:
"""
Check the database schema version.
Returns:
String representing the schema version
"""
from CDocs import db
try:
# Query for schema version info
result = db.run_query(
"""
MATCH (c:CDocs)
RETURN c.version as version
LIMIT 1
"""
)
if result and len(result) > 0 and 'version' in result[0]:
return result[0]['version']
else:
return "0.0.0" # Default for unversioned schema
except Exception as e:
logger.error(f"Error checking database version: {e}")
return "0.0.0"
Return Value
Type: str
Returns a string representing the database schema version in semantic versioning format (e.g., '1.2.3'). Returns '0.0.0' if no version is found in the database, if the query returns no results, or if an exception occurs during the query execution.
Dependencies
neo4jloggingCDocs
Required Imports
import logging
from CDocs import db
Conditional/Optional Imports
These imports are only needed under specific conditions:
from CDocs import db
Condition: imported inside the function body, required for database query execution
Required (conditional)Usage Example
# Ensure logger is configured
import logging
logger = logging.getLogger(__name__)
# Assuming CDocs.db is properly configured with Neo4j connection
from your_module import check_database_version
# Check the current database schema version
version = check_database_version()
print(f"Current database schema version: {version}")
# Use in version comparison logic
if version == '0.0.0':
print("Database is unversioned or not initialized")
else:
print(f"Database is at version {version}")
Best Practices
- Ensure the 'logger' variable is defined in the module scope before calling this function
- The function returns '0.0.0' for both unversioned databases and error conditions - implement additional logging or error handling if you need to distinguish between these cases
- This function performs a database query on every call - consider caching the result if called frequently
- The LIMIT 1 clause assumes all CDocs nodes have the same version - ensure your database maintains version consistency
- Handle the '0.0.0' return value appropriately in your application logic to trigger initialization or migration workflows
- The function catches all exceptions broadly - consider more specific exception handling for production use
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function update_database_version 78.5% similar
-
function update_schema 62.5% similar
-
function update_schema_v1 62.4% similar
-
function validate_schema 58.5% similar
-
function initialize_schema_v1 57.6% similar