-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathchangelog.rs
715 lines (649 loc) · 27 KB
/
changelog.rs
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
use crate::{
db_diesel::store_row::store, diesel_macros::apply_equal_filter, name_link, DBType, EqualFilter,
LockedConnection, NameLinkRow, RepositoryError, StorageConnection,
};
use diesel::{
helper_types::{IntoBoxed, LeftJoin},
prelude::*,
};
use serde::{Deserialize, Serialize};
use std::convert::TryInto;
use strum::EnumIter;
use strum::IntoEnumIterator;
use util::inline_init;
use diesel_derive_enum::DbEnum;
table! {
changelog (cursor) {
cursor -> BigInt,
table_name -> crate::db_diesel::changelog::ChangelogTableNameMapping,
record_id -> Text,
row_action -> crate::db_diesel::changelog::RowActionTypeMapping,
name_link_id -> Nullable<Text>,
store_id -> Nullable<Text>,
is_sync_update -> Bool,
source_site_id -> Nullable<Integer>,
}
}
table! {
changelog_deduped (cursor) {
cursor -> BigInt,
table_name -> crate::db_diesel::changelog::ChangelogTableNameMapping,
record_id -> Text,
row_action -> crate::db_diesel::changelog::RowActionTypeMapping,
name_link_id -> Nullable<Text>,
store_id -> Nullable<Text>,
is_sync_update -> Bool,
source_site_id -> Nullable<Integer>,
}
}
joinable!(changelog_deduped -> name_link (name_link_id));
allow_tables_to_appear_in_same_query!(changelog_deduped, name_link);
#[cfg(not(feature = "postgres"))]
define_sql_function!(
fn last_insert_rowid() -> BigInt
);
#[derive(DbEnum, Debug, Clone, PartialEq, Eq, Default)]
#[DbValueStyle = "SCREAMING_SNAKE_CASE"]
pub enum RowActionType {
#[default]
Upsert,
Delete,
}
#[derive(DbEnum, Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize, EnumIter)]
#[DbValueStyle = "snake_case"]
pub enum ChangelogTableName {
BackendPlugin,
Number,
Location,
LocationMovement,
StockLine,
Invoice,
InvoiceLine,
Stocktake,
StocktakeLine,
Requisition,
RequisitionLine,
ActivityLog,
InventoryAdjustmentReason,
Barcode,
Clinician,
ClinicianStoreJoin,
ColdStorageType,
Name,
NameStoreJoin,
Document,
Sensor,
TemperatureBreach,
TemperatureBreachConfig,
TemperatureLog,
PackVariant,
Currency,
AssetClass,
AssetCategory,
AssetCatalogueType,
AssetCatalogueItem,
AssetCatalogueItemProperty,
AssetCatalogueProperty,
AssetInternalLocation,
#[default]
SyncFileReference,
Asset,
AssetLog,
AssetLogReason,
AssetProperty,
Property,
NameProperty,
NameOmsFields,
RnrForm,
RnrFormLine,
Demographic,
VaccineCourse,
VaccineCourseItem,
VaccineCourseDose,
Vaccination,
ItemVariant,
PackagingVariant,
IndicatorValue,
BundledItem,
Item,
ContactForm,
SystemLog,
InsuranceProvider,
FrontendPlugin,
NameInsuranceJoin,
Report,
FormSchema,
PluginData,
Preference,
}
pub(crate) enum ChangeLogSyncStyle {
Legacy, // Everything that goes to Legacy mSupply server
Central, // Data created on Open-mSupply central server
Remote,
File,
RemoteAndCentral, // These records will sync like remote record if store_id exist, otherwise they will sync like central records
RemoteToCentral, // These records won't sync back to the remote site on re-initalisation
}
// When adding a new change log record type, specify how it should be synced
// If new requirements are needed a different ChangeLogSyncStyle can be added
impl ChangelogTableName {
pub(crate) fn sync_style(&self) -> ChangeLogSyncStyle {
match self {
ChangelogTableName::BackendPlugin => ChangeLogSyncStyle::Central,
ChangelogTableName::Number => ChangeLogSyncStyle::Legacy,
ChangelogTableName::Location => ChangeLogSyncStyle::Legacy,
ChangelogTableName::LocationMovement => ChangeLogSyncStyle::Legacy,
ChangelogTableName::StockLine => ChangeLogSyncStyle::Legacy,
ChangelogTableName::Invoice => ChangeLogSyncStyle::Legacy,
ChangelogTableName::InvoiceLine => ChangeLogSyncStyle::Legacy,
ChangelogTableName::Stocktake => ChangeLogSyncStyle::Legacy,
ChangelogTableName::StocktakeLine => ChangeLogSyncStyle::Legacy,
ChangelogTableName::Requisition => ChangeLogSyncStyle::Legacy,
ChangelogTableName::RequisitionLine => ChangeLogSyncStyle::Legacy,
ChangelogTableName::ActivityLog => ChangeLogSyncStyle::Legacy,
ChangelogTableName::InventoryAdjustmentReason => ChangeLogSyncStyle::Legacy,
ChangelogTableName::Barcode => ChangeLogSyncStyle::Legacy,
ChangelogTableName::Clinician => ChangeLogSyncStyle::Legacy,
ChangelogTableName::ClinicianStoreJoin => ChangeLogSyncStyle::Legacy,
ChangelogTableName::Name => ChangeLogSyncStyle::Legacy,
ChangelogTableName::NameStoreJoin => ChangeLogSyncStyle::Legacy,
ChangelogTableName::Document => ChangeLogSyncStyle::Legacy,
ChangelogTableName::Sensor => ChangeLogSyncStyle::Legacy,
ChangelogTableName::TemperatureBreach => ChangeLogSyncStyle::Legacy,
ChangelogTableName::TemperatureBreachConfig => ChangeLogSyncStyle::Legacy,
ChangelogTableName::TemperatureLog => ChangeLogSyncStyle::Legacy,
ChangelogTableName::Currency => ChangeLogSyncStyle::Legacy,
ChangelogTableName::Item => ChangeLogSyncStyle::Legacy,
ChangelogTableName::PackVariant => ChangeLogSyncStyle::Central,
ChangelogTableName::AssetClass => ChangeLogSyncStyle::Central,
ChangelogTableName::AssetCategory => ChangeLogSyncStyle::Central,
ChangelogTableName::AssetCatalogueType => ChangeLogSyncStyle::Central,
ChangelogTableName::AssetCatalogueItem => ChangeLogSyncStyle::Central,
ChangelogTableName::Asset => ChangeLogSyncStyle::Remote,
ChangelogTableName::AssetInternalLocation => ChangeLogSyncStyle::Remote,
ChangelogTableName::SyncFileReference => ChangeLogSyncStyle::File,
ChangelogTableName::AssetLog => ChangeLogSyncStyle::Remote,
ChangelogTableName::AssetCatalogueItemProperty => ChangeLogSyncStyle::Central,
ChangelogTableName::AssetCatalogueProperty => ChangeLogSyncStyle::Central,
ChangelogTableName::AssetLogReason => ChangeLogSyncStyle::Central,
ChangelogTableName::AssetProperty => ChangeLogSyncStyle::Central,
ChangelogTableName::Property => ChangeLogSyncStyle::Central,
ChangelogTableName::NameProperty => ChangeLogSyncStyle::Central,
ChangelogTableName::NameOmsFields => ChangeLogSyncStyle::Central,
ChangelogTableName::RnrForm => ChangeLogSyncStyle::Remote,
ChangelogTableName::RnrFormLine => ChangeLogSyncStyle::Remote,
ChangelogTableName::Demographic => ChangeLogSyncStyle::Central,
ChangelogTableName::VaccineCourse => ChangeLogSyncStyle::Central,
ChangelogTableName::VaccineCourseItem => ChangeLogSyncStyle::Central,
ChangelogTableName::VaccineCourseDose => ChangeLogSyncStyle::Central,
ChangelogTableName::Vaccination => ChangeLogSyncStyle::Remote,
ChangelogTableName::ColdStorageType => ChangeLogSyncStyle::Legacy,
ChangelogTableName::ItemVariant => ChangeLogSyncStyle::Central,
ChangelogTableName::PackagingVariant => ChangeLogSyncStyle::Central,
ChangelogTableName::IndicatorValue => ChangeLogSyncStyle::Legacy,
ChangelogTableName::BundledItem => ChangeLogSyncStyle::Central,
ChangelogTableName::ContactForm => ChangeLogSyncStyle::RemoteToCentral,
ChangelogTableName::SystemLog => ChangeLogSyncStyle::RemoteToCentral, // System Log records won't be synced to remote site on initialisation
ChangelogTableName::InsuranceProvider => ChangeLogSyncStyle::Legacy,
ChangelogTableName::FrontendPlugin => ChangeLogSyncStyle::Central,
ChangelogTableName::NameInsuranceJoin => ChangeLogSyncStyle::Legacy,
ChangelogTableName::Report => ChangeLogSyncStyle::Central,
ChangelogTableName::FormSchema => ChangeLogSyncStyle::Central,
ChangelogTableName::PluginData => ChangeLogSyncStyle::RemoteAndCentral,
ChangelogTableName::Preference => ChangeLogSyncStyle::Central,
}
}
}
#[derive(Debug, PartialEq, Insertable, Default)]
#[diesel(table_name = changelog)]
pub struct ChangeLogInsertRow {
pub table_name: ChangelogTableName,
pub record_id: String,
pub row_action: RowActionType,
pub name_link_id: Option<String>,
pub store_id: Option<String>,
}
#[derive(Clone, Queryable, Debug, PartialEq, Insertable)]
#[diesel(table_name = changelog)]
pub struct ChangelogRow {
pub cursor: i64,
pub table_name: ChangelogTableName,
pub record_id: String,
pub row_action: RowActionType,
#[diesel(column_name = "name_link_id")]
pub name_id: Option<String>,
pub store_id: Option<String>,
pub is_sync_update: bool,
pub source_site_id: Option<i32>,
}
#[derive(Default, Clone)]
pub struct ChangelogFilter {
pub table_name: Option<EqualFilter<ChangelogTableName>>,
pub name_id: Option<EqualFilter<String>>,
pub store_id: Option<EqualFilter<String>>,
pub record_id: Option<EqualFilter<String>>,
pub action: Option<EqualFilter<RowActionType>>,
pub is_sync_update: Option<EqualFilter<bool>>,
pub source_site_id: Option<EqualFilter<i32>>,
}
pub struct ChangelogRepository<'a> {
connection: &'a StorageConnection,
}
type ChangelogJoin = (ChangelogRow, Option<NameLinkRow>);
impl<'a> ChangelogRepository<'a> {
pub fn new(connection: &'a StorageConnection) -> Self {
ChangelogRepository { connection }
}
/// Returns changelog rows order by operation sequence in asc order
///
/// # Arguments
///
/// * `earliest` - Starting cursor (first returned changelogs may be ahead in sequence from starting cursor)
/// * `limit` - Maximum number of entries to be returned
/// * `filter` - Extra filter to apply on change_logs
pub fn changelogs(
&self,
earliest: u64,
limit: u32,
filter: Option<ChangelogFilter>,
) -> Result<Vec<ChangelogRow>, RepositoryError> {
let result = with_locked_changelog_table(self.connection, |locked_con| {
let query = create_filtered_query(earliest, filter)
.order(changelog_deduped::dsl::cursor.asc())
.limit(limit.into());
// // Debug diesel query
// println!(
// "{}",
// diesel::debug_query::<crate::DBType, _>(&query).to_string()
// );
let result: Vec<ChangelogJoin> = query.load(locked_con.connection())?;
Ok(result
.into_iter()
.map(|(change_log_row, name_link_row)| ChangelogRow {
cursor: change_log_row.cursor,
table_name: change_log_row.table_name,
record_id: change_log_row.record_id,
row_action: change_log_row.row_action,
name_id: name_link_row.map(|r| r.name_id),
store_id: change_log_row.store_id,
is_sync_update: change_log_row.is_sync_update,
source_site_id: change_log_row.source_site_id,
})
.collect())
})?;
Ok(result)
}
pub fn count(
&self,
earliest: u64,
filter: Option<ChangelogFilter>,
) -> Result<u64, RepositoryError> {
let result = create_filtered_query(earliest, filter)
.count()
.get_result::<i64>(self.connection.lock().connection())?;
Ok(result as u64)
}
pub fn outgoing_sync_records_from_central(
&self,
earliest: u64,
batch_size: u32,
sync_site_id: i32,
is_initialized: bool,
) -> Result<Vec<ChangelogRow>, RepositoryError> {
let result = with_locked_changelog_table(self.connection, |locked_con| {
let query = create_filtered_outgoing_sync_query(earliest, sync_site_id, is_initialized)
.order(changelog_deduped::cursor.asc())
.limit(batch_size.into());
// Debug diesel query
// println!(
// "{}",
// diesel::debug_query::<crate::DBType, _>(&query).to_string()
// );
let result: Vec<ChangelogJoin> = query.load(locked_con.connection())?;
Ok(result
.into_iter()
.map(|(change_log_row, name_link_row)| ChangelogRow {
cursor: change_log_row.cursor,
table_name: change_log_row.table_name,
record_id: change_log_row.record_id,
row_action: change_log_row.row_action,
name_id: name_link_row.map(|r| r.name_id),
store_id: change_log_row.store_id,
is_sync_update: change_log_row.is_sync_update,
source_site_id: change_log_row.source_site_id,
})
.collect())
})?;
Ok(result)
}
/// This returns the number of changelog records that should be evaluated to send to the remote site when doing a v6_pull
/// This looks up associated records to decide if change log should be sent to the site or not
/// Update this method when adding new record types to the system
pub fn count_outgoing_sync_records_from_central(
&self,
earliest: u64,
sync_site_id: i32,
is_initialized: bool,
) -> Result<u64, RepositoryError> {
let result = create_filtered_outgoing_sync_query(earliest, sync_site_id, is_initialized)
.count()
.get_result::<i64>(self.connection.lock().connection())?;
Ok(result as u64)
}
/// Returns latest change log
/// After initial sync we use this method to get the latest cursor to make sure we don't try to push any records that were synced to this site on initialisation
pub fn latest_cursor(&self) -> Result<u64, RepositoryError> {
let result = changelog::table
.select(diesel::dsl::max(changelog::cursor))
.first::<Option<i64>>(self.connection.lock().connection())?;
Ok(result.unwrap_or(0) as u64)
}
// Delete all change logs with cursor greater-equal cursor_ge
pub fn delete(&self, cursor_ge: i64) -> Result<(), RepositoryError> {
diesel::delete(changelog::dsl::changelog)
.filter(changelog::dsl::cursor.ge(cursor_ge))
.execute(self.connection.lock().connection())?;
Ok(())
}
// Needed for tests, when is_sync_update needs to be reset when records were inserted via
// PullUpsertRecord (but not through sync)
#[cfg(feature = "integration_test")]
pub fn reset_is_sync_update(&self, from_cursor: u64) -> Result<(), RepositoryError> {
diesel::update(changelog::table)
.set(changelog::is_sync_update.eq(false))
.filter(changelog::cursor.gt(from_cursor as i64))
.execute(self.connection.lock().connection())?;
Ok(())
}
pub fn set_source_site_id_and_is_sync_update(
&self,
cursor_id: i64,
source_site_id: Option<i32>,
) -> Result<(), RepositoryError> {
diesel::update(changelog::table)
.set((
changelog::source_site_id.eq(source_site_id),
changelog::is_sync_update.eq(true),
))
.filter(changelog::cursor.eq(cursor_id))
.execute(self.connection.lock().connection())?;
Ok(())
}
/// Inserts a changelog record, and returns the cursor of the inserted record
#[cfg(feature = "postgres")]
pub fn insert(&self, row: &ChangeLogInsertRow) -> Result<i64, RepositoryError> {
// Insert the record, and then return the cursor of the inserted record
// Using a returning clause makes this thread safe
let cursor_id = diesel::insert_into(changelog::table)
.values(row)
.returning(changelog::cursor)
.get_results(self.connection.lock().connection())?
.pop()
.unwrap_or_default(); // This shouldn't happen, maybe should unwrap or panic?
Ok(cursor_id)
}
#[cfg(not(feature = "postgres"))]
pub fn insert(&self, row: &ChangeLogInsertRow) -> Result<i64, RepositoryError> {
// Insert the record, and then return the cursor of the inserted record
// SQLite docs say this is safe if you don't have different threads sharing a single connection
diesel::insert_into(changelog::table)
.values(row)
.execute(self.connection.lock().connection())?;
let cursor_id = diesel::select(last_insert_rowid())
.get_result::<i64>(self.connection.lock().connection())?;
Ok(cursor_id)
}
}
type BoxedChangelogQuery =
IntoBoxed<'static, LeftJoin<changelog_deduped::table, name_link::table>, DBType>;
fn create_filtered_query(earliest: u64, filter: Option<ChangelogFilter>) -> BoxedChangelogQuery {
let mut query = changelog_deduped::table
.left_join(name_link::table)
.filter(changelog_deduped::cursor.ge(earliest.try_into().unwrap_or(0)))
.into_boxed();
if let Some(f) = filter {
let ChangelogFilter {
table_name,
name_id,
store_id,
record_id,
is_sync_update,
action,
source_site_id,
} = f;
apply_equal_filter!(query, table_name, changelog_deduped::table_name);
apply_equal_filter!(query, name_id, name_link::name_id);
apply_equal_filter!(query, store_id, changelog_deduped::store_id);
apply_equal_filter!(query, record_id, changelog_deduped::record_id);
apply_equal_filter!(query, action, changelog_deduped::row_action);
apply_equal_filter!(query, is_sync_update, changelog_deduped::is_sync_update);
apply_equal_filter!(query, source_site_id, changelog_deduped::source_site_id);
}
query
}
// The idea for this method is to build a query in such a way as to allow
// extracting all relevant records for a site from change_log
// A resulting SQL might look something like this...
//
// SELECT * FROM changelog_dedup
// WHERE cursor > {remote site SyncPullCursorV6} AND last_sync_site_id != {remote site id}
// AND
// (
// table_name in {central_record_names}
// OR
// (table_name in {transfer record names} AND name_id IN {name_ids of active stores on remote site})
// OR
// // Special cases
// (table_name in {patient record name} AND patient_id IN {select name_id from name_store_join where store_id in {active stores on remote site})
// )
/// This looks up associated records to decide if change log should be sent to the site or not
/// Update this method when adding new sync styles to the system
fn create_filtered_outgoing_sync_query(
earliest: u64,
sync_site_id: i32,
is_initialized: bool,
) -> BoxedChangelogQuery {
let mut query = changelog_deduped::table
.left_join(name_link::table)
.filter(changelog_deduped::cursor.ge(earliest.try_into().unwrap_or(0)))
.into_boxed();
// If we are initialising, we want to send all the records for the site, even ones that originally came from the site
// The rest of the time we want to exclude any records that were created by the site
if is_initialized {
query = query.filter(
changelog_deduped::source_site_id
.ne(Some(sync_site_id))
.or(changelog_deduped::source_site_id.is_null()),
)
}
// Loop through all the Sync tables and add them to the query if they have the right sync style
// Central Records
let central_sync_table_names: Vec<ChangelogTableName> = ChangelogTableName::iter()
.filter(|table| matches!(table.sync_style(), ChangeLogSyncStyle::Central))
.collect();
// Remote Records
let remote_sync_table_names: Vec<ChangelogTableName> = ChangelogTableName::iter()
.filter(|table| {
matches!(
table.sync_style(),
ChangeLogSyncStyle::Remote | ChangeLogSyncStyle::RemoteAndCentral
)
})
.collect();
// Central record where store id is null
let central_by_empty_store_id: Vec<ChangelogTableName> = ChangelogTableName::iter()
.filter(|table| matches!(table.sync_style(), ChangeLogSyncStyle::RemoteAndCentral))
.collect();
let active_stores_for_site = store::table
.filter(store::site_id.eq(sync_site_id))
.select(store::id.nullable())
.into_boxed();
// Filter the query for the matching records for each type
query = query.filter(
changelog_deduped::table_name
.eq_any(central_sync_table_names)
.or(changelog_deduped::table_name.eq(ChangelogTableName::SyncFileReference)) // All sites get all sync file references (not necessarily files)
.or(changelog_deduped::table_name
.eq_any(remote_sync_table_names)
.and(changelog_deduped::store_id.eq_any(active_stores_for_site)))
.or(changelog_deduped::table_name
.eq_any(central_by_empty_store_id)
.and(changelog_deduped::store_id.is_null())),
// Any other special cases could be handled here...
);
query
}
/// Runs some DB operation with a fully locked `changelog` table.
/// This only applies for for Postgres and does nothing for Sqlite.
///
/// Motivation:
/// When querying changelog entries, ongoing transactions might continue adding changelog entries
/// to the queried range of changelogs.
/// This is because Postgres has Read Committed isolation level (instead of Serialized in Sqlite).
/// However, we assume that there will be no new changelog entries in the queried range in the
/// future, e.g. when updating the cursor position.
///
/// For example, a changelog may contain [1, 3, 4, 5] while another (slow) tx is about to commit a
/// changelog row with cursor = 2.
/// We need to wait for this changelog 2 to be added before doing the changelogs() query, otherwise
/// we might update the latest changelog cursor to 5 and the changelog with cursor = 2 will be left
/// unhandled when continuing from the latest cursor position.
///
/// Locking the changelog table will wait for ongoing writers and will prevent new writers while
/// reading the changelog.
fn with_locked_changelog_table<T, F>(
connection: &StorageConnection,
f: F,
) -> Result<T, RepositoryError>
where
F: FnOnce(&mut LockedConnection) -> Result<T, RepositoryError>,
{
if cfg!(feature = "postgres") {
use diesel::connection::SimpleConnection;
let result = connection.transaction_sync_etc(
|con| {
let mut locked_con = con.lock();
locked_con
.connection()
.batch_execute("LOCK TABLE ONLY changelog IN ACCESS EXCLUSIVE MODE")?;
f(&mut locked_con)
},
false,
)?;
Ok(result)
} else {
let mut locked_con = connection.lock();
f(&mut locked_con)
}
}
// Only used in tests (cfg flag doesn't seem to work for inline_init even in tests)
impl Default for ChangelogRow {
fn default() -> Self {
Self {
row_action: RowActionType::Upsert,
table_name: ChangelogTableName::Invoice,
// Default
cursor: Default::default(),
record_id: Default::default(),
name_id: Default::default(),
store_id: Default::default(),
is_sync_update: Default::default(),
source_site_id: Default::default(),
}
}
}
impl ChangelogFilter {
pub fn new() -> Self {
Default::default()
}
pub fn table_name(mut self, filter: EqualFilter<ChangelogTableName>) -> Self {
self.table_name = Some(filter);
self
}
pub fn name_id(mut self, filter: EqualFilter<String>) -> Self {
self.name_id = Some(filter);
self
}
pub fn store_id(mut self, filter: EqualFilter<String>) -> Self {
self.store_id = Some(filter);
self
}
pub fn record_id(mut self, filter: EqualFilter<String>) -> Self {
self.record_id = Some(filter);
self
}
pub fn action(mut self, filter: EqualFilter<RowActionType>) -> Self {
self.action = Some(filter);
self
}
pub fn is_sync_update(mut self, filter: EqualFilter<bool>) -> Self {
self.is_sync_update = Some(filter);
self
}
pub fn source_site_id(mut self, filter: EqualFilter<i32>) -> Self {
self.source_site_id = Some(filter);
self
}
}
impl ChangelogTableName {
pub fn equal_to(&self) -> EqualFilter<Self> {
inline_init(|r: &mut EqualFilter<Self>| r.equal_to = Some(self.clone()))
}
}
impl RowActionType {
pub fn equal_to(&self) -> EqualFilter<Self> {
inline_init(|r: &mut EqualFilter<Self>| r.equal_to = Some(self.clone()))
}
}
#[cfg(test)]
mod test {
use tokio::sync::oneshot;
use util::inline_init;
use crate::{
mock::MockDataInserts, test_db::setup_all, ChangelogRepository, ClinicianRow,
ClinicianRowRepository, RepositoryError, TransactionError,
};
/// Example from with_locked_changelog_table() comment
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_late_changelog_rows() {
let (_, connection, connection_manager, _) =
setup_all("test_late_changelog_rows", MockDataInserts::none()).await;
ClinicianRowRepository::new(&connection)
.upsert_one(&inline_init(|r: &mut ClinicianRow| {
r.id = String::from("1");
r.is_active = true;
}))
.unwrap();
let (sender, receiver) = oneshot::channel::<()>();
let manager_2 = connection_manager.clone();
let process_2 = tokio::spawn(async move {
let connection = manager_2.connection().unwrap();
let result: Result<(), TransactionError<RepositoryError>> = connection
.transaction_sync(|con| {
ClinicianRowRepository::new(con)
.upsert_one(&inline_init(|r: &mut ClinicianRow| {
r.id = String::from("2");
r.is_active = true;
}))
.unwrap();
sender.send(()).unwrap();
std::thread::sleep(core::time::Duration::from_millis(100));
Ok(())
});
result
});
receiver.await.unwrap();
ClinicianRowRepository::new(&connection)
.upsert_one(&inline_init(|r: &mut ClinicianRow| {
r.id = String::from("3");
r.is_active = true;
}))
.unwrap();
let changelogs = ChangelogRepository::new(&connection)
.changelogs(0, 10, None)
.unwrap();
assert_eq!(changelogs.len(), 3);
// being good and awaiting the task to finish orderly and check it did run fine
process_2.await.unwrap().unwrap();
}
}