core/build.rs: set user_version PRAGMA in generated schema.sql

The generated schema did not include the `user_version` which tracks
which migration is the latest one. This made the README.md example of
creating a database manually fail because it would be initialized with a
`user_version` of 0 and then `mailpot` would attempt to apply migrations
to it.

```shell
$ sqlite3 /path/to/db < ./core/src/schema.sql
$ cargo run --bin mpot -- [some command]
ERROR - 1 no such table: templates in "
ALTER TABLE templates RENAME TO template;"
[1] Error returned from sqlite3 no such table: templates.
```

Fixes #1

https://git.meli.delivery/meli/mailpot/issues/1

Signed-off-by: Manos Pitsidianakis <manos@pitsidianak.is>
web-cookie-secret
Manos Pitsidianakis 2023-10-19 10:27:31 +03:00
parent 0a0aa04677
commit ad63687c22
Signed by: Manos Pitsidianakis
GPG Key ID: 7729C7707F7E09D0
3 changed files with 15 additions and 2 deletions

View File

@ -62,7 +62,7 @@ fn main() {
String::from_utf8_lossy(&output.stderr)
);
}
make_migrations("migrations", MIGRATION_RS, &mut output.stdout);
let user_version: i32 = make_migrations("migrations", MIGRATION_RS, &mut output.stdout);
let mut verify = Command::new(std::env::var("SQLITE_BIN").unwrap_or("sqlite3".into()))
.stdin(Stdio::piped())
.stdout(Stdio::piped())
@ -87,4 +87,9 @@ fn main() {
}
let mut file = std::fs::File::create("./src/schema.sql").unwrap();
file.write_all(&output.stdout).unwrap();
file.write_all(
&format!("\n\n-- Set current schema version.\n\nPRAGMA user_version = {user_version};\n")
.as_bytes(),
)
.unwrap();
}

View File

@ -25,11 +25,13 @@ use std::{fs::read_dir, io::Write, path::Path};
///
/// If a migration is a data migration (not a CREATE, DROP or ALTER statement) it is appended to
/// the schema file.
///
/// Returns the current `user_version` PRAGMA value.
pub fn make_migrations<M: AsRef<Path>, O: AsRef<Path>>(
migrations_path: M,
output_file: O,
schema_file: &mut Vec<u8>,
) {
) -> i32 {
let migrations_folder_path = migrations_path.as_ref();
let output_file_path = output_file.as_ref();
@ -104,4 +106,5 @@ pub fn make_migrations<M: AsRef<Path>, O: AsRef<Path>>(
}
migr_rs.write_all(b"]").unwrap();
migr_rs.flush().unwrap();
paths.len() as i32
}

View File

@ -650,3 +650,8 @@ INSERT OR REPLACE INTO settings_json_schema(id, value) VALUES('MimeRejectSetting
}
}
}');
-- Set current schema version.
PRAGMA user_version = 7;