Skip to main content

uqa_engine/migration/
run.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Transactional migration orchestration.
8
9use super::{
10    create_tables, ensure_destination_empty, install_secondary_indexes, is_python_catalog,
11    load_catalog_indexes, load_table_specs, migrate_analyzers, migrate_column_stats,
12    migrate_documents, migrate_foreign_servers, migrate_foreign_tables, migrate_graphs,
13    migrate_models, migrate_path_indexes, migrate_scoring_params, migrate_table_field_analyzers,
14    open_read_only, persist_catalog_indexes, reject_same_path, resolve_source_database, Engine,
15    Path, PythonMigrationError, PythonMigrationReport,
16};
17
18pub fn migrate_python_database(
19    source: &Path,
20    destination: &Path,
21) -> Result<PythonMigrationReport, PythonMigrationError> {
22    let source_path = resolve_source_database(source)?;
23    reject_same_path(&source_path, destination)?;
24    ensure_destination_empty(destination)?;
25
26    let source_conn = open_read_only(&source_path)?;
27    if !is_python_catalog(&source_conn)? {
28        return Err(PythonMigrationError::NotPythonCatalog(source_path));
29    }
30
31    let index_rows = load_catalog_indexes(&source_conn)?;
32    let specs = load_table_specs(&source_conn, &index_rows)?;
33    let engine = Engine::open(destination)?;
34
35    if !engine.table_names_in_execution()?.is_empty() || !engine.list_graphs()?.is_empty() {
36        return Err(PythonMigrationError::DestinationNotEmpty(
37            destination.display().to_string(),
38        ));
39    }
40
41    let mut report = PythonMigrationReport {
42        source_path,
43        destination_path: destination.to_path_buf(),
44        tables: 0,
45        documents: 0,
46        fts_fields: 0,
47        vector_fields: 0,
48        indexes: 0,
49        analyzers: 0,
50        table_field_analyzers: 0,
51        foreign_servers: 0,
52        foreign_tables: 0,
53        graphs: 0,
54        graph_vertices: 0,
55        graph_edges: 0,
56        path_indexes: 0,
57        scoring_params: 0,
58        models: 0,
59        column_stats: 0,
60    };
61
62    // The destination starts empty, so migration is one logical catalog/data
63    // replacement. Keep every table, index, registry, document, and graph
64    // write on the engine's pinned connection; a late corrupt source row must
65    // not leave a valid-looking partial destination behind.
66    engine.with_implicit_mapped_transaction(
67        |engine| {
68            report.analyzers = migrate_analyzers(&source_conn, engine)?;
69            create_tables(&source_conn, engine, &specs, &mut report)?;
70            report.table_field_analyzers = migrate_table_field_analyzers(&source_conn, engine)?;
71            install_secondary_indexes(engine, &specs, &mut report)?;
72            migrate_documents(&source_conn, engine, &specs, &mut report)?;
73            report.indexes = persist_catalog_indexes(engine, &index_rows)?;
74            report.column_stats = migrate_column_stats(&source_conn, engine)?;
75            report.scoring_params = migrate_scoring_params(&source_conn, engine)?;
76            report.models = migrate_models(&source_conn, engine)?;
77            report.foreign_servers = migrate_foreign_servers(&source_conn, engine)?;
78            report.foreign_tables = migrate_foreign_tables(&source_conn, engine)?;
79            migrate_graphs(&source_conn, engine, &mut report)?;
80            report.path_indexes = migrate_path_indexes(&source_conn, engine)?;
81            Ok(report)
82        },
83        |error| PythonMigrationError::Invalid(error.to_string()),
84    )
85}