Author: Manos Pitsidianakis [manos@pitsidianak.is]
Hash: ad63687c22c919b447e01c31c414ee558f0d48c0
Timestamp: Thu, 19 Oct 2023 07:27:31 +0000 (11 months ago)

+15 -2 +/-3 browse
core/build.rs: set user_version PRAGMA in generated schema.sql
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>
1diff --git a/core/build.rs b/core/build.rs
2index 99aff85..44e41d2 100644
3--- a/core/build.rs
4+++ b/core/build.rs
5 @@ -62,7 +62,7 @@ fn main() {
6 String::from_utf8_lossy(&output.stderr)
7 );
8 }
9- make_migrations("migrations", MIGRATION_RS, &mut output.stdout);
10+ let user_version: i32 = make_migrations("migrations", MIGRATION_RS, &mut output.stdout);
11 let mut verify = Command::new(std::env::var("SQLITE_BIN").unwrap_or("sqlite3".into()))
12 .stdin(Stdio::piped())
13 .stdout(Stdio::piped())
14 @@ -87,4 +87,9 @@ fn main() {
15 }
16 let mut file = std::fs::File::create("./src/schema.sql").unwrap();
17 file.write_all(&output.stdout).unwrap();
18+ file.write_all(
19+ &format!("\n\n-- Set current schema version.\n\nPRAGMA user_version = {user_version};\n")
20+ .as_bytes(),
21+ )
22+ .unwrap();
23 }
24 diff --git a/core/make_migrations.rs b/core/make_migrations.rs
25index 4b4acbe..91f3f2e 100644
26--- a/core/make_migrations.rs
27+++ b/core/make_migrations.rs
28 @@ -25,11 +25,13 @@ use std::{fs::read_dir, io::Write, path::Path};
29 ///
30 /// If a migration is a data migration (not a CREATE, DROP or ALTER statement) it is appended to
31 /// the schema file.
32+ ///
33+ /// Returns the current `user_version` PRAGMA value.
34 pub fn make_migrations<M: AsRef<Path>, O: AsRef<Path>>(
35 migrations_path: M,
36 output_file: O,
37 schema_file: &mut Vec<u8>,
38- ) {
39+ ) -> i32 {
40 let migrations_folder_path = migrations_path.as_ref();
41 let output_file_path = output_file.as_ref();
42
43 @@ -104,4 +106,5 @@ pub fn make_migrations<M: AsRef<Path>, O: AsRef<Path>>(
44 }
45 migr_rs.write_all(b"]").unwrap();
46 migr_rs.flush().unwrap();
47+ paths.len() as i32
48 }
49 diff --git a/core/src/schema.sql b/core/src/schema.sql
50index ba839cb..52e6d34 100644
51--- a/core/src/schema.sql
52+++ b/core/src/schema.sql
53 @@ -650,3 +650,8 @@ INSERT OR REPLACE INTO settings_json_schema(id, value) VALUES('MimeRejectSetting
54 }
55 }
56 }');
57+
58+
59+ -- Set current schema version.
60+
61+ PRAGMA user_version = 7;