function migrate_from_legacy
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.
/tf/active/vicechatdev/vice_ai/new_app.py
1795 - 1808
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
flasklogging
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
Tags
Similar Components
AI-powered semantic similarity - components with related functionality:
-
function create_sample_data 70.9% similar
-
function main_v13 60.6% similar
-
class LegacySystemMigrator 60.2% similar
-
function upload_data 52.6% similar
-
function create_data_section 51.9% similar