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.
This commit is contained in:
projectmoon 2020-12-08 21:15:38 +00:00
parent 0c09a3fdef
commit 245bd4f2cc
7 changed files with 52 additions and 5 deletions

13
Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

@ -3,3 +3,5 @@
[print_schema]
file = "src/schema.rs"
import_types = ["diesel::sql_types::*", "crate::models::characters::*"]
patch_file = "src/schema.patch"

View File

@ -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
);

View File

@ -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<u8>,
}
@ -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],
}

18
src/schema.patch Normal file
View File

@ -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! {

View File

@ -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,