sofie-cli 0.4.0

The `sofie` command: gen, check and the tools reading the app model.
Documentation
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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
//! The module's current schema is read from its migration files, so no database is needed.

use std::fs;

use sofie_check::MigratedSchema;
use sofie_core::sql_lexer::lex;
use sofie_model::Module;

use super::Edits;
use super::declared_tables::{DeclaredColumn, DeclaredIndex, DeclaredTable, declared_tables};
use super::schema::{SQLITE_NEEDS_NO_STEP, create_migration, next_migration_number};
use crate::gen_command::GenError;

pub(super) fn new_schema_migration(
    edits: &mut Edits,
    module: &Module,
    name: &str,
) -> Result<(), GenError> {
    let schema = migrated_schema(module)?;
    let tables = declared_tables(module);
    let plan = MigrationPlan::of(&tables, &schema);
    if plan.is_empty() {
        return Err(GenError::Usage(nothing_to_do(module, &plan.notes)));
    }
    let first = next_migration_number(module);
    for (number, migration) in (first..).zip(plan.migrations(name)) {
        let notes = if number == first {
            plan.comments()
        } else {
            String::new()
        };
        create_migration(
            edits,
            module,
            &format!("{number:04}_{}.sql", migration.name),
            &(notes.clone() + &migration.render(Dialect::Postgres)),
            |prefix| notes + &migration.render(Dialect::Sqlite { prefix }),
        )?;
    }
    Ok(())
}

fn migrated_schema(module: &Module) -> Result<MigratedSchema, GenError> {
    let mut schema = MigratedSchema::default();
    for file in &module.migrations {
        let sql = fs::read_to_string(file).map_err(|source| GenError::Io {
            path: file.clone(),
            source,
        })?;
        schema.read(&lex(&sql));
    }
    Ok(schema)
}

fn nothing_to_do(module: &Module, notes: &[String]) -> String {
    let mut message = format!(
        "module `{}`'s migrations already create every table, column, CHECK and index its \
         aggregates declare; nothing to write",
        module.krate.crate_name
    );
    for note in notes {
        message.push_str("\n  ");
        message.push_str(note);
    }
    message
}

#[derive(Clone, Copy)]
enum Statement<'a> {
    CreateTable(&'a DeclaredTable),
    CreateIndex {
        table: &'a str,
        index: &'a DeclaredIndex,
        concurrently: bool,
    },
    AddColumn {
        table: &'a str,
        column: &'a DeclaredColumn,
    },
    AddCheck {
        table: &'a str,
        column: &'a DeclaredColumn,
        added: bool,
    },
    Validate {
        table: &'a str,
        column: &'a DeclaredColumn,
    },
}

/// Postgres validates a CHECK added `NOT VALID` and builds an index concurrently
/// only in a later migration, so the plan spans several files.
#[derive(Default)]
struct MigrationPlan<'a> {
    /// What the generator leaves to the developer.
    notes: Vec<String>,
    /// Contract steps, written commented out: never destructive by default.
    contracts: Vec<String>,
    expand: Vec<Statement<'a>>,
    validations: Vec<Statement<'a>>,
    concurrent_indexes: Vec<(&'a str, DeclaredIndex)>,
}

struct Migration<'a> {
    name: String,
    statements: Vec<Statement<'a>>,
}

impl<'a> MigrationPlan<'a> {
    fn of(tables: &'a [DeclaredTable], schema: &MigratedSchema) -> Self {
        let mut plan = Self::default();
        for table in tables {
            if schema.knows_table(&table.name) {
                plan.alter(table, schema);
            } else {
                plan.create(table);
            }
        }
        plan
    }

    fn is_empty(&self) -> bool {
        self.contracts.is_empty()
            && self.expand.is_empty()
            && self.validations.is_empty()
            && self.concurrent_indexes.is_empty()
    }

    fn create(&mut self, table: &'a DeclaredTable) {
        self.expand.push(Statement::CreateTable(table));
        self.expand
            .extend(table.indexes.iter().map(|index| Statement::CreateIndex {
                table: &table.name,
                index,
                concurrently: false,
            }));
        for column in &table.columns {
            self.note_bounds(&table.name, column);
        }
        for field in &table.unknown {
            self.note_unknown(&table.name, &field.name, &field.type_name);
        }
        self.notes
            .extend(table.unshaped.iter().map(|index| index.reason.clone()));
    }

    fn alter(&mut self, table: &'a DeclaredTable, schema: &MigratedSchema) {
        for column in &table.columns {
            if schema.has_column(&table.name, &column.name) {
                self.check_existing(&table.name, column, schema);
            } else {
                self.add_column(&table.name, column);
            }
        }
        for index in &table.indexes {
            if !is_present(&table.name, index, schema) {
                self.add_index(&table.name, index, schema);
            }
        }
        for index in &table.unshaped {
            if !schema.has_index(&format!("{}_{}", table.name, index.suffix)) {
                self.notes.push(index.reason.clone());
            }
        }
        let existing = schema.columns(&table.name);
        for field in &table.unknown {
            if !existing.iter().any(|column| field.may_store(column)) {
                self.note_unknown(&table.name, &field.name, &field.type_name);
            }
        }
        for column in existing {
            if column != "id" && !table.may_store(column) {
                self.notes.push(format!(
                    "Contract step, once no running version reads `{}.{column}`:\n\
                     ALTER TABLE {} DROP COLUMN {};",
                    table.name,
                    quoted(&table.name),
                    quoted(column)
                ));
            }
        }
    }

    /// A unique index takes the `_key` name Postgres gives an inline `UNIQUE` when a plain
    /// index holds its own: `CREATE INDEX IF NOT EXISTS` would skip it silently.
    fn add_index(&mut self, table: &'a str, index: &DeclaredIndex, schema: &MigratedSchema) {
        let name = format!("{table}_{}", index.suffix);
        let mut index = index.clone();
        if index.unique && schema.has_index(&name) {
            index.suffix.push_str("_key");
            self.contracts.push(format!(
                "Contract step: the unique index `{name}_key` serves every lookup the index \
                 `{name}` does, so once it is built:\n\
                 DROP INDEX CONCURRENTLY IF EXISTS {};",
                quoted(&name)
            ));
        }
        self.concurrent_indexes.push((table, index));
    }

    fn add_column(&mut self, table: &'a str, column: &'a DeclaredColumn) {
        self.expand.push(Statement::AddColumn { table, column });
        if !column.allowed.is_empty() {
            self.expand.push(Statement::AddCheck {
                table,
                column,
                added: true,
            });
            self.validations.push(Statement::Validate { table, column });
        }
        if !column.nullable {
            self.contracts.push(match column.backfill {
                Some(_) => drop_default_contract(table, &column.name),
                None => not_null_contract(
                    table,
                    &column.name,
                    "added nullable, as existing rows have no value",
                ),
            });
        }
        self.note_bounds(table, column);
    }

    fn check_existing(
        &mut self,
        table: &'a str,
        column: &'a DeclaredColumn,
        schema: &MigratedSchema,
    ) {
        self.compare_column(table, column, schema);
        if column.allowed.is_empty() {
            return;
        }
        let Some(checks) = schema.check_literals(table, &column.name) else {
            self.expand.push(Statement::AddCheck {
                table,
                column,
                added: false,
            });
            self.validations.push(Statement::Validate { table, column });
            return;
        };
        let lists_all = checks.iter().any(|listed| {
            column
                .allowed
                .iter()
                .all(|name| listed.contains(&name.as_str()))
        });
        if !lists_all {
            self.notes.push(format!(
                "The CHECK on `{table}.{}` does not list every one of {}: replace it with one that \
                 does, by hand.",
                column.name,
                literals(&column.allowed)
            ));
        }
    }

    fn compare_column(&mut self, table: &str, column: &DeclaredColumn, schema: &MigratedSchema) {
        let name = &column.name;
        if schema.has_type(table, name, column.sql_type) == Some(false) {
            self.notes.push(format!(
                "`{table}.{name}` is declared as `{}`, yet migrated as another type: change it by \
                 hand, in steps the running version can read through.",
                column.sql_type
            ));
        }
        match schema.is_nullable(table, name) {
            Some(true) if !column.nullable => {
                self.notes
                    .push(not_null_contract(table, name, "migrated nullable"));
            }
            Some(false) if column.nullable => self.notes.push(format!(
                "`{table}.{name}` is optional, yet migrated NOT NULL, so a record without one \
                 fails to save. Before a version saving one runs:\n\
                 ALTER TABLE {} ALTER COLUMN {} DROP NOT NULL;",
                quoted(table),
                quoted(name)
            )),
            _ => {}
        }
    }

    fn note_bounds(&mut self, table: &str, column: &DeclaredColumn) {
        if column.bounds.is_empty() {
            return;
        }
        let bounds: Vec<String> = column
            .bounds
            .iter()
            .map(|bound| format!("`{bound}`"))
            .collect();
        self.notes.push(format!(
            "`{table}.{}` declares {}: add the CHECK it needs by hand.",
            column.name,
            bounds.join(", ")
        ));
    }

    fn note_unknown(&mut self, table: &str, field: &str, type_name: &str) {
        self.notes.push(format!(
            "`{table}.{field}` is a `{type_name}`, whose columns `sofie gen migration` cannot \
             tell: add them by hand."
        ));
    }

    fn comments(&self) -> String {
        self.notes
            .iter()
            .chain(&self.contracts)
            .map(|paragraph| commented(paragraph) + "\n")
            .collect()
    }

    fn migrations(&self, name: &str) -> Vec<Migration<'_>> {
        let mut migrations = Vec::new();
        // Contract steps alone open no file: one holding only comments could never carry them,
        // once applied. They go first in whichever file comes first.
        if !self.expand.is_empty() {
            migrations.push(Migration {
                name: name.to_owned(),
                statements: self.expand.clone(),
            });
        }
        if !self.validations.is_empty() {
            migrations.push(Migration {
                name: format!("{name}_checks"),
                statements: self.validations.clone(),
            });
        }
        migrations.extend(
            self.concurrent_indexes
                .iter()
                .map(|(table, index)| Migration {
                    name: format!("index_{table}_{}", index.suffix),
                    statements: vec![Statement::CreateIndex {
                        table,
                        index,
                        concurrently: true,
                    }],
                }),
        );
        migrations
    }
}

fn is_present(table: &str, index: &DeclaredIndex, schema: &MigratedSchema) -> bool {
    let columns: Vec<&str> = index.columns.iter().map(String::as_str).collect();
    match (index.unique, columns.as_slice()) {
        (true, _) => schema.is_unique_over(table, &columns),
        (false, [column]) => schema.is_indexed(table, column),
        (false, _) => schema.has_index(&format!("{table}_{}", index.suffix)),
    }
}

/// The version still serving inserts rows without the column, so its default outlives the expand step.
fn drop_default_contract(table: &str, column: &str) -> String {
    format!(
        "Contract step, once no running version inserts without `{table}.{column}`:\n\
         ALTER TABLE {} ALTER COLUMN {} DROP DEFAULT;",
        quoted(table),
        quoted(column)
    )
}

fn not_null_contract(table: &str, column: &str, why: &str) -> String {
    let (table_name, column_name) = (quoted(table), quoted(column));
    let constraint = quoted(&format!("{table}_{column}_not_null"));
    format!(
        "Contract step: `{table}.{column}` is required, yet {why}. Once every row has one:\n\
         ALTER TABLE {table_name} ADD CONSTRAINT {constraint} CHECK ({column_name} IS NOT NULL) NOT VALID;\n\
         then in a later migration:\n\
         ALTER TABLE {table_name} VALIDATE CONSTRAINT {constraint};\n\
         ALTER TABLE {table_name} ALTER COLUMN {column_name} SET NOT NULL;"
    )
}

#[derive(Clone, Copy)]
enum Dialect<'m> {
    Postgres,
    /// SQLite has no schemas, so a module's tables are named `<module>_<table>`.
    Sqlite {
        prefix: &'m str,
    },
}

impl Dialect<'_> {
    fn table(self, table: &str) -> String {
        match self {
            Self::Postgres => table.to_owned(),
            Self::Sqlite { prefix } => format!("{prefix}_{table}"),
        }
    }
}

impl Migration<'_> {
    fn render(&self, dialect: Dialect) -> String {
        let statements: Vec<String> = self
            .statements
            .iter()
            .map(|statement| statement.render(dialect))
            .filter(|text| !text.is_empty())
            .collect();
        if statements.is_empty() && matches!(dialect, Dialect::Sqlite { .. }) {
            return SQLITE_NEEDS_NO_STEP.to_owned();
        }
        statements.concat()
    }
}

impl Statement<'_> {
    fn render(&self, dialect: Dialect) -> String {
        match *self {
            Self::CreateTable(table) => create_table(table, dialect),
            Self::CreateIndex {
                table,
                index,
                concurrently,
            } => create_index(table, index, concurrently, dialect),
            Self::AddColumn { table, column } => add_column(table, column, dialect),
            Self::AddCheck {
                table,
                column,
                added,
            } => add_check(table, column, added, dialect),
            Self::Validate { table, column } => match dialect {
                Dialect::Postgres => format!(
                    "ALTER TABLE {} VALIDATE CONSTRAINT {};\n",
                    quoted(table),
                    quoted(&check_name(table, &column.name))
                ),
                Dialect::Sqlite { .. } => String::new(),
            },
        }
    }
}

fn create_table(table: &DeclaredTable, dialect: Dialect) -> String {
    let id = match dialect {
        Dialect::Postgres => "\"id\" uuid PRIMARY KEY",
        Dialect::Sqlite { .. } => "\"id\" uuid PRIMARY KEY NOT NULL",
    };
    let columns: Vec<String> = std::iter::once(id.to_owned())
        .chain(table.columns.iter().map(|column| {
            let required = if column.nullable { "" } else { " NOT NULL" };
            format!(
                "{} {}{required}{}",
                quoted(&column.name),
                column.sql_type,
                inline_check(column)
            )
        }))
        .collect();
    format!(
        "CREATE TABLE {} (\n    {}\n);\n",
        quoted(&dialect.table(&table.name)),
        columns.join(",\n    ")
    )
}

fn create_index(
    table: &str,
    index: &DeclaredIndex,
    concurrently: bool,
    dialect: Dialect,
) -> String {
    let table = dialect.table(table);
    let unique = if index.unique { "UNIQUE " } else { "" };
    let concurrently = match dialect {
        Dialect::Postgres if concurrently => "CONCURRENTLY IF NOT EXISTS ",
        _ => "",
    };
    let columns: Vec<String> = index.columns.iter().map(|column| quoted(column)).collect();
    let only_where = index
        .only_where
        .map(|column| format!(" WHERE {}", quoted(column)))
        .unwrap_or_default();
    format!(
        "CREATE {unique}INDEX {concurrently}{} ON {} ({}){only_where};\n",
        quoted(&format!("{table}_{}", index.suffix)),
        quoted(&table),
        columns.join(", ")
    )
}

fn add_column(table: &str, column: &DeclaredColumn, dialect: Dialect) -> String {
    let table = quoted(&dialect.table(table));
    let name = quoted(&column.name);
    let added = format!("ALTER TABLE {table} ADD COLUMN {name} {}", column.sql_type);
    match (dialect, column.backfill) {
        (Dialect::Postgres, Some(backfill)) if !column.nullable => {
            format!("{added} NOT NULL DEFAULT {backfill};\n")
        }
        (Dialect::Postgres, _) => format!("{added};\n"),
        (Dialect::Sqlite { .. }, Some(NOW)) if !column.nullable => format!(
            "-- SQLite adds no NOT NULL column without a constant default, so it stays nullable.\n\
             {added};\nUPDATE {table} SET {name} = {SQLITE_NOW};\n"
        ),
        (Dialect::Sqlite { .. }, Some(backfill)) if !column.nullable => format!(
            "{added} NOT NULL DEFAULT {backfill}{};\n",
            inline_check(column)
        ),
        (Dialect::Sqlite { .. }, _) => {
            let nullable = if column.nullable {
                ""
            } else {
                "-- SQLite adds no NOT NULL column without a constant default, so it stays nullable.\n"
            };
            format!("{nullable}{added}{};\n", inline_check(column))
        }
    }
}

const NOW: &str = "now()";

/// As sofie-sqlite stores an instant: microseconds since the epoch.
const SQLITE_NOW: &str = "CAST(unixepoch('subsec') * 1000000 AS INTEGER)";

fn add_check(table: &str, column: &DeclaredColumn, added: bool, dialect: Dialect) -> String {
    match dialect {
        Dialect::Postgres => format!(
            "ALTER TABLE {} ADD CONSTRAINT {} CHECK ({}) NOT VALID;\n",
            quoted(table),
            quoted(&check_name(table, &column.name)),
            allowed_values(column)
        ),
        Dialect::Sqlite { .. } if added => String::new(),
        Dialect::Sqlite { .. } => format!(
            "-- SQLite adds no CHECK to an existing column: copy `{}` into a table whose `{}` \
             has CHECK ({}).\n",
            dialect.table(table),
            column.name,
            allowed_values(column)
        ),
    }
}

/// Postgres's own name for a CHECK on one column, so a later migration names it either way.
fn check_name(table: &str, column: &str) -> String {
    format!("{table}_{column}_check")
}

fn inline_check(column: &DeclaredColumn) -> String {
    if column.allowed.is_empty() {
        return String::new();
    }
    format!(" CHECK ({})", allowed_values(column))
}

fn allowed_values(column: &DeclaredColumn) -> String {
    format!(
        "{} IN ({})",
        quoted(&column.name),
        literals(&column.allowed)
    )
}

fn literals(names: &[String]) -> String {
    let quoted: Vec<String> = names.iter().map(|name| format!("'{name}'")).collect();
    quoted.join(", ")
}

/// Every name quoted: a table named after a type is often a reserved word (`order`, `user`).
fn quoted(name: &str) -> String {
    format!("\"{name}\"")
}

fn commented(paragraph: &str) -> String {
    paragraph
        .lines()
        .map(|line| format!("-- {line}"))
        .collect::<Vec<_>>()
        .join("\n")
}