function update_database_version
Updates the database schema version in a Neo4j graph database by setting the version property on a CDocs node and recording the update timestamp.
/tf/active/vicechatdev/CDocs single class/db/schema_manager.py
829 - 863
moderate
Purpose
This function is used for database schema version management in a Neo4j-based system. It updates the version property of a CDocs node to track schema migrations or version changes. The function ensures that version updates are logged and handles errors gracefully, returning a boolean to indicate success or failure. It's typically used during database migrations or system upgrades to maintain version consistency.
Source Code
def update_database_version(version: str) -> bool:
"""
Update the database schema version.
Args:
version: New version string
Returns:
Boolean indicating success
"""
from CDocs import db
try:
# Update schema version
result = db.run_query(
"""
MATCH (c:CDocs)
SET c.version = $version,
c.updated_at = datetime()
RETURN c.UID as uid
LIMIT 1
""",
{"version": version}
)
if result and len(result) > 0 and 'uid' in result[0]:
logger.info(f"Updated database schema version to {version}")
return True
else:
logger.warning(f"Failed to update database schema version")
return False
except Exception as e:
logger.error(f"Error updating database version: {e}")
logger.error(traceback.format_exc())
return False
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
version |
str | - | positional_or_keyword |
Parameter Details
version: A string representing the new database schema version to be set. This should follow a versioning scheme (e.g., '1.0.0', '2.1.3') and will be stored in the CDocs node's version property. No format validation is performed by the function itself.
Return Value
Type: bool
Returns a boolean value: True if the database version was successfully updated (i.e., a CDocs node was found and updated with a valid UID), False if the update failed (no matching node found, query error, or exception occurred). The function logs appropriate messages at different log levels based on the outcome.
Dependencies
CDocsloggingtracebackneo4j
Required Imports
import logging
import traceback
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 operations
Required (conditional)Usage Example
import logging
import traceback
from CDocs import db
# Ensure logger is configured
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
logger.addHandler(handler)
# Update database version
new_version = '2.0.1'
success = update_database_version(new_version)
if success:
print(f'Database version successfully updated to {new_version}')
else:
print('Failed to update database version')
# Example with version checking
current_version = '1.5.0'
target_version = '2.0.0'
if update_database_version(target_version):
print(f'Migration from {current_version} to {target_version} completed')
else:
print('Migration failed, rolling back...')
Best Practices
- Ensure the logger variable is defined in the module scope before calling this function
- Always check the return value to verify the update succeeded before proceeding with dependent operations
- Use semantic versioning (e.g., 'MAJOR.MINOR.PATCH') for the version parameter to maintain consistency
- Ensure at least one CDocs node exists in the database before calling this function, or it will return False
- Consider wrapping this function in a transaction if it's part of a larger migration process
- The function only updates the first matching CDocs node (LIMIT 1), so ensure your database schema has a single CDocs configuration node
- Monitor logs for warnings and errors to detect version update failures in production environments
- The updated_at timestamp is automatically set to the current datetime in the database's timezone
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function update_schema_v1 80.1% similar
-
function update_schema 78.8% similar
-
function check_database_version 78.5% similar
-
function initialize_schema_v1 58.9% similar
-
function create_document_version_v1 57.9% similar