5 releases

new 0.1.5 Apr 17, 2024
0.1.3 Jan 31, 2024
0.1.2 Jan 17, 2024
0.1.1 Jan 17, 2024
0.1.0 Jan 16, 2024

#2588 in Database interfaces


Used in filigree-cli

MIT/Apache

32KB
710 lines

sql-migration-sim

This package reads multiple SQL statements, as from database migrations, and simulates the DDL statements to figure out the resulting schema from running them all.


lib.rs:

This library is meant to parse multiple related SQL migration files, and calculate the final schema that results from running them in order.

Example

use sql_migration_sim::{Schema, Error, ast::DataType};

let mut schema = Schema::new();

let create_statement = r##"CREATE TABLE ships (
   id BIGINT PRIMARY KEY,
   name TEXT NOT NULL,
   mast_count INT not null
);"##;

let alter = r##"
    ALTER TABLE ships ALTER COLUMN mast_count DROP NOT NULL;
    ALTER TABLE ships ADD COLUMN has_motor BOOLEAN NOT NULL;
    "##;

schema.apply_sql(create_statement)?;
schema.apply_sql(alter)?;


let result = schema.tables.get("ships").unwrap();
assert_eq!(result.columns.len(), 4);
assert_eq!(result.columns[0].name(), "id");
assert!(matches!(result.columns[0].data_type, DataType::BigInt(_)));
assert_eq!(result.columns[0].not_null(), true);
assert_eq!(result.columns[1].name(), "name");
assert_eq!(result.columns[1].not_null(), true);
assert_eq!(result.columns[2].name(), "mast_count");
assert_eq!(result.columns[2].not_null(), false);
assert_eq!(result.columns[3].name(), "has_motor");
assert_eq!(result.columns[3].not_null(), true);


Dependencies

~1.4–2MB
~44K SLoC