summaryrefslogtreecommitdiffstats
path: root/rust/src/database_models.rs
blob: 0e138439bc12eddc54179dd97c04f36d51a2d7d2 (plain)
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
use chrono;
//use serde_json;
use database_schema::*;
use uuid::Uuid;
use errors::*;

// Ugh. I thought the whole point was to *not* do this, but:
// https://github.com/diesel-rs/diesel/issues/1589

pub enum EntityState {
    WorkInProgress,
    Active(i64),
    Redirect(Uuid, i64),
    Deleted,
}

impl EntityState {
    pub fn shortname(&self) -> String {
        match self {
            EntityState::WorkInProgress => "wip",
            EntityState::Active(_) => "active",
            EntityState::Redirect(_,_) => "redirect",
            EntityState::Deleted => "deleted",
        }.to_string()
    }
}

pub trait EntityIdentRow {
    fn state(&self) -> Result<EntityState>;
}

// Helper for constructing tables
macro_rules! entity_structs {
    ($edit_table:expr, $edit_struct:ident, $ident_table:expr, $ident_struct:ident) => {
        #[derive(Debug, Queryable, Identifiable, Associations, AsChangeset, QueryableByName)]
        #[table_name = $edit_table]
        pub struct $edit_struct {
            pub id: i64,
            pub ident_id: Uuid,
            pub rev_id: Option<i64>,
            pub redirect_id: Option<Uuid>,
            pub editgroup_id: i64,
            //pub extra_json: Option<String>,
        }

        #[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
        #[table_name = $ident_table]
        pub struct $ident_struct {
            pub id: Uuid,
            pub is_live: bool,
            pub rev_id: Option<i64>,
            pub redirect_id: Option<Uuid>,
        }

        impl EntityIdentRow for $ident_struct {
            fn state(&self) -> Result<EntityState> {
                if !self.is_live {
                    return Ok(EntityState::WorkInProgress);
                }
                match (self.redirect_id, self.rev_id) {
                    (None, None) => Ok(EntityState::Deleted),
                    (Some(redir), Some(rev)) => Ok(EntityState::Redirect(redir, rev)),
                    (None, Some(rev)) => Ok(EntityState::Active(rev)),
                    _ => bail!("Invalid EntityIdentRow state")
                }
            }
        }
    };
}

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "container_rev"]
pub struct ContainerRevRow {
    pub id: i64,
    //pub extra_json: Option<serde_json::Value>,
    pub name: String,
    pub publisher: Option<String>,
    pub issn: Option<String>,
}

entity_structs!(
    "container_edit",
    ContainerEditRow,
    "container_ident",
    ContainerIdentRow
);

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "creator_rev"]
pub struct CreatorRevRow {
    pub id: i64,
    //extra_json: Option<Json>,
    pub name: String,
    pub orcid: Option<String>,
}

entity_structs!(
    "creator_edit",
    CreatorEditRow,
    "creator_ident",
    CreatorIdentRow
);

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "file_rev"]
pub struct FileRevRow {
    pub id: i64,
    //extra_json: Option<Json>,
    pub size: Option<i32>,
    pub sha1: Option<String>,
    pub url: Option<String>,
}

entity_structs!("file_edit", FileEditRow, "file_ident", FileIdentRow);

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "release_rev"]
pub struct ReleaseRevRow {
    pub id: i64,
    //extra_json: Option<Json>,
    pub work_ident_id: Uuid,
    pub container_ident_id: Option<Uuid>,
    pub title: String,
    pub release_type: Option<String>,
    pub date: Option<String>,
    pub doi: Option<String>,
    pub volume: Option<String>,
    pub pages: Option<String>,
    pub issue: Option<String>,
}

entity_structs!(
    "release_edit",
    ReleaseEditRow,
    "release_ident",
    ReleaseIdentRow
);

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "work_rev"]
pub struct WorkRevRow {
    pub id: i64,
    //extra_json: Option<Json>,
    pub work_type: Option<String>,
    pub primary_release_id: Option<Uuid>,
}

entity_structs!("work_edit", WorkEditRow, "work_ident", WorkIdentRow);

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "release_contrib"]
pub struct ReleaseContribRow {
    id: i64,
    release_rev: i64,
    creator_ident_id: Option<Uuid>,
    stub: Option<String>,
    contrib_type: Option<String>,
}

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "release_ref"]
pub struct ReleaseRefRow {
    id: i64,
    release_rev: i64,
    target_release_ident_id: Option<Uuid>,
    index: Option<i32>,
    stub: Option<String>,
}

/*
#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "file_release"]
pub struct FileReleaseRow {
    id: i64,
    file_rev: i64,
    target_release_ident_id: Uuid,
}
*/

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "editgroup"]
pub struct EditgroupRow {
    pub id: i64,
    //extra_json: Option<Json>,
    pub editor_id: i64,
    pub description: Option<String>,
}

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "editor"]
pub struct EditorRow {
    pub id: i64,
    pub username: String,
    pub is_admin: bool,
    pub active_editgroup_id: Option<i64>,
}

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "changelog"]
pub struct ChangelogRow {
    pub id: i64,
    pub editgroup_id: i64,
    pub timestamp: chrono::NaiveDateTime,
}