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
//! A module that represents tables and columns
//!
//! A table is a collection of columns and some metadata. Creating
//! a table gives you access to the metadata fields that can only
//! be set when creating the table.
//!
//! You can also change existing tables with a closure that can
//! then access individual columns in that table.

use super::{
    backend::SqlGenerator, ConstraintChange, ForeignKeyChange, IndexChange, PrimaryKeyChange,
    TableChange,
};
use crate::types::Type;
use std::fmt::{Debug, Formatter, Result as FmtResult};

impl Debug for TableChange {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        f.write_str("TableChange")
    }
}

impl Debug for IndexChange {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        f.write_str("IndexChange")
    }
}

impl Debug for ConstraintChange {
    fn fmt(&self, f: &mut Formatter) -> FmtResult {
        f.write_str("ConstraintChange")
    }
}

impl Debug for ForeignKeyChange {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        f.write_str("ForeignKeyChange")
    }
}

impl Debug for PrimaryKeyChange {
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        f.write_str("PrimaryKeyChange")
    }
}

#[derive(Debug, Clone)]
pub struct Table {
    pub meta: TableMeta,
    columns: Vec<TableChange>,
    indices: Vec<IndexChange>,
    constraints: Vec<ConstraintChange>,
    foreign_keys: Vec<ForeignKeyChange>,
    primary_key: Option<PrimaryKeyChange>,
}

#[derive(Debug, Clone)]
pub struct SqlChanges {
    pub(crate) columns: Vec<String>,
    pub(crate) indices: Vec<String>,
    pub(crate) constraints: Vec<String>,
    pub(crate) foreign_keys: Vec<String>,
    pub(crate) primary_key: Option<String>,
}

impl Table {
    pub fn new<S: Into<String>>(name: S) -> Self {
        Self {
            meta: TableMeta::new(name.into()),
            columns: vec![],
            indices: vec![],
            constraints: vec![],
            foreign_keys: vec![],
            primary_key: None,
        }
    }

    /// Add a new column to a table
    ///
    /// ```rust
    /// # use barrel::{types, Migration};
    /// # let mut m = Migration::new();
    /// # m.create_table("users", |table| {
    /// table.add_column("id", types::primary());
    /// table.add_column("name", types::varchar(64));
    /// # });
    /// ```
    pub fn add_column<S: Into<String>>(&mut self, name: S, _type: Type) -> &mut Type {
        self.columns
            .push(TableChange::AddColumn(name.into(), _type));

        match self.columns.last_mut().unwrap() {
            &mut TableChange::AddColumn(_, ref mut c) => c,
            _ => unreachable!(),
        }
    }

    pub fn drop_column<S: Into<String>>(&mut self, name: S) {
        self.columns.push(TableChange::DropColumn(name.into()));
    }

    pub fn rename_column<S: Into<String>>(&mut self, old: S, new: S) {
        self.columns
            .push(TableChange::RenameColumn(old.into(), new.into()));
    }

    /// Inject a line of custom SQL into the table block
    ///
    /// This is a bypass to the barrel typesystem, in case there is
    /// something your database supports that barrel doesn't, or if
    /// there is an issue with the way that barrel represents types.
    /// It does however mean that the SQL provided needs to be
    /// specific for one database, meaning that future migrations
    /// might become cumbersome.
    pub fn inject_custom<S: Into<String>>(&mut self, sql: S) {
        self.columns.push(TableChange::CustomLine(sql.into()));
    }

    /// Add a new index to a table, spanning over multiple columns
    pub fn add_index<S: Into<String>>(&mut self, name: S, columns: Type) {
        match columns.inner {
            crate::types::BaseType::Index(_) => {}
            _ => panic!("Calling `add_index` with a non-`Index` type is not allowed!"),
        }

        self.indices.push(IndexChange::AddIndex {
            table: self.meta.name.clone(),
            index: name.into(),
            columns,
        });
    }

    pub fn add_constraint<S: Into<String>>(&mut self, name: S, columns: Type) {
        match columns.inner {
            crate::types::BaseType::Constraint(_, _) => {}
            _ => panic!("Calling `add_index` with a non-`Constraint` type is not allowed!"),
        }

        self.constraints.push(ConstraintChange::AddConstraint {
            index: name.into(),
            columns,
        });
    }

    pub fn add_partial_index<S: Into<String>>(&mut self, name: S, columns: Type, conditions: S) {
        match columns.inner {
            crate::types::BaseType::Index(_) => {}
            _ => panic!("Calling `add_index` with a non-`Index` type is not allowed!"),
        }

        self.indices.push(IndexChange::AddPartialIndex {
            table: self.meta.name.clone(),
            index: name.into(),
            columns,
            conditions: conditions.into(),
        });
    }

    /// Drop an index on this table
    pub fn drop_index<S: Into<String>>(&mut self, name: S) {
        self.indices.push(IndexChange::RemoveIndex(
            self.meta.name.clone(),
            name.into(),
        ));
    }

    pub fn add_foreign_key(
        &mut self,
        columns_on_this_side: &[&str],
        related_table: &str,
        columns_on_that_side: &[&str],
    ) {
        let table = related_table.into();

        let columns = columns_on_this_side
            .into_iter()
            .map(|c| String::from(*c))
            .collect();

        let relation_columns = columns_on_that_side
            .into_iter()
            .map(|c| String::from(*c))
            .collect();

        self.foreign_keys.push(ForeignKeyChange::AddForeignKey {
            table,
            columns,
            relation_columns,
        })
    }

    pub fn set_primary_key(&mut self, columns: &[&str]) {
        let primary_key =
            PrimaryKeyChange::AddPrimaryKey(columns.into_iter().map(|s| s.to_string()).collect());

        self.primary_key = Some(primary_key);
    }

    /// Generate Sql for this table.
    pub fn make<T: SqlGenerator>(&mut self, ex: bool, schema: Option<&str>) -> SqlChanges {
        use ConstraintChange as CC;
        use ForeignKeyChange as KFC;
        use IndexChange as IC;
        use PrimaryKeyChange as PKC;
        use TableChange as TC;

        let columns = self
            .columns
            .iter_mut()
            .map(|change| match change {
                &mut TC::AddColumn(ref name, ref col) => T::add_column(ex, schema, name, &col),
                &mut TC::DropColumn(ref name) => T::drop_column(name),
                &mut TC::RenameColumn(ref old, ref new) => T::rename_column(old, new),
                &mut TC::ChangeColumn(ref mut name, _, _) => T::alter_table(name, schema),
                &mut TC::CustomLine(ref sql) => sql.clone(),
            })
            .collect();

        let indices = self
            .indices
            .iter()
            .map(|change| match change {
                IC::AddIndex {
                    index,
                    table,
                    columns,
                } => T::create_index(table, schema, index, columns),
                IC::AddPartialIndex {
                    index,
                    table,
                    columns,
                    conditions,
                } => T::create_partial_index(table, schema, index, columns, conditions),
                IC::RemoveIndex(_, index) => T::drop_index(index),
            })
            .collect();

        let primary_key = self.primary_key.as_ref().map(|pk| match pk {
            PKC::AddPrimaryKey(ref cols) => T::add_primary_key(cols),
        });

        let constraints = self
            .constraints
            .iter()
            .map(|c| match c {
                CC::AddConstraint { index, columns } => T::create_constraint(index, columns),
            })
            .collect();

        let foreign_keys = self
            .foreign_keys
            .iter()
            .map(|change| match change {
                KFC::AddForeignKey {
                    columns,
                    table,
                    relation_columns,
                } => T::add_foreign_key(
                    columns.as_slice(),
                    table,
                    relation_columns.as_slice(),
                    schema,
                ),
            })
            .collect();

        SqlChanges {
            columns,
            indices,
            constraints,
            foreign_keys,
            primary_key,
        }
    }
}

/// Some metadata about a table that was just created
#[derive(Debug, Clone)]
pub struct TableMeta {
    pub name: String,
    pub encoding: String,
}

impl TableMeta {
    /// Create a new tablemeta with default values
    pub fn new(name: String) -> Self {
        Self {
            name,
            encoding: "utf-8".to_owned(),
        }
    }

    /// Get a clone of the table name
    pub fn name(&self) -> String {
        self.name.clone()
    }

    /// Specify an encoding for this table which might vary from the main encoding
    /// of your database
    pub fn encoding<S: Into<String>>(&mut self, enc: S) -> &mut TableMeta {
        self.encoding = enc.into();
        self
    }
}