From 44dded7f280f24297260b52f05f330384415ce29 Mon Sep 17 00:00:00 2001 From: jeff Date: Tue, 8 Dec 2020 21:15:38 +0000 Subject: [PATCH] Switch character data type to an enum, including schema patch. Schema patch required because the diesel enum derive crate does not seem to deal with print-schema for sqlite. --- Cargo.lock | 13 +++++++++++++ Cargo.toml | 1 + diesel.toml | 2 ++ migrations/2020-12-02-213704_characters/up.sql | 2 +- src/models/characters.rs | 13 ++++++++++--- src/schema.patch | 18 ++++++++++++++++++ src/schema.rs | 8 +++++++- 7 files changed, 52 insertions(+), 5 deletions(-) create mode 100644 src/schema.patch diff --git a/Cargo.lock b/Cargo.lock index f6167ec..bdac6bc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -342,6 +342,18 @@ dependencies = [ "r2d2", ] +[[package]] +name = "diesel-derive-enum" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "703e71c268ea2d8da9c0ab0b40d8b217179ee622209c170875d24443193a0dfb" +dependencies = [ + "heck", + "proc-macro2 1.0.24", + "quote 1.0.7", + "syn 1.0.53", +] + [[package]] name = "diesel_derives" version = "1.4.1" @@ -1436,6 +1448,7 @@ name = "tenebrous-sheets" version = "0.1.0" dependencies = [ "diesel", + "diesel-derive-enum", "log 0.4.11", "prost", "prost-build", diff --git a/Cargo.toml b/Cargo.toml index 540749d..999d3ff 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,6 +14,7 @@ serde = "1.0" serde_derive = "1.0" serde_json = "1.0" diesel = "1.4" +diesel-derive-enum = { version = "1", features = ["sqlite"] } thiserror = "1.0" rust-argon2 = "0.8" log = "0.4" diff --git a/diesel.toml b/diesel.toml index 92267c8..b0a4667 100644 --- a/diesel.toml +++ b/diesel.toml @@ -3,3 +3,5 @@ [print_schema] file = "src/schema.rs" +import_types = ["diesel::sql_types::*", "crate::models::characters::*"] +patch_file = "src/schema.patch" \ No newline at end of file diff --git a/migrations/2020-12-02-213704_characters/up.sql b/migrations/2020-12-02-213704_characters/up.sql index 146e333..383481c 100644 --- a/migrations/2020-12-02-213704_characters/up.sql +++ b/migrations/2020-12-02-213704_characters/up.sql @@ -3,7 +3,7 @@ CREATE TABLE characters( user_id INTEGER NOT NULL, viewable BOOLEAN NOT NULL, character_name TEXT NOT NULL, - data_type TEXT NOT NULL, + data_type TEXT CHECK(data_type IN ('chronicles_of_darkness_v1', 'changeling_v1')) NOT NULL, data_version INTEGER NOT NULL, data BLOB NOT NULL ); diff --git a/src/models/characters.rs b/src/models/characters.rs index 315b906..89285fc 100644 --- a/src/models/characters.rs +++ b/src/models/characters.rs @@ -1,5 +1,6 @@ use crate::models::users::User; use crate::schema::characters; +use diesel_derive_enum::DbEnum; use serde_derive::Serialize; /// Control system visibility of a character for a particular user. @@ -29,6 +30,12 @@ pub(crate) trait Visibility { } } +#[derive(DbEnum, Debug, Serialize, PartialEq)] +pub enum CharacterDataType { + ChroniclesOfDarknessV1, + ChangelingV1, +} + /// An entry that appears in a user's character list. Properties are /// in order of table columns. #[derive(Serialize, Debug, Queryable)] @@ -37,7 +44,7 @@ pub struct Character { pub user_id: i32, pub viewable: bool, pub character_name: String, - pub data_type: String, + pub data_type: CharacterDataType, pub data_version: i32, pub data: Vec, } @@ -58,7 +65,7 @@ pub struct StrippedCharacter { pub user_id: i32, pub viewable: bool, pub character_name: String, - pub data_type: String, + pub data_type: CharacterDataType, pub data_version: i32, } @@ -80,7 +87,7 @@ pub struct NewCharacter<'a> { pub user_id: i32, pub viewable: bool, pub character_name: &'a str, - pub data_type: &'a str, + pub data_type: CharacterDataType, pub data_version: i32, pub data: &'a [u8], } diff --git a/src/schema.patch b/src/schema.patch new file mode 100644 index 0000000..3b3cd75 --- /dev/null +++ b/src/schema.patch @@ -0,0 +1,18 @@ +This patch converts the generated schema to an enum type. +--- a/src/schema.rs ++++ b/src/schema.rs +@@ -4,13 +4,13 @@ table! { + + characters (id) { + id -> Integer, + user_id -> Integer, + viewable -> Bool, + character_name -> Text, +- data_type -> Text, ++ data_type -> CharacterDataTypeMapping, + data_version -> Integer, + data -> Binary, + } + } + + table! { diff --git a/src/schema.rs b/src/schema.rs index bc4efd0..b9a6d68 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -1,16 +1,22 @@ table! { + use diesel::sql_types::*; + use crate::models::characters::*; + characters (id) { id -> Integer, user_id -> Integer, viewable -> Bool, character_name -> Text, - data_type -> Text, + data_type -> CharacterDataTypeMapping, data_version -> Integer, data -> Binary, } } table! { + use diesel::sql_types::*; + use crate::models::characters::*; + users (id) { id -> Integer, username -> Text,