Crate sql_migration_sim
source ·Expand description
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);
Modules§
- SQL Abstract Syntax Tree (AST) types
Structs§
- A column in a database table
- A function in the database
- An object and its type
- The database schema, built from parsing one or more SQL statements.
- A table in the database
- A view in the database
Enums§
- Errors that can occur while parsing SQL and updating the schema
- The type of an object in the Schema
Functions§
- Given an index name and the table it’s on calculate the name with schema.
- Apply a schema to a name
- Name, buf if the schema is “public” then remove it.
- Extract a name into the name and an optional schema.
- Get the name of a table constraint