1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//! Schema-change cookbook.
//!
//! Every builder returns a self-verifying [`crate::Operation`]: it has a stable
//! id, human-readable label, prechecks, execute steps, and postchecks. Add the
//! resulting operations to a [`crate::Migration`] and apply the migration with
//! an adapter crate. A passing postcheck makes the operation a safe no-op.
//!
//! # Tables and columns
//!
//! These builders work with both built-in dialects where the underlying SQL is
//! supported. `ColumnDef` type and default strings are authored SQL, so use the
//! syntax of the selected database.
//!
//! ```rust
//! use migratrom::operations::{add_column, create_table, rename_column, rename_table};
//! use migratrom::{ColumnDef, PrimaryKey, SqliteDialect};
//!
//! # fn main() -> Result<(), migratrom::MigratromError> {
//! let dialect = SqliteDialect;
//! let _create = create_table(
//! "main", "users",
//! &[ColumnDef::new("id", "INTEGER"), ColumnDef::new("email", "TEXT")],
//! Some(&PrimaryKey { columns: vec!["id".into()] }),
//! &dialect,
//! )?;
//! let _add = add_column(
//! "main", "users", &ColumnDef::new("display_name", "TEXT").nullable(true), &dialect,
//! )?;
//! let _rename_table = rename_table("main", "users", "accounts", &dialect)?;
//! let _rename_column = rename_column("main", "accounts", "display_name", "name", &dialect)?;
//! # Ok(())
//! # }
//! ```
//!
//! # PostgreSQL column changes and constraints
//!
//! `set_column_default`, `set_column_not_null`, and the constraint builders
//! require PostgreSQL capabilities. They return [`crate::MigratromError::Unsupported`]
//! with [`crate::SqliteDialect`].
//!
//! ```rust
//! use migratrom::operations::{
//! add_check, add_foreign_key, add_primary_key, add_unique, set_column_default,
//! set_column_not_null,
//! };
//! use migratrom::{ForeignKeyReference, ForeignKeySpec, PostgresDialect, ReferentialAction};
//!
//! # fn main() -> Result<(), migratrom::MigratromError> {
//! let dialect = PostgresDialect;
//! let _default = set_column_default("public", "users", "role", "DEFAULT 'member'", &dialect)?;
//! let _not_null = set_column_not_null("public", "users", "email", &dialect)?;
//! let _unique = add_unique("public", "users", "users_email_key", &["email".into()], &dialect)?;
//! let _check = add_check("public", "users", "users_email_nonempty", "email <> ''", &dialect)?;
//! let _primary_key = add_primary_key("public", "events", "events_pkey", &["id".into()], &dialect)?;
//! let _foreign_key = add_foreign_key(
//! "public", "posts",
//! &ForeignKeySpec {
//! name: "posts_author_id_fkey".into(),
//! columns: vec!["author_id".into()],
//! references: ForeignKeyReference { table: "users".into(), columns: vec!["id".into()] },
//! on_delete: Some(ReferentialAction::Cascade),
//! on_update: None,
//! },
//! &dialect,
//! )?;
//! # Ok(())
//! # }
//! ```
//!
//! # Indexes
//!
//! A normal index works with both dialects. Setting
//! [`crate::CreateIndexOptions::concurrently`] uses PostgreSQL's `CREATE INDEX
//! CONCURRENTLY`; the operation is marked [`crate::Operation::outside_transaction`]
//! and is rejected by SQLite.
//!
//! ```rust
//! use migratrom::operations::create_index;
//! use migratrom::{CreateIndexOptions, PostgresDialect};
//!
//! # fn main() -> Result<(), migratrom::MigratromError> {
//! let dialect = PostgresDialect;
//! let _index = create_index(
//! "public", "users", "users_email_idx", &["email".into()],
//! &CreateIndexOptions { concurrently: true }, &dialect,
//! )?;
//! # Ok(())
//! # }
//! ```
//!
//! # PostgreSQL schema objects
//!
//! Schemas, extensions, sequences, enum types, enum values, and materialized
//! views are PostgreSQL-only. Regular views work with both built-in dialects.
//!
//! ```rust
//! use migratrom::operations::{
//! add_enum_value, create_extension, create_materialized_view, create_schema,
//! create_sequence, create_type, create_view, AddEnumValueOptions, CreateExtensionOptions,
//! };
//! use migratrom::PostgresDialect;
//!
//! # fn main() -> Result<(), migratrom::MigratromError> {
//! let dialect = PostgresDialect;
//! let _schema = create_schema("app", &dialect)?;
//! let _extension = create_extension(
//! "pgcrypto", &CreateExtensionOptions { schema: Some("app".into()) }, &dialect,
//! )?;
//! let _sequence = create_sequence("app", "event_ids", &dialect)?;
//! let _type = create_type("app", "role", &["member".into(), "admin".into()], &dialect)?;
//! let _enum_value = add_enum_value(
//! "app", "role", "owner", &AddEnumValueOptions { before: Some("admin".into()) }, &dialect,
//! )?;
//! let _view = create_view("app", "active_users", "SELECT * FROM app.users WHERE active", &dialect)?;
//! let _materialized = create_materialized_view(
//! "app", "user_counts", "SELECT count(*) AS total FROM app.users", &dialect,
//! )?;
//! # Ok(())
//! # }
//! ```
//!
//! # Raw SQL
//!
//! Use [`raw_sql`] when the built-in builders do not cover a change. Supply at
//! least one execute step and one boolean postcheck; otherwise the builder returns
//! [`crate::MigratromError::Config`].
//!
//! ```rust
//! use migratrom::operations::{raw_sql, RawCheck, RawSqlInput, RawStep};
//!
//! # fn main() -> Result<(), migratrom::MigratromError> {
//! let _operation = raw_sql(RawSqlInput {
//! label: "Create audit trigger".into(),
//! execute: vec![RawStep::new("CREATE TRIGGER audit_users AFTER INSERT ON users BEGIN SELECT 1; END")],
//! postcheck: vec![RawCheck::new(
//! "SELECT EXISTS (SELECT 1 FROM sqlite_master WHERE type = 'trigger' AND name = 'audit_users')",
//! )],
//! ..Default::default()
//! })?;
//! # Ok(())
//! # }
//! ```
pub use ;
pub use ;
pub use create_index;
pub use ;
pub use ;
pub use ;