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
//! Render SQL DDL statements.
//!
//! Conventions:
//!
//! - Use 4 spaces for indentation (see common::SQL_INDENTATION)
//! - SQL types and keywords, like CREATE TABLE and VARCHAR, should be upper
//!   case, for consistency.
//! - SqlRenderer implementations do not add semicolons at the end of
//!   statements, this is done later.

mod common;
mod mssql_renderer;
mod mysql_renderer;
mod postgres_renderer;
mod sqlite_renderer;

pub(crate) use common::IteratorJoin;

use self::common::{Quoted, QuotedWithPrefix};
use crate::{
    migration_pair::MigrationPair,
    sql_migration::{
        AlterEnum, AlterExtension, AlterTable, CreateExtension, DropExtension, RedefineTable, SequenceChanges,
    },
};
use sql_schema_describer::{
    self as sql,
    walkers::{EnumWalker, ForeignKeyWalker, IndexWalker, TableWalker, UserDefinedTypeWalker, ViewWalker},
    SqlSchema,
};

pub(crate) trait SqlRenderer {
    fn quote<'a>(&self, name: &'a str) -> Quoted<&'a str>;

    fn render_add_foreign_key(&self, foreign_key: ForeignKeyWalker<'_>) -> String;

    fn render_alter_enum(&self, alter_enum: &AlterEnum, schemas: MigrationPair<&SqlSchema>) -> Vec<String>;

    fn render_alter_primary_key(&self, _tables: MigrationPair<TableWalker<'_>>) -> Vec<String> {
        unreachable!("unreachable render_alter_primary_key()")
    }

    fn render_alter_sequence(
        &self,
        _idx: MigrationPair<u32>,
        _: SequenceChanges,
        _: MigrationPair<&SqlSchema>,
    ) -> Vec<String> {
        unreachable!("unreachable render_alter_sequence");
    }

    fn render_rename_index(&self, _indexes: MigrationPair<IndexWalker<'_>>) -> Vec<String> {
        unreachable!("unreachable render_alter_index")
    }

    fn render_rename_primary_key(&self, _tables: MigrationPair<TableWalker<'_>>) -> Vec<String> {
        unreachable!("unreachable render_rename_index")
    }

    fn render_alter_table(&self, alter_table: &AlterTable, schemas: MigrationPair<&SqlSchema>) -> Vec<String>;

    /// Render a `CreateEnum` step.
    fn render_create_enum(&self, create_enum: EnumWalker<'_>) -> Vec<String>;

    fn render_create_index(&self, index: IndexWalker<'_>) -> String;

    /// Render a table creation step.
    fn render_create_table(&self, table: TableWalker<'_>) -> String;

    /// Render a table creation with the provided table name.
    fn render_create_table_as(&self, table: TableWalker<'_>, table_name: QuotedWithPrefix<&str>) -> String;

    fn render_drop_and_recreate_index(&self, _indexes: MigrationPair<IndexWalker<'_>>) -> Vec<String> {
        unreachable!("unreachable render_drop_and_recreate_index")
    }

    /// Render a `DropEnum` step.
    fn render_drop_enum(&self, dropped_enum: EnumWalker<'_>) -> Vec<String>;

    /// Render a `DropForeignKey` step.
    fn render_drop_foreign_key(&self, foreign_key: ForeignKeyWalker<'_>) -> String;

    /// Render a `DropIndex` step.
    fn render_drop_index(&self, index: IndexWalker<'_>) -> String;

    /// Render a `DropTable` step.
    fn render_drop_table(&self, namespace: Option<&str>, table_name: &str) -> Vec<String> {
        let name = match namespace {
            Some(namespace) => format!("{}.{}", self.quote(namespace), self.quote(table_name)),
            None => format!("{}", self.quote(table_name)),
        };
        vec![format!("DROP TABLE {name}")]
    }

    /// Render a `RedefineTables` step.
    fn render_redefine_tables(&self, tables: &[RedefineTable], schemas: MigrationPair<&SqlSchema>) -> Vec<String>;

    /// Render a table renaming step.
    fn render_rename_table(&self, namespace: Option<&str>, name: &str, new_name: &str) -> String;

    /// Render a drop view step.
    fn render_drop_view(&self, view: ViewWalker<'_>) -> String;

    /// Render a drop type step.
    fn render_drop_user_defined_type(&self, udt: &UserDefinedTypeWalker<'_>) -> String;

    /// Render a transaction start.
    fn render_begin_transaction(&self) -> Option<&'static str> {
        None
    }

    /// Render a transaction commit.
    fn render_commit_transaction(&self) -> Option<&'static str> {
        None
    }

    /// Render a `RenameForeignKey` step.
    fn render_rename_foreign_key(&self, fks: MigrationPair<ForeignKeyWalker<'_>>) -> String;

    fn render_create_namespace(&self, _namespace: sql::NamespaceWalker<'_>) -> String {
        unreachable!()
    }

    fn render_create_extension(&self, _create: &CreateExtension, _schema: &SqlSchema) -> Vec<String> {
        unreachable!("render_create_extension")
    }

    fn render_alter_extension(&self, _alter: &AlterExtension, _schemas: MigrationPair<&SqlSchema>) -> Vec<String> {
        unreachable!("render_alter_extension")
    }

    fn render_drop_extension(&self, _drop: &DropExtension, _schema: &SqlSchema) -> Vec<String> {
        unreachable!("render_drop_extension")
    }
}