🔍 Code Extractor

function load_database_schema

Maturity: 41

Loads a database schema from a JSON file and returns it as a DatabaseSchema object.

File:
/tf/active/vicechatdev/full_smartstat/sql_query_generator.py
Lines:
1913 - 1915
Complexity:
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

  • json
  • logging
  • pathlib
  • dataclasses
  • typing
  • column_mapping
  • datetime
  • urllib
  • re
  • importlib

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

    A dataclass that represents database schema information, including table categories, relationships, and system architecture. Provides functionality to load schema from JSON files.

    From: /tf/active/vicechatdev/smartstat/sql_query_generator.py
  • function get_default_schema_file 64.4% similar

    Returns the absolute file path to the default database schema JSON file (database_schema_20251003_120434.json) located in the same directory as the function.

    From: /tf/active/vicechatdev/full_smartstat/sql_query_generator.py
  • function load_connection_config 62.5% similar

    Loads database connection configuration from a specified configuration file and returns a ConnectionConfig object.

    From: /tf/active/vicechatdev/full_smartstat/sql_query_generator.py
  • function get_default_schema_file_v1 58.5% similar

    Returns the absolute file path to the default database schema JSON file (Poulpharm_labosoft.json) located in the same directory as the function's module.

    From: /tf/active/vicechatdev/smartstat/sql_query_generator.py
  • function load_all_documents 57.7% similar

    Loads all JSON documents from a designated documents directory and returns them as a dictionary indexed by document ID.

    From: /tf/active/vicechatdev/vice_ai/complex_app.py
← Back to Browse