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
#[cfg(feature = "sqlite-default")]
mod sqlite;

use diesel::prelude::*;
#[cfg(feature = "sqlite-default")]
use sqlite::MIGRATION_TABLE;

#[derive(Debug, thiserror::Error)]
pub enum MigrationError {
    #[error("Can not create migration table: {0}")]
    CanNotCreateMigrationTable(diesel::result::Error),
    #[error("Can not find latest applied migration number: {0}")]
    CanNotFindLatestAppliedMigrationNumber(diesel::result::Error),
    #[error("Invalid migration: {0}")]
    InvalidMigration(#[from] InvalidMigrationError),
    #[error("apply migration error: {0}")]
    ApplyMigration(#[from] ApplyMigrationError),
}

pub type FnMigration = (
    i32,
    &'static str,
    fn(&mut ft_sdk::Connection) -> Result<(), diesel::result::Error>,
);

pub fn migrate(
    conn: &mut ft_sdk::Connection,
    app_name: &str,
    migration_sqls: include_dir::Dir,
    migration_functions: Vec<FnMigration>,
) -> Result<(), MigrationError> {
    let now = ft_sdk::env::now();
    // check if the migration table exists, if not create it
    create_migration_table(conn).map_err(MigrationError::CanNotCreateMigrationTable)?;

    // Migration for fastn app
    migrate_fastn(conn, now)?;

    // find the latest applied migration number from the table
    migrate_app(conn, app_name, migration_sqls, migration_functions, now)?;

    Ok(())
}

// Migration for fastn app
fn migrate_fastn(
    conn: &mut ft_sdk::Connection,
    now: chrono::DateTime<chrono::Utc>,
) -> Result<(), MigrationError> {
    migrate_app(
        conn,
        "fastn",
        include_dir::include_dir!("$CARGO_MANIFEST_DIR/migrations"),
        vec![],
        now,
    )
}

fn migrate_app(
    conn: &mut ft_sdk::Connection,
    app_name: &str,
    migration_sqls: include_dir::Dir,
    migration_functions: Vec<FnMigration>,
    now: chrono::DateTime<chrono::Utc>,
) -> Result<(), MigrationError> {
    let latest_applied_migration_number = find_latest_applied_migration_number(conn, app_name)
        .map_err(MigrationError::CanNotFindLatestAppliedMigrationNumber)?;

    let migrations = sort_migrations(
        migration_sqls,
        migration_functions,
        latest_applied_migration_number,
    )?;

    for cmd in migrations {
        conn.transaction::<_, ApplyMigrationError, _>(|conn| match cmd {
            Cmd::Sql { id, name, sql } => {
                apply_sql_migration(conn, app_name, id, name.as_str(), sql.as_str(), &now)
            }
            Cmd::Fn { id, name, r#fn } => apply_fn_migration(conn, app_name, id, name, r#fn, &now),
        })?;
    }

    Ok(())
}
#[derive(Debug, thiserror::Error)]
pub enum ApplyMigrationError {
    #[error("failed to apply migration: {0}")]
    ApplyMigration(diesel::result::Error),
    #[error("failed to apply migration: {0}")]
    RecordMigration(diesel::result::Error),
    #[error("failed to commit transaction: {0}")]
    CommitTransaction(#[from] diesel::result::Error),
}

fn apply_sql_migration(
    conn: &mut ft_sdk::Connection,
    app_name: &str,
    id: i32,
    name: &str,
    sql: &str,
    now: &chrono::DateTime<chrono::Utc>,
) -> Result<(), ApplyMigrationError> {
    diesel::connection::SimpleConnection::batch_execute(conn, sql)
        .map_err(ApplyMigrationError::ApplyMigration)?;
    mark_migration_applied(conn, app_name, id, name, now)
        .map_err(ApplyMigrationError::RecordMigration)
}

fn apply_fn_migration(
    conn: &mut ft_sdk::Connection,
    app_name: &str,
    id: i32,
    name: &str,
    r#fn: fn(&mut ft_sdk::Connection) -> Result<(), diesel::result::Error>,
    now: &chrono::DateTime<chrono::Utc>,
) -> Result<(), ApplyMigrationError> {
    r#fn(conn).map_err(ApplyMigrationError::ApplyMigration)?;
    mark_migration_applied(conn, app_name, id, name, now)
        .map_err(ApplyMigrationError::RecordMigration)
}

table! {
    fastn_migration {
        id -> Integer,
        app_name -> Text,
        migration_number -> Integer,
        migration_name -> Text,
        applied_on -> Timestamptz,
    }
}

pub fn mark_migration_applied(
    conn: &mut ft_sdk::Connection,
    app_name: &str,
    id: i32,
    name: &str,
    now: &chrono::DateTime<chrono::Utc>,
) -> Result<(), diesel::result::Error> {
    diesel::insert_into(fastn_migration::table)
        .values((
            fastn_migration::app_name.eq(app_name),
            fastn_migration::migration_number.eq(id),
            fastn_migration::migration_name.eq(name),
            fastn_migration::applied_on.eq(now),
            // fastn_migration::time_taken.eq(time_take),
        ))
        .execute(conn)
        .map(|_| ())
}

fn create_migration_table(conn: &mut ft_sdk::Connection) -> Result<(), diesel::result::Error> {
    diesel::dsl::sql_query(MIGRATION_TABLE).execute(conn)?;
    Ok(())
}

fn find_latest_applied_migration_number(
    conn: &mut ft_sdk::Connection,
    app_name: &str,
) -> Result<Option<i32>, diesel::result::Error> {
    fastn_migration::table
        .select(fastn_migration::migration_number)
        .filter(fastn_migration::app_name.eq(app_name))
        .order(fastn_migration::migration_number.desc())
        .first(conn)
        .optional()
}

#[derive(Debug, thiserror::Error)]
pub enum InvalidMigrationError {
    /// If the same migration exists in both sql and function migrations
    #[error("Duplicate migration number: {0}")]
    DuplicateMigration(i32),
    #[error("Invalid sql file not utf8: {0:?}")]
    InvalidSqlFileNameNotUtf8(std::ffi::OsString),
    #[error("Invalid sql content not utf8: {0}, {1:?}")]
    InvalidSqlFileContentNotUtf8(i32, std::string::FromUtf8Error),
    #[error("SQL file does not start with integer: {0:?}, {1:?}")]
    SqlFileIsNotInteger(String, std::num::ParseIntError),
}

enum Cmd {
    Sql {
        id: i32,
        name: String,
        sql: String,
    },
    Fn {
        id: i32,
        name: &'static str,
        r#fn: fn(&mut ft_sdk::Connection) -> Result<(), diesel::result::Error>,
    },
}

impl Cmd {
    fn id(&self) -> i32 {
        match self {
            Cmd::Sql { id, .. } => *id,
            Cmd::Fn { id, .. } => *id,
        }
    }
}

fn sort_migrations(
    migration_sqls: include_dir::Dir,
    migration_functions: Vec<FnMigration>,
    after: Option<i32>,
) -> Result<Vec<Cmd>, InvalidMigrationError> {
    let mut cmds = vec![];

    for file in migration_sqls.files() {
        if file.path().extension() != Some(std::ffi::OsStr::new("sql")) {
            continue;
        }

        let file_stem = file.path().file_stem().unwrap();
        let file_stem = match file_stem.to_str() {
            Some(v) => v,
            None => {
                return Err(InvalidMigrationError::InvalidSqlFileNameNotUtf8(
                    file_stem.into(),
                ));
            }
        };

        let (migration_number, migration_name) = parse_migration_name(file_stem)?;

        if Some(migration_number) > after {
            cmds.push(Cmd::Sql {
                id: migration_number,
                name: migration_name.to_string(),
                sql: match String::from_utf8(file.contents().to_vec()) {
                    Ok(v) => v,
                    Err(e) => {
                        return Err(InvalidMigrationError::InvalidSqlFileContentNotUtf8(
                            migration_number,
                            e,
                        ));
                    }
                },
            })
        }
    }

    for (migration_number, name, the_fn) in migration_functions.into_iter() {
        if Some(migration_number) > after {
            cmds.push(Cmd::Fn {
                id: migration_number,
                name,
                r#fn: the_fn,
            })
        }
    }

    cmds.sort_by_key(|k| k.id());

    Ok(cmds)
}

fn parse_migration_name(name: &str) -> Result<(i32, &str), InvalidMigrationError> {
    let (number, name) = match name.split_once('-') {
        Some(v) => v,
        None => (name, name),
    };

    Ok((
        number
            .parse()
            .map_err(|e| InvalidMigrationError::SqlFileIsNotInteger(name.to_string(), e))?,
        name,
    ))
}