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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
//! This library is meant to parse multiple related SQL migration files, and calculate the final
//! schema that resutls 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);
//!
//! # Ok::<(), Error>(())
//!
//! ```
//!

#![warn(missing_docs)]
use std::{
    collections::HashMap,
    ops::{Deref, DerefMut},
};

use sqlparser::ast::{
    AlterColumnOperation, AlterTableOperation, ColumnDef, ColumnOption, ColumnOptionDef,
    ObjectType, Statement,
};
pub use sqlparser::{ast, dialect::Dialect};

/// A column in a database table
#[derive(Debug)]
pub struct Column(pub ColumnDef);

impl Deref for Column {
    type Target = ColumnDef;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl DerefMut for Column {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl Column {
    /// The name of the column
    pub fn name(&self) -> &str {
        self.name.value.as_str()
    }

    /// Whether the column is nullable or not
    pub fn not_null(&self) -> bool {
        self.options
            .iter()
            .find_map(|o| match o.option {
                ColumnOption::Null => Some(false),
                ColumnOption::NotNull => Some(true),
                ColumnOption::Unique { is_primary } => is_primary.then_some(true),
                _ => None,
            })
            .unwrap_or(false)
    }
}

/// A table in the database
pub struct Table {
    /// The name of the table
    pub name: String,
    /// The columns in the table
    pub columns: Vec<Column>,
}

/// A view in the database
pub struct View {
    /// The name of the view
    pub name: String,
    /// The columns in the view
    pub columns: Vec<String>,
}

/// Errors that can occur while parsing SQL and updating the schema
#[derive(Debug, thiserror::Error)]
pub enum Error {
    /// Encountered an ALTER TABLE statement on a nonexistent table.
    #[error("Attempted to alter a table {0} that does not exist")]
    AlteredMissingTable(String),
    /// Encountered an ALTER COLUMN statement on a nonexistent column.
    #[error("Attempted to alter a column {0} that does not exist in table {1}")]
    AlteredMissingColumn(String, String),
    /// Attempted to create a table that already exists
    #[error("Attempted to create table {0} that already exists")]
    TableAlreadyExists(String),
    /// Attempted to create a column that already exists
    #[error("Attempted to create column {0} that already exists in table {1}")]
    ColumnAlreadyExists(String, String),
    /// The SQL parser encountered an error
    #[error("SQL Parse Error {0}")]
    Parse(#[from] sqlparser::parser::ParserError),
    /// Error reading a file
    #[error("Failed to read file {filename}")]
    File {
        /// The underlying error
        #[source]
        source: std::io::Error,
        /// The name of the file on which the error occurred
        filename: String,
    },
}

/// The database schema, built from parsing one or more SQL statements.
pub struct Schema {
    dialect: Box<dyn Dialect>,
    /// The tables in the schema
    pub tables: HashMap<String, Table>,
    /// The views in the schema
    pub views: HashMap<String, View>,
}

impl Schema {
    /// Create a new [Schema] that parses with a generic SQL dialect
    pub fn new() -> Self {
        Self::new_with_dialect(sqlparser::dialect::GenericDialect {})
    }

    /// Create a new [Schema] that parses with the given SQL dialect
    pub fn new_with_dialect<D: Dialect>(dialect: D) -> Self {
        let dialect = Box::new(dialect);
        Self {
            tables: HashMap::new(),
            views: HashMap::new(),
            dialect,
        }
    }

    fn create_table(&mut self, name: String, columns: Vec<ColumnDef>) -> Result<(), Error> {
        if self.tables.contains_key(&name) {
            return Err(Error::TableAlreadyExists(name));
        }

        self.tables.insert(
            name.clone(),
            Table {
                name,
                columns: columns.into_iter().map(Column).collect(),
            },
        );

        Ok(())
    }

    fn create_view(
        &mut self,
        name: String,
        or_replace: bool,
        columns: Vec<String>,
    ) -> Result<(), Error> {
        if !or_replace && self.views.contains_key(&name) {
            return Err(Error::TableAlreadyExists(name));
        }

        self.views.insert(name.clone(), View { name, columns });

        Ok(())
    }

    fn apply_statement(&mut self, statement: Statement) -> Result<(), Error> {
        match statement {
            Statement::CreateTable { name, columns, .. } => {
                self.create_table(name.to_string(), columns)?;
            }
            Statement::AlterTable {
                name, operations, ..
            } => {
                let name = name.to_string();
                for operation in operations {
                    match operation {
                        AlterTableOperation::AddColumn {
                            if_not_exists,
                            column_def,
                            ..
                        } => {
                            let table = self
                                .tables
                                .get_mut(&name)
                                .ok_or_else(|| Error::AlteredMissingTable(name.clone()))?;

                            let existing_column =
                                table.columns.iter().find(|c| c.name == column_def.name);

                            if existing_column.is_none() {
                                table.columns.push(Column(column_def));
                            } else if !if_not_exists {
                                return Err(Error::ColumnAlreadyExists(
                                    column_def.name.value,
                                    name.clone(),
                                ));
                            }
                        }

                        AlterTableOperation::DropColumn { column_name, .. } => {
                            let table = self
                                .tables
                                .get_mut(&name)
                                .ok_or_else(|| Error::AlteredMissingTable(name.clone()))?;
                            table.columns.retain(|c| c.name != column_name);
                        }

                        AlterTableOperation::RenameColumn {
                            old_column_name,
                            new_column_name,
                        } => {
                            let table = self
                                .tables
                                .get_mut(&name)
                                .ok_or_else(|| Error::AlteredMissingTable(name.clone()))?;

                            let column = table
                                .columns
                                .iter_mut()
                                .find(|c| c.name == old_column_name)
                                .ok_or_else(|| {
                                    Error::AlteredMissingColumn(
                                        old_column_name.value.clone(),
                                        name.clone(),
                                    )
                                })?;
                            column.name = new_column_name;
                        }

                        AlterTableOperation::RenameTable {
                            table_name: new_table_name,
                        } => {
                            let new_table_name = new_table_name.to_string();
                            let mut table = self.tables.remove(&name).ok_or_else(|| {
                                Error::AlteredMissingTable(new_table_name.clone())
                            })?;
                            table.name = new_table_name.clone();
                            // TODO this probably doesn't properly handle tables that are in a
                            // non-default schema
                            self.tables.insert(new_table_name, table);
                        }

                        AlterTableOperation::AlterColumn { column_name, op } => {
                            let table = self
                                .tables
                                .get_mut(&name)
                                .ok_or_else(|| Error::AlteredMissingTable(name.clone()))?;

                            let column = table
                                .columns
                                .iter_mut()
                                .find(|c| c.name == column_name)
                                .ok_or_else(|| {
                                Error::AlteredMissingColumn(
                                    table.name.clone(),
                                    column_name.value.clone(),
                                )
                            })?;

                            match op {
                                AlterColumnOperation::SetNotNull => {
                                    if column
                                        .options
                                        .iter()
                                        .find(|o| o.option == ColumnOption::NotNull)
                                        .is_none()
                                    {
                                        column.options.push(ColumnOptionDef {
                                            name: None,
                                            option: ColumnOption::NotNull,
                                        });
                                    }

                                    column.options.retain(|o| o.option != ColumnOption::Null);
                                }
                                AlterColumnOperation::DropNotNull => {
                                    column.options.retain(|o| o.option != ColumnOption::NotNull);
                                }
                                AlterColumnOperation::SetDefault { value } => {
                                    if let Some(default_option) = column
                                        .options
                                        .iter_mut()
                                        .find(|o| matches!(o.option, ColumnOption::Default(_)))
                                    {
                                        default_option.option = ColumnOption::Default(value);
                                    } else {
                                        column.options.push(ColumnOptionDef {
                                            name: None,
                                            option: ColumnOption::Default(value),
                                        })
                                    }
                                }
                                AlterColumnOperation::DropDefault => {
                                    column
                                        .options
                                        .retain(|o| !matches!(o.option, ColumnOption::Default(_)));
                                }

                                AlterColumnOperation::SetDataType { data_type, .. } => {
                                    column.data_type = data_type
                                }
                            }
                        }

                        _ => {}
                    }
                }
            }
            Statement::CreateView {
                name,
                columns,
                or_replace,
                ..
            } => {
                self.create_view(
                    name.to_string(),
                    or_replace,
                    columns.into_iter().map(|c| c.value).collect(),
                )?;
            }

            Statement::Drop {
                object_type, names, ..
            } => {
                for name in names {
                    let name = name.to_string();
                    match object_type {
                        ObjectType::Table => {
                            self.tables.remove(&name);
                        }
                        ObjectType::View => {
                            self.views.remove(&name);
                        }
                        _ => {}
                    }
                }
            }
            _ => {}
        }

        Ok(())
    }

    /// Apply one or more SQL statements to the schema
    pub fn apply_sql(&mut self, sql: &str) -> Result<(), Error> {
        sqlparser::parser::Parser::new(self.dialect.as_ref())
            .try_with_sql(sql)?
            .parse_statements()?
            .into_iter()
            .try_for_each(|statement| self.apply_statement(statement))
    }

    /// Read a SQL file and apply its contents to the schema
    pub fn apply_file(&mut self, filename: &str) -> Result<(), Error> {
        let contents = std::fs::read_to_string(filename).map_err(|e| Error::File {
            source: e,
            filename: filename.to_string(),
        })?;

        self.apply_sql(&contents)
    }
}