aboutsummaryrefslogtreecommitdiffstats
path: root/rust/src/database_models.rs
blob: 0427f9c898944dafa05308304e361e17c89d7615 (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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
#![allow(proc_macro_derive_resolution_fallback)]

use crate::database_schema::*;
use crate::errors::*;
use crate::identifiers::uuid2fcid;
use chrono::Utc;
use fatcat_openapi::models::{
    ChangelogEntry, Editgroup, EditgroupAnnotation, Editor, EntityEdit, ReleaseRef,
};
use serde_json;
use uuid::Uuid;

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

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum EntityState {
    WorkInProgress,
    Active(Uuid),
    Redirect(Uuid, Option<Uuid>),
    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>;
}

pub trait EntityEditRow {
    fn into_model(self) -> Result<EntityEdit>;
}

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

        #[derive(Debug, Associations, AsChangeset, QueryableByName, Insertable)]
        #[table_name = $edit_table]
        pub struct $edit_new_struct {
            pub editgroup_id: Uuid,
            pub ident_id: Uuid,
            pub rev_id: Option<Uuid>,
            pub redirect_id: Option<Uuid>,
            pub prev_rev: Option<Uuid>,
            pub extra_json: Option<serde_json::Value>,
        }

        impl EntityEditRow for $edit_struct {
            /// Go from a row (SQL model) to an API model
            fn into_model(self) -> Result<EntityEdit> {
                Ok(EntityEdit {
                    editgroup_id: uuid2fcid(&self.editgroup_id),
                    revision: self.rev_id.map(|v| v.to_string()),
                    redirect_ident: self.redirect_id.map(|v| uuid2fcid(&v)),
                    prev_revision: self.prev_rev.map(|v| v.to_string()),
                    ident: uuid2fcid(&self.ident_id),
                    edit_id: self.id.to_string(),
                    extra: self.extra_json,
                })
            }
        }

        #[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<Uuid>,
            pub redirect_id: Option<Uuid>,
        }

        #[derive(Debug, Associations, AsChangeset, Insertable)]
        #[table_name = $ident_table]
        pub struct $ident_new_struct {
            pub is_live: bool,
            pub rev_id: Option<Uuid>,
            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), 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: Uuid,
    pub extra_json: Option<serde_json::Value>,
    pub name: String,
    pub container_type: Option<String>,
    pub publisher: Option<String>,
    pub issnl: Option<String>,
    pub wikidata_qid: Option<String>,
    pub issne: Option<String>,
    pub issnp: Option<String>,
    pub publication_status: Option<String>,
}

#[derive(Debug, Associations, AsChangeset, Insertable)]
#[table_name = "container_rev"]
pub struct ContainerRevNewRow {
    pub extra_json: Option<serde_json::Value>,
    pub name: String,
    pub container_type: Option<String>,
    pub publisher: Option<String>,
    pub issnl: Option<String>,
    pub wikidata_qid: Option<String>,
    pub issne: Option<String>,
    pub issnp: Option<String>,
    pub publication_status: Option<String>,
}

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

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "creator_rev"]
pub struct CreatorRevRow {
    pub id: Uuid,
    pub extra_json: Option<serde_json::Value>,
    pub display_name: String,
    pub given_name: Option<String>,
    pub surname: Option<String>,
    pub orcid: Option<String>,
    pub wikidata_qid: Option<String>,
}

#[derive(Debug, Associations, AsChangeset, Insertable)]
#[table_name = "creator_rev"]
pub struct CreatorRevNewRow {
    pub extra_json: Option<serde_json::Value>,
    pub display_name: String,
    pub given_name: Option<String>,
    pub surname: Option<String>,
    pub orcid: Option<String>,
    pub wikidata_qid: Option<String>,
}

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

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "file_rev_url"]
pub struct FileRevUrlRow {
    pub id: i64,
    pub file_rev: Uuid,
    pub rel: String,
    pub url: String,
}

#[derive(Debug, Queryable, Associations, AsChangeset, Insertable)]
#[table_name = "file_rev_url"]
pub struct FileRevUrlNewRow {
    pub file_rev: Uuid,
    pub rel: String,
    pub url: String,
}

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "file_rev"]
pub struct FileRevRow {
    pub id: Uuid,
    pub extra_json: Option<serde_json::Value>,
    pub size_bytes: Option<i64>,
    pub sha1: Option<String>,
    pub sha256: Option<String>,
    pub md5: Option<String>,
    pub mimetype: Option<String>,
    pub content_scope: Option<String>,
}

#[derive(Debug, Associations, AsChangeset, Insertable)]
#[table_name = "file_rev"]
pub struct FileRevNewRow {
    pub extra_json: Option<serde_json::Value>,
    pub size_bytes: Option<i64>,
    pub sha1: Option<String>,
    pub sha256: Option<String>,
    pub md5: Option<String>,
    pub mimetype: Option<String>,
    pub content_scope: Option<String>,
}

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

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "fileset_rev_file"]
pub struct FilesetRevFileRow {
    pub id: i64,
    pub fileset_rev: Uuid,
    pub path_name: String,
    pub size_bytes: i64,
    pub md5: Option<String>,
    pub sha1: Option<String>,
    pub sha256: Option<String>,
    pub extra_json: Option<serde_json::Value>,
    pub mimetype: Option<String>,
}

#[derive(Debug, Queryable, Associations, AsChangeset, Insertable)]
#[table_name = "fileset_rev_file"]
pub struct FilesetRevFileNewRow {
    pub fileset_rev: Uuid,
    pub path_name: String,
    pub size_bytes: i64,
    pub md5: Option<String>,
    pub sha1: Option<String>,
    pub sha256: Option<String>,
    pub extra_json: Option<serde_json::Value>,
    pub mimetype: Option<String>,
}

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "fileset_rev_url"]
pub struct FilesetRevUrlRow {
    pub id: i64,
    pub fileset_rev: Uuid,
    pub rel: String,
    pub url: String,
}

#[derive(Debug, Queryable, Associations, AsChangeset, Insertable)]
#[table_name = "fileset_rev_url"]
pub struct FilesetRevUrlNewRow {
    pub fileset_rev: Uuid,
    pub rel: String,
    pub url: String,
}

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "fileset_rev"]
pub struct FilesetRevRow {
    pub id: Uuid,
    pub extra_json: Option<serde_json::Value>,
    pub content_scope: Option<String>,
}

#[derive(Debug, Associations, AsChangeset, Insertable)]
#[table_name = "fileset_rev"]
pub struct FilesetRevNewRow {
    pub extra_json: Option<serde_json::Value>,
    pub content_scope: Option<String>,
}

entity_structs!(
    "fileset_edit",
    FilesetEditRow,
    FilesetEditNewRow,
    "fileset_ident",
    FilesetIdentRow,
    FilesetIdentNewRow
);
#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "webcapture_rev_cdx"]
pub struct WebcaptureRevCdxRow {
    pub id: i64,
    pub webcapture_rev: Uuid,
    pub surt: String,
    pub timestamp: chrono::DateTime<Utc>,
    pub url: String,
    pub mimetype: Option<String>,
    pub status_code: Option<i64>,
    pub sha1: String,
    pub sha256: Option<String>,
    pub size_bytes: Option<i64>,
}

#[derive(Debug, Queryable, Associations, AsChangeset, Insertable)]
#[table_name = "webcapture_rev_cdx"]
pub struct WebcaptureRevCdxNewRow {
    pub webcapture_rev: Uuid,
    pub surt: String,
    pub timestamp: chrono::DateTime<Utc>,
    pub url: String,
    pub mimetype: Option<String>,
    pub status_code: Option<i64>,
    pub sha1: String,
    pub sha256: Option<String>,
    pub size_bytes: Option<i64>,
}

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "webcapture_rev_url"]
pub struct WebcaptureRevUrlRow {
    pub id: i64,
    pub webcapture_rev: Uuid,
    pub rel: String,
    pub url: String,
}

#[derive(Debug, Queryable, Associations, AsChangeset, Insertable)]
#[table_name = "webcapture_rev_url"]
pub struct WebcaptureRevUrlNewRow {
    pub webcapture_rev: Uuid,
    pub rel: String,
    pub url: String,
}

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "webcapture_rev"]
pub struct WebcaptureRevRow {
    pub id: Uuid,
    pub extra_json: Option<serde_json::Value>,
    pub original_url: String,
    pub timestamp: chrono::NaiveDateTime,
    pub content_scope: Option<String>,
}

#[derive(Debug, Associations, AsChangeset, Insertable)]
#[table_name = "webcapture_rev"]
pub struct WebcaptureRevNewRow {
    pub extra_json: Option<serde_json::Value>,
    pub original_url: String,
    pub timestamp: chrono::NaiveDateTime,
    pub content_scope: Option<String>,
}

entity_structs!(
    "webcapture_edit",
    WebcaptureEditRow,
    WebcaptureEditNewRow,
    "webcapture_ident",
    WebcaptureIdentRow,
    WebcaptureIdentNewRow
);

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "release_rev"]
pub struct ReleaseRevRow {
    pub id: Uuid,
    pub extra_json: Option<serde_json::Value>,
    pub work_ident_id: Uuid,
    pub container_ident_id: Option<Uuid>,
    pub refs_blob_sha1: Option<String>,
    pub title: String,
    pub original_title: Option<String>,
    pub release_type: Option<String>,
    pub release_stage: Option<String>,
    pub release_date: Option<chrono::NaiveDate>,
    pub release_year: Option<i64>,
    pub doi: Option<String>,
    pub pmid: Option<String>,
    pub pmcid: Option<String>,
    pub wikidata_qid: Option<String>,
    pub core_id: Option<String>,
    pub volume: Option<String>,
    pub issue: Option<String>,
    pub pages: Option<String>,
    pub publisher: Option<String>,
    pub language: Option<String>,
    pub license_slug: Option<String>,
    pub number: Option<String>,
    pub version: Option<String>,
    pub subtitle: Option<String>,
    pub withdrawn_status: Option<String>,
    pub withdrawn_date: Option<chrono::NaiveDate>,
    pub withdrawn_year: Option<i64>,
}

#[derive(Debug, Associations, AsChangeset, Insertable)]
#[table_name = "release_rev"]
pub struct ReleaseRevNewRow {
    pub extra_json: Option<serde_json::Value>,
    pub work_ident_id: Uuid,
    pub container_ident_id: Option<Uuid>,
    pub refs_blob_sha1: Option<String>,
    pub title: String,
    pub original_title: Option<String>,
    pub release_type: Option<String>,
    pub release_stage: Option<String>,
    pub release_date: Option<chrono::NaiveDate>,
    pub release_year: Option<i64>,
    pub doi: Option<String>,
    pub pmid: Option<String>,
    pub pmcid: Option<String>,
    pub wikidata_qid: Option<String>,
    pub core_id: Option<String>,
    pub volume: Option<String>,
    pub issue: Option<String>,
    pub pages: Option<String>,
    pub publisher: Option<String>,
    pub language: Option<String>,
    pub license_slug: Option<String>,
    pub number: Option<String>,
    pub version: Option<String>,
    pub subtitle: Option<String>,
    pub withdrawn_status: Option<String>,
    pub withdrawn_date: Option<chrono::NaiveDate>,
    pub withdrawn_year: Option<i64>,
}

#[derive(Debug, Queryable, Associations, AsChangeset, Insertable)]
#[table_name = "release_rev_extid"]
pub struct ReleaseExtidRow {
    pub release_rev: Uuid,
    pub extid_type: String,
    pub value: String,
}

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

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "work_rev"]
pub struct WorkRevRow {
    pub id: Uuid,
    pub extra_json: Option<serde_json::Value>,
}

#[derive(Debug, Associations, AsChangeset, Insertable)]
#[table_name = "work_rev"]
pub struct WorkRevNewRow {
    pub extra_json: Option<serde_json::Value>,
}

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

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "release_rev_abstract"]
pub struct ReleaseRevAbstractRow {
    pub id: i64,
    pub release_rev: Uuid,
    pub abstract_sha1: String,
    pub mimetype: Option<String>,
    pub lang: Option<String>,
}

#[derive(Debug, Queryable, Associations, AsChangeset, Insertable)]
#[table_name = "release_rev_abstract"]
pub struct ReleaseRevAbstractNewRow {
    pub release_rev: Uuid,
    pub abstract_sha1: String,
    pub mimetype: Option<String>,
    pub lang: Option<String>,
}

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "release_contrib"]
pub struct ReleaseContribRow {
    pub id: i64,
    pub release_rev: Uuid,
    pub creator_ident_id: Option<Uuid>,
    pub raw_name: Option<String>,
    pub role: Option<String>,
    pub raw_affiliation: Option<String>,
    pub index_val: Option<i32>,
    pub extra_json: Option<serde_json::Value>,
    pub given_name: Option<String>,
    pub surname: Option<String>,
}

#[derive(Debug, Insertable)]
#[table_name = "release_contrib"]
pub struct ReleaseContribNewRow {
    pub release_rev: Uuid,
    pub creator_ident_id: Option<Uuid>,
    pub raw_name: Option<String>,
    pub role: Option<String>,
    pub raw_affiliation: Option<String>,
    pub index_val: Option<i32>,
    pub extra_json: Option<serde_json::Value>,
    pub given_name: Option<String>,
    pub surname: Option<String>,
}

#[derive(Debug, Queryable, Insertable, Associations, AsChangeset)]
#[table_name = "release_ref"]
pub struct ReleaseRefRow {
    pub release_rev: Uuid,
    pub index_val: i32,
    pub target_release_ident_id: Uuid,
}

#[derive(Debug, Queryable, Insertable, Associations, AsChangeset)]
#[table_name = "refs_blob"]
pub struct RefsBlobRow {
    pub sha1: String,
    pub refs_json: serde_json::Value,
}

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
/// This model is a stable representation of what goes in a RefsBlobRow `refs_json` field (an array
/// of this model). We could rely on the `ReleaseRef` API spec model directly, but that would lock
/// the database contents to the API spec rigidly; by defining this struct independently, we can
/// migrate the schemas. To start, this is a direct copy of the `ReleaseRef` model.
pub struct RefsBlobJson {
    #[serde(rename = "index")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub index: Option<i64>,

    /// base32-encoded unique identifier
    #[serde(rename = "target_release_id")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub target_release_id: Option<String>,

    #[serde(rename = "extra")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub extra: Option<serde_json::Value>,

    #[serde(rename = "key")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub key: Option<String>,

    #[serde(rename = "year")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub year: Option<i64>,

    #[serde(rename = "container_name")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub container_name: Option<String>,

    #[serde(rename = "title")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub title: Option<String>,

    #[serde(rename = "locator")]
    #[serde(skip_serializing_if = "Option::is_none")]
    pub locator: Option<String>,
}

impl RefsBlobJson {
    pub fn into_model(self) -> ReleaseRef {
        ReleaseRef {
            index: self.index,
            target_release_id: self.target_release_id,
            extra: self.extra,
            key: self.key,
            year: self.year,
            container_name: self.container_name,
            title: self.title,
            locator: self.locator,
        }
    }

    pub fn to_model(&self) -> ReleaseRef {
        ReleaseRef {
            index: self.index,
            target_release_id: self.target_release_id.clone(),
            extra: self.extra.clone(),
            key: self.key.clone(),
            year: self.year,
            container_name: self.container_name.clone(),
            title: self.title.clone(),
            locator: self.locator.clone(),
        }
    }

    pub fn from_model(model: &ReleaseRef) -> RefsBlobJson {
        RefsBlobJson {
            index: model.index,
            target_release_id: model.target_release_id.clone(),
            extra: model.extra.clone(),
            key: model.key.clone(),
            year: model.year,
            container_name: model.container_name.clone(),
            title: model.title.clone(),
            locator: model.locator.clone(),
        }
    }
}

#[derive(Debug, Queryable, Insertable, Associations, AsChangeset)]
#[table_name = "file_rev_release"]
pub struct FileRevReleaseRow {
    pub file_rev: Uuid,
    pub target_release_ident_id: Uuid,
}

#[derive(Debug, Queryable, Insertable, Associations, AsChangeset)]
#[table_name = "fileset_rev_release"]
pub struct FilesetRevReleaseRow {
    pub fileset_rev: Uuid,
    pub target_release_ident_id: Uuid,
}

#[derive(Debug, Queryable, Insertable, Associations, AsChangeset)]
#[table_name = "webcapture_rev_release"]
pub struct WebcaptureRevReleaseRow {
    pub webcapture_rev: Uuid,
    pub target_release_ident_id: Uuid,
}

#[derive(Debug, Queryable, Insertable, Associations, AsChangeset)]
#[table_name = "abstracts"]
pub struct AbstractsRow {
    pub sha1: String,
    pub content: String,
}

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "editgroup"]
pub struct EditgroupRow {
    pub id: Uuid,
    pub editor_id: Uuid,
    pub created: chrono::NaiveDateTime,
    pub submitted: Option<chrono::NaiveDateTime>,
    pub is_accepted: bool,
    pub description: Option<String>,
    pub extra_json: Option<serde_json::Value>,
}

impl EditgroupRow {
    /// Returns an Editgroup API model *without* the entity edits actually populated. Useful for,
    /// eg, entity history queries (where we already have the entity edit we want)
    pub fn into_model_partial(
        self,
        changelog_index: Option<i64>,
        editor: Option<Editor>,
    ) -> Editgroup {
        Editgroup {
            editgroup_id: Some(uuid2fcid(&self.id)),
            editor_id: Some(uuid2fcid(&self.editor_id)),
            editor: editor,
            changelog_index: changelog_index,
            created: Some(chrono::DateTime::from_utc(self.created, chrono::Utc)),
            submitted: self
                .submitted
                .map(|v| chrono::DateTime::from_utc(v, chrono::Utc)),
            description: self.description,
            extra: self.extra_json,
            annotations: None,
            edits: None,
        }
    }
}

#[derive(Debug, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "editgroup_annotation"]
pub struct EditgroupAnnotationRow {
    pub id: Uuid,
    pub editgroup_id: Uuid,
    pub editor_id: Uuid,
    pub created: chrono::NaiveDateTime,
    pub comment_markdown: Option<String>,
    pub extra_json: Option<serde_json::Value>,
}

impl EditgroupAnnotationRow {
    pub fn into_model(self) -> EditgroupAnnotation {
        EditgroupAnnotation {
            annotation_id: Some(self.id.to_string()),
            editgroup_id: Some(uuid2fcid(&self.editgroup_id)),
            editor_id: Some(uuid2fcid(&self.editor_id)),
            editor: None,
            created: Some(chrono::DateTime::from_utc(self.created, chrono::Utc)),
            comment_markdown: self.comment_markdown,
            extra: self.extra_json,
        }
    }
}

#[derive(Debug, Clone, Queryable, Identifiable, Associations, AsChangeset)]
#[table_name = "editor"]
pub struct EditorRow {
    pub id: Uuid,
    pub username: String,
    pub is_superuser: bool,
    pub is_admin: bool,
    pub is_bot: bool,
    pub is_active: bool,
    pub registered: chrono::NaiveDateTime,
    pub auth_epoch: chrono::NaiveDateTime,
    pub wrangler_id: Option<Uuid>,
}

impl EditorRow {
    pub fn into_model(self) -> Editor {
        Editor {
            editor_id: Some(uuid2fcid(&self.id)),
            username: self.username,
            is_admin: Some(self.is_admin),
            is_bot: Some(self.is_bot),
            is_active: Some(self.is_active),
        }
    }
}

#[derive(Debug, Clone, Queryable, Associations, AsChangeset)]
#[table_name = "auth_oidc"]
pub struct AuthOidcRow {
    pub id: i64,
    pub created: chrono::NaiveDateTime,
    pub editor_id: Uuid,
    pub provider: String,
    pub oidc_iss: String,
    pub oidc_sub: String,
}

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

impl ChangelogRow {
    pub fn into_model(self) -> ChangelogEntry {
        ChangelogEntry {
            index: self.id,
            editgroup_id: uuid2fcid(&self.editgroup_id),
            editgroup: None,
            timestamp: chrono::DateTime::from_utc(self.timestamp, chrono::Utc),
        }
    }
}