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§

ast
SQL Abstract Syntax Tree (AST) types
dialect

Structs§

Column
A column in a database table
Function
A function in the database
ObjectNameAndType
An object and its type
Schema
The database schema, built from parsing one or more SQL statements.
Table
A table in the database
View
A view in the database

Enums§

Error
Errors that can occur while parsing SQL and updating the schema
SchemaObjectType
The type of an object in the Schema

Functions§

index_full_name
Given an index name and the table it’s on calculate the name with schema.
name_with_schema
Apply a schema to a name
normalized_name
Name, buf if the schema is “public” then remove it.
object_schema_and_name
Extract a name into the name and an optional schema.
table_constraint_name
Get the name of a table constraint