🛠️ This documentation is still under construction
Rust API
Consolidate

Consolidate Version Source

The consolidate_version_source function helps you optimize and clean up your version source files. It merges all schema changes and default data into a single, up-to-date version, making your version source easier to maintain and more efficient to apply.

What Consolidation Does

When you run consolidation, it:

  1. Combines all table definitions: Merges all createtable and altertable operations (add, modify, drop, rename columns, etc.) into a single, up-to-date schema for each table.
  2. Handles column renames and drops: Tracks column renames and drops across all versions, so the final schema reflects all changes.
  3. Merges default data: Combines all default_data entries for each table, applying column renames and drops as needed.
  4. Keeps only the latest version: The output version source contains a single version entry, using the latest version number from your original file.
  5. Non-destructive: The original file is never modified. The consolidated file is saved as a new file with a timestamp.

Usage

To consolidate a version source in Rust, use the consolidate_version_source function from the alphadb crate:

use std::fs;
use alphadb::utils::consolidate::consolidate_version_source;
 
let version_source = fs::read_to_string("path/to/version_source.json").expect("Failed to read file");
let consolidated = consolidate_version_source(version_source).expect("Consolidation failed");
 
// Write the consolidated version source to a new file
fs::write("path/to/consolidated_version_source.json", serde_json::to_string_pretty(&consolidated).unwrap()).expect("Failed to write file");

Example

Suppose you have a version source with multiple versions, including column renames, drops, and default data:

{
  "name": "example-db",
  "version": [
    {
      "_id": "1.0.0",
      "createtable": {
        "users": {
          "primary_key": "id",
          "id": { "type": "INT", "a_i": true },
          "username": { "type": "VARCHAR", "length": 100 },
          "email": { "type": "VARCHAR", "length": 255 }
        }
      },
      "default_data": {
        "users": [
          { "id": 1, "username": "alice", "email": "[email protected]" }
        ]
      }
    },
    {
      "_id": "1.1.0",
      "altertable": {
        "users": {
          "renamecolumn": { "username": "name" },
          "addcolumn": {
            "created_at": { "type": "DATETIME" }
          }
        }
      }
    },
    {
      "_id": "1.2.0",
      "altertable": {
        "users": {
          "dropcolumn": ["email"]
        }
      },
      "default_data": {
        "users": [
          { "id": 2, "name": "bob", "created_at": "2024-04-30 10:00:00" }
        ]
      }
    }
  ]
}

After consolidation, the version source will be optimized to:

{
  "name": "example-db",
  "version": [
    {
      "_id": "1.2.0",
      "createtable": {
        "users": {
          "primary_key": "id",
          "id": { "type": "INT", "a_i": true },
          "name": { "type": "VARCHAR", "length": 100 },
          "created_at": { "type": "DATETIME" }
        }
      },
      "default_data": {
        "users": [
          { "id": 1, "name": "alice" },
          { "id": 2, "name": "bob", "created_at": "2024-04-30 10:00:00" }
        ]
      }
    }
  ]
}

Notice how:

  • The username column was renamed to name.
  • The email column was dropped.
  • The created_at column was added.
  • All default data is merged, and columns reflect the final schema.

Best Practices

  1. Always keep a backup of your original version source files.
  2. Review the consolidated version source before using it.
  3. Use consolidation when you have accumulated many small changes.
  4. Consider consolidating before major releases.

Notes

  • The consolidation process is safe and non-destructive.
  • The original version source file is not modified.
  • The consolidated file maintains the same functionality as the original, but is easier to read and apply.