🔍 Code Extractor

function migrate_from_legacy

Maturity: 46

Flask API endpoint that migrates data from a legacy system to the current database by accepting a data directory path and executing a migration process.

File:
/tf/active/vicechatdev/vice_ai/new_app.py
Lines:
1795 - 1808
Complexity:
moderate

Purpose

This endpoint serves as a REST API route for system administrators to migrate historical data from a legacy system into the current application's database. It accepts a POST request with a legacy data directory path, instantiates a LegacySystemMigrator, executes the migration, and returns a detailed migration report. The endpoint is protected by authentication and includes error handling with logging.

Source Code

def migrate_from_legacy():
    """Migrate data from legacy system"""
    data = request.get_json()
    legacy_data_dir = data.get('data_dir', './data')
    
    try:
        migrator = LegacySystemMigrator(legacy_data_dir, DATABASE_PATH)
        migration_report = migrator.run_migration()
        
        return jsonify(migration_report)
        
    except Exception as e:
        logger.error(f"Migration error: {e}")
        return jsonify({'error': str(e)}), 500

Return Value

Returns a JSON response containing a migration report dictionary with details about the migration process (success/failure counts, migrated items, etc.) on success with HTTP 200. On error, returns a JSON object with an 'error' key containing the error message string and HTTP status code 500.

Dependencies

  • flask
  • logging

Required Imports

from flask import request
from flask import jsonify
from migration import LegacySystemMigrator
import logging

Usage Example

# Client-side usage example
import requests

# Assuming the Flask app is running on localhost:5000
url = 'http://localhost:5000/api/setup/migrate'
headers = {'Authorization': 'Bearer YOUR_AUTH_TOKEN', 'Content-Type': 'application/json'}
payload = {'data_dir': '/path/to/legacy/data'}

response = requests.post(url, json=payload, headers=headers)

if response.status_code == 200:
    migration_report = response.json()
    print(f"Migration completed: {migration_report}")
else:
    error = response.json()
    print(f"Migration failed: {error['error']}")

Best Practices

  • Ensure the legacy data directory path is validated and sanitized before passing to the migrator to prevent directory traversal attacks
  • The endpoint requires authentication via the require_auth decorator - ensure valid credentials are provided
  • Monitor the migration process as it may be time-consuming for large datasets; consider implementing timeout handling or async processing for production use
  • Review the migration report returned to verify all expected data was migrated successfully
  • Backup the current database before running migrations to prevent data loss
  • The endpoint uses a global DATABASE_PATH constant - ensure this is properly configured before deployment
  • Consider implementing idempotency checks to prevent duplicate migrations if the endpoint is called multiple times
  • Log files should be monitored for migration errors as detailed error information is logged via the logger

Similar Components

AI-powered semantic similarity - components with related functionality:

  • function create_sample_data 70.9% similar

    Flask API endpoint that creates sample data for testing purposes by invoking the LegacySystemMigrator's create_sample_data method and returns the generated data as JSON.

    From: /tf/active/vicechatdev/vice_ai/new_app.py
  • function main_v13 60.6% similar

    Command-line interface function that orchestrates database migration from a legacy document AI system to a new architecture, with options for verification and sample data creation.

    From: /tf/active/vicechatdev/vice_ai/migration.py
  • class LegacySystemMigrator 60.2% similar

    Migrates data from a legacy file-based document system to a new database-backed architecture with TextSection-centric design, including document metadata, sections, chat configurations, and version history.

    From: /tf/active/vicechatdev/vice_ai/migration.py
  • function upload_data 52.6% similar

    Flask route handler that accepts file uploads via POST request, validates the file, saves it with a timestamp, and loads the data into an analysis session.

    From: /tf/active/vicechatdev/full_smartstat/app.py
  • function create_data_section 51.9% similar

    Flask API endpoint that creates a new data section for authenticated users, accepting title and description from JSON request body.

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