function load_database_schema
Loads a database schema from a JSON file and returns it as a DatabaseSchema object.
/tf/active/vicechatdev/full_smartstat/sql_query_generator.py
1913 - 1915
simple
Purpose
This function serves as a simple loader utility to deserialize database schema information stored in JSON format into a DatabaseSchema object. It's typically used during application initialization or when dynamically loading database configurations. The function delegates the actual parsing logic to the DatabaseSchema class's from_json method.
Source Code
def load_database_schema(schema_file: str) -> DatabaseSchema:
"""Load database schema from JSON file"""
return DatabaseSchema.from_json(schema_file)
Parameters
| Name | Type | Default | Kind |
|---|---|---|---|
schema_file |
str | - | positional_or_keyword |
Parameter Details
schema_file: A string representing the file path to the JSON file containing the database schema. Can be an absolute or relative path. The file should contain valid JSON that can be parsed by DatabaseSchema.from_json method.
Return Value
Type: DatabaseSchema
Returns a DatabaseSchema object that represents the parsed database schema. This object contains the structure, tables, columns, relationships, and other metadata defined in the JSON file. The exact structure depends on the DatabaseSchema class implementation.
Dependencies
jsonloggingpathlibdataclassestypingcolumn_mappingdatetimeurllibreimportlib
Required Imports
from typing import Dict, List, Optional, Any, Tuple
from dataclasses import dataclass
from pathlib import Path
import json
import logging
import os
from column_mapping import COLUMN_MAPPING, RECOMMENDED_JOINS, PROBLEMATIC_TABLES, RECOMMENDED_RESULT_TABLES
from datetime import datetime
from urllib.parse import quote_plus
import re
import importlib.util
Usage Example
# Assuming DatabaseSchema class is defined
schema_file_path = 'database_schema.json'
try:
db_schema = load_database_schema(schema_file_path)
print(f'Successfully loaded schema: {db_schema}')
except FileNotFoundError:
print(f'Schema file not found: {schema_file_path}')
except json.JSONDecodeError:
print('Invalid JSON format in schema file')
except Exception as e:
print(f'Error loading schema: {e}')
Best Practices
- Ensure the schema_file path is valid and the file exists before calling this function
- Handle potential exceptions such as FileNotFoundError, json.JSONDecodeError, or custom exceptions from DatabaseSchema.from_json
- Validate the returned DatabaseSchema object to ensure it contains expected data
- Consider caching the loaded schema if it will be used multiple times to avoid repeated file I/O
- Use absolute paths or Path objects for better cross-platform compatibility
- Ensure proper error handling around this function call as file operations can fail
- The function assumes DatabaseSchema.from_json handles all parsing and validation logic
Similar Components
AI-powered semantic similarity - components with related functionality:
-
class DatabaseSchema_v1 69.0% similar
-
function get_default_schema_file 64.4% similar
-
function load_connection_config 62.5% similar
-
function get_default_schema_file_v1 58.5% similar
-
function load_all_documents 57.7% similar