From 8a1d24bc2ded60fffb7b7dc8195c96cc91dab6df Mon Sep 17 00:00:00 2001 From: Anton Weber Date: Fri, 4 Oct 2024 17:42:59 +0200 Subject: [PATCH] Migrate remaining usage of ResourceSyncStatus --- .../pretix/libpretixsync/sync/OrderCleanup.kt | 6 +- .../libpretixsync/sync/SyncManager.java | 5 +- .../sqldelight/ResourceSyncStatus.sq | 13 +++ .../pretix/libpretixsync/sqldelight/compat.sq | 3 + .../pretix/libpretixsync/sqldelight/compat.sq | 3 + .../libpretixsync/sync/OrderSyncTest.kt | 81 +++++++++---------- 6 files changed, 65 insertions(+), 46 deletions(-) diff --git a/libpretixsync/src/main/java/eu/pretix/libpretixsync/sync/OrderCleanup.kt b/libpretixsync/src/main/java/eu/pretix/libpretixsync/sync/OrderCleanup.kt index c10728ea..1f59c317 100644 --- a/libpretixsync/src/main/java/eu/pretix/libpretixsync/sync/OrderCleanup.kt +++ b/libpretixsync/src/main/java/eu/pretix/libpretixsync/sync/OrderCleanup.kt @@ -4,7 +4,6 @@ import eu.pretix.libpretixsync.api.ApiException import eu.pretix.libpretixsync.api.PretixApi import eu.pretix.libpretixsync.db.Order import eu.pretix.libpretixsync.db.OrderPosition -import eu.pretix.libpretixsync.db.ResourceSyncStatus import eu.pretix.libpretixsync.models.db.toModel import eu.pretix.libpretixsync.sqldelight.SyncDatabase import eu.pretix.libpretixsync.sync.SyncManager.ProgressFeedback @@ -161,7 +160,10 @@ class OrderCleanup(val db: SyncDatabase, val store: BlockingEntityStore(0) val deletionDate = deletionTimeForEvent(slug) if (deletionDate == null || deletionDate < System.currentTimeMillis()) { - store.delete(ResourceSyncStatus::class.java).where(ResourceSyncStatus.RESOURCE.like("order%")).and(ResourceSyncStatus.EVENT_SLUG.eq(slug)) + db.resourceSyncStatusQueries.deleteByResourceFilterAndEventSlug( + filter = "order%", + event_slug = slug, + ) while (true) { val ordersToDelete: List = store.select(Order.ID).where(Order.EVENT_SLUG.eq(slug)).limit(200).get().toList() if (ordersToDelete.isEmpty()) { diff --git a/libpretixsync/src/main/java/eu/pretix/libpretixsync/sync/SyncManager.java b/libpretixsync/src/main/java/eu/pretix/libpretixsync/sync/SyncManager.java index fcde75bc..c07450ad 100644 --- a/libpretixsync/src/main/java/eu/pretix/libpretixsync/sync/SyncManager.java +++ b/libpretixsync/src/main/java/eu/pretix/libpretixsync/sync/SyncManager.java @@ -26,7 +26,6 @@ import eu.pretix.libpretixsync.db.Receipt; import eu.pretix.libpretixsync.db.ReceiptLine; import eu.pretix.libpretixsync.db.ReceiptPayment; -import eu.pretix.libpretixsync.db.ResourceSyncStatus; import io.requery.BlockingEntityStore; import io.requery.Persistable; @@ -479,7 +478,7 @@ protected void downloadData(ProgressFeedback feedback, Boolean skip_orders, Stri db.getCompatQueries().truncateCheckIn(); dataStore.delete(OrderPosition.class).get().value(); dataStore.delete(Order.class).get().value(); - dataStore.delete(ResourceSyncStatus.class).where(ResourceSyncStatus.RESOURCE.like("order%")).get().value(); + db.getResourceSyncStatusQueries().deleteByResourceFilter("order%"); if ((System.currentTimeMillis() - configStore.getLastCleanup()) > 3600 * 1000 * 12) { OrderCleanup oc = new OrderCleanup(db, dataStore, fileStorage, api, configStore.getSyncCycleId(), feedback); oc.deleteOldPdfImages(); @@ -493,7 +492,7 @@ protected void downloadData(ProgressFeedback feedback, Boolean skip_orders, Stri dataStore.delete(OrderPosition.class).get().value(); dataStore.delete(Order.class).get().value(); db.getCompatQueries().truncateReusableMedium(); - dataStore.delete(ResourceSyncStatus.class).get().value(); + db.getCompatQueries().truncateResourceSyncStatus(); throw new SyncException(e.getMessage()); } catch (JSONException e) { e.printStackTrace(); diff --git a/libpretixsync/src/main/sqldelight/common/eu/pretix/libpretixsync/sqldelight/ResourceSyncStatus.sq b/libpretixsync/src/main/sqldelight/common/eu/pretix/libpretixsync/sqldelight/ResourceSyncStatus.sq index 819f2460..3b1b5589 100644 --- a/libpretixsync/src/main/sqldelight/common/eu/pretix/libpretixsync/sqldelight/ResourceSyncStatus.sq +++ b/libpretixsync/src/main/sqldelight/common/eu/pretix/libpretixsync/sqldelight/ResourceSyncStatus.sq @@ -1,3 +1,8 @@ +selectByResource: +SELECT * +FROM ResourceSyncStatus +WHERE resource = ?; + selectByResourceAndEventSlug: SELECT * FROM ResourceSyncStatus @@ -8,6 +13,14 @@ deleteById: DELETE FROM ResourceSyncStatus WHERE id = ?; +deleteByResourceFilter: +DELETE FROM ResourceSyncStatus +WHERE resource LIKE :filter; + +deleteByResourceFilterAndEventSlug: +DELETE FROM ResourceSyncStatus +WHERE resource LIKE :filter AND event_slug = :event_slug; + insert: INSERT INTO ResourceSyncStatus ( diff --git a/libpretixsync/src/main/sqldelight/postgres/eu/pretix/libpretixsync/sqldelight/compat.sq b/libpretixsync/src/main/sqldelight/postgres/eu/pretix/libpretixsync/sqldelight/compat.sq index 415faa9b..5a94d734 100644 --- a/libpretixsync/src/main/sqldelight/postgres/eu/pretix/libpretixsync/sqldelight/compat.sq +++ b/libpretixsync/src/main/sqldelight/postgres/eu/pretix/libpretixsync/sqldelight/compat.sq @@ -28,6 +28,9 @@ SELECT currval('quota_id_seq') AS Long; truncateCheckIn: TRUNCATE CheckIn; +truncateResourceSyncStatus: +TRUNCATE ResourceSyncStatus; + truncateReusableMedium: TRUNCATE ReusableMedium; diff --git a/libpretixsync/src/main/sqldelight/sqlite/eu/pretix/libpretixsync/sqldelight/compat.sq b/libpretixsync/src/main/sqldelight/sqlite/eu/pretix/libpretixsync/sqldelight/compat.sq index 8b37414a..9689dcd0 100644 --- a/libpretixsync/src/main/sqldelight/sqlite/eu/pretix/libpretixsync/sqldelight/compat.sq +++ b/libpretixsync/src/main/sqldelight/sqlite/eu/pretix/libpretixsync/sqldelight/compat.sq @@ -28,6 +28,9 @@ SELECT last_insert_rowid(); truncateCheckIn: DELETE FROM CheckIn; +truncateResourceSyncStatus: +DELETE FROM ResourceSyncStatus; + truncateReusableMedium: DELETE FROM ReusableMedium; diff --git a/libpretixsync/src/test/java/eu/pretix/libpretixsync/sync/OrderSyncTest.kt b/libpretixsync/src/test/java/eu/pretix/libpretixsync/sync/OrderSyncTest.kt index e03fcd5d..f3a6f74e 100644 --- a/libpretixsync/src/test/java/eu/pretix/libpretixsync/sync/OrderSyncTest.kt +++ b/libpretixsync/src/test/java/eu/pretix/libpretixsync/sync/OrderSyncTest.kt @@ -4,7 +4,6 @@ import eu.pretix.libpretixsync.api.ApiException import eu.pretix.libpretixsync.db.BaseDatabaseTest import eu.pretix.libpretixsync.db.Order import eu.pretix.libpretixsync.db.OrderPosition -import eu.pretix.libpretixsync.db.ResourceSyncStatus import eu.pretix.pretixscan.scanproxy.tests.test.FakeConfigStore import eu.pretix.pretixscan.scanproxy.tests.test.FakeFileStorage import eu.pretix.pretixscan.scanproxy.tests.test.FakePretixApi @@ -66,10 +65,10 @@ class OrderSyncTest : BaseDatabaseTest() { assertEquals(2, dataStore.count(Order::class.java).get().value()) assertEquals(5, dataStore.count(OrderPosition::class.java).get().value()) assertEquals(3L, db.checkInQueries.count().executeAsOne()) - val rlm = dataStore.select(ResourceSyncStatus::class.java).where(ResourceSyncStatus.RESOURCE.eq("orders_withpdfdata")).get().first() - assertEquals(rlm.getEvent_slug(), "demo") - assertEquals(rlm.getLast_modified(), "timestamp1") - assertEquals(rlm.getStatus(), "complete") + val rlm = db.resourceSyncStatusQueries.selectByResource("orders_withpdfdata").executeAsList().first() + assertEquals(rlm.event_slug, "demo") + assertEquals(rlm.last_modified, "timestamp1") + assertEquals(rlm.status, "complete") } @Test @@ -101,10 +100,10 @@ class OrderSyncTest : BaseDatabaseTest() { assertEquals(5, dataStore.count(OrderPosition::class.java).get().value()) assertEquals(3L, db.checkInQueries.count().executeAsOne()) - val rlm = dataStore.select(ResourceSyncStatus::class.java).where(ResourceSyncStatus.RESOURCE.eq("orders_withpdfdata")).get().first() - assertEquals(rlm.getEvent_slug(), "demo") - assertEquals(rlm.getLast_modified(), "timestamp1") - assertEquals(rlm.getStatus(), "complete") + val rlm = db.resourceSyncStatusQueries.selectByResource("orders_withpdfdata").executeAsList().first() + assertEquals(rlm.event_slug, "demo") + assertEquals(rlm.last_modified, "timestamp1") + assertEquals(rlm.status, "complete") } @Test @@ -142,10 +141,10 @@ class OrderSyncTest : BaseDatabaseTest() { assertEquals(5, dataStore.count(OrderPosition::class.java).get().value()) assertEquals(3L, db.checkInQueries.count().executeAsOne()) - val rlm = dataStore.select(ResourceSyncStatus::class.java).where(ResourceSyncStatus.RESOURCE.eq("orders_withpdfdata")).get().first() - assertEquals(rlm.getEvent_slug(), "demo") - assertEquals(rlm.getLast_modified(), "timestamp1") - assertEquals(rlm.getStatus(), "incomplete:2019-01-01T00:11:30Z") + val rlm = db.resourceSyncStatusQueries.selectByResource("orders_withpdfdata").executeAsList().first() + assertEquals(rlm.event_slug, "demo") + assertEquals(rlm.last_modified, "timestamp1") + assertEquals(rlm.status, "incomplete:2019-01-01T00:11:30Z") fakeApi.fetchResponses.add { val respdata = JSONObject() @@ -163,10 +162,10 @@ class OrderSyncTest : BaseDatabaseTest() { assertEquals("http://1.1.1.1/api/v1/organizers/demo/events/demo/orders/?testmode=false&exclude=downloads&exclude=payment_date&exclude=payment_provider&exclude=fees&exclude=positions.downloads&exclude=payments&exclude=refunds&pdf_data=true&ordering=datetime&created_since=2019-01-01T00%3A11%3A30Z", fakeApi.lastRequestUrl) assertEquals(4, dataStore.count(Order::class.java).get().value()) - val rlm2 = dataStore.select(ResourceSyncStatus::class.java).where(ResourceSyncStatus.RESOURCE.eq("orders_withpdfdata")).get().first() - assertEquals(rlm2.getEvent_slug(), "demo") - assertEquals(rlm2.getLast_modified(), "timestamp1") - assertEquals(rlm2.getStatus(), "complete") + val rlm2 = db.resourceSyncStatusQueries.selectByResource("orders_withpdfdata").executeAsList().first() + assertEquals(rlm2.event_slug, "demo") + assertEquals(rlm2.last_modified, "timestamp1") + assertEquals(rlm2.status, "complete") } @Test @@ -204,10 +203,10 @@ class OrderSyncTest : BaseDatabaseTest() { assertEquals(5, dataStore.count(OrderPosition::class.java).get().value()) assertEquals(3L, db.checkInQueries.count().executeAsOne()) - val rlm = dataStore.select(ResourceSyncStatus::class.java).where(ResourceSyncStatus.RESOURCE.eq("orders_withpdfdata")).get().first() - assertEquals(rlm.getEvent_slug(), "demo") - assertEquals(rlm.getLast_modified(), "timestamp1") - assertEquals(rlm.getStatus(), "incomplete:2019-01-01T00:11:30Z") + val rlm = db.resourceSyncStatusQueries.selectByResource("orders_withpdfdata").executeAsList().first() + assertEquals(rlm.event_slug, "demo") + assertEquals(rlm.last_modified, "timestamp1") + assertEquals(rlm.status, "incomplete:2019-01-01T00:11:30Z") fakeApi.fetchResponses.add { @@ -231,10 +230,10 @@ class OrderSyncTest : BaseDatabaseTest() { assertEquals("%page4?testmode=false&exclude=downloads&exclude=payment_date&exclude=payment_provider&exclude=fees&exclude=positions.downloads&exclude=payments&exclude=refunds&pdf_data=true&ordering=datetime&created_since=2019-01-01T00%3A11%3A30Z", fakeApi.lastRequestUrl) assertEquals(3, dataStore.count(Order::class.java).get().value()) - val rlm3 = dataStore.select(ResourceSyncStatus::class.java).where(ResourceSyncStatus.RESOURCE.eq("orders_withpdfdata")).get().first() - assertEquals(rlm3.getEvent_slug(), "demo") - assertEquals(rlm3.getLast_modified(), "timestamp1") - assertEquals(rlm3.getStatus(), "incomplete:2019-01-01T00:15:15Z") + val rlm3 = db.resourceSyncStatusQueries.selectByResource("orders_withpdfdata").executeAsList().first() + assertEquals(rlm3.event_slug, "demo") + assertEquals(rlm3.last_modified, "timestamp1") + assertEquals(rlm3.status, "incomplete:2019-01-01T00:15:15Z") fakeApi.fetchResponses.add { @@ -252,10 +251,10 @@ class OrderSyncTest : BaseDatabaseTest() { assertEquals("http://1.1.1.1/api/v1/organizers/demo/events/demo/orders/?testmode=false&exclude=downloads&exclude=payment_date&exclude=payment_provider&exclude=fees&exclude=positions.downloads&exclude=payments&exclude=refunds&pdf_data=true&ordering=datetime&created_since=2019-01-01T00%3A15%3A15Z", fakeApi.lastRequestUrl) assertEquals(4, dataStore.count(Order::class.java).get().value()) - val rlm2 = dataStore.select(ResourceSyncStatus::class.java).where(ResourceSyncStatus.RESOURCE.eq("orders_withpdfdata")).get().first() - assertEquals(rlm2.getEvent_slug(), "demo") - assertEquals(rlm2.getLast_modified(), "timestamp1") - assertEquals(rlm2.getStatus(), "complete") + val rlm2 = db.resourceSyncStatusQueries.selectByResource("orders_withpdfdata").executeAsList().first() + assertEquals(rlm2.event_slug, "demo") + assertEquals(rlm2.last_modified, "timestamp1") + assertEquals(rlm2.status, "complete") } @Test @@ -293,10 +292,10 @@ class OrderSyncTest : BaseDatabaseTest() { assertEquals("http://1.1.1.1/api/v1/organizers/demo/events/demo/orders/?testmode=false&exclude=downloads&exclude=payment_date&exclude=payment_provider&exclude=fees&exclude=positions.downloads&exclude=payments&exclude=refunds&pdf_data=true&ordering=-last_modified&modified_since=timestamp1", fakeApi.lastRequestUrl) assertEquals(11, dataStore.count(OrderPosition::class.java).get().value()) assertEquals(6L, db.checkInQueries.count().executeAsOne()) - val rlm = dataStore.select(ResourceSyncStatus::class.java).where(ResourceSyncStatus.RESOURCE.eq("orders_withpdfdata")).get().first() - assertEquals(rlm.getEvent_slug(), "demo") - assertEquals(rlm.getLast_modified(), "timestamp2") - assertEquals(rlm.getStatus(), "complete") + val rlm = db.resourceSyncStatusQueries.selectByResource("orders_withpdfdata").executeAsList().first() + assertEquals(rlm.event_slug, "demo") + assertEquals(rlm.last_modified, "timestamp2") + assertEquals(rlm.status, "complete") } @Test @@ -339,10 +338,10 @@ class OrderSyncTest : BaseDatabaseTest() { assertEquals(4, dataStore.count(Order::class.java).get().value()) assertEquals(6L, db.checkInQueries.count().executeAsOne()) - val rlm = dataStore.select(ResourceSyncStatus::class.java).where(ResourceSyncStatus.RESOURCE.eq("orders_withpdfdata")).get().first() - assertEquals(rlm.getEvent_slug(), "demo") - assertEquals(rlm.getLast_modified(), "timestamp1") - assertEquals(rlm.getStatus(), "complete") + val rlm = db.resourceSyncStatusQueries.selectByResource("orders_withpdfdata").executeAsList().first() + assertEquals(rlm.event_slug, "demo") + assertEquals(rlm.last_modified, "timestamp1") + assertEquals(rlm.status, "complete") fakeApi.fetchResponses.add { val respdata = JSONObject() @@ -359,9 +358,9 @@ class OrderSyncTest : BaseDatabaseTest() { assertEquals("http://1.1.1.1/api/v1/organizers/demo/events/demo/orders/?testmode=false&exclude=downloads&exclude=payment_date&exclude=payment_provider&exclude=fees&exclude=positions.downloads&exclude=payments&exclude=refunds&pdf_data=true&ordering=-last_modified&modified_since=timestamp1", fakeApi.lastRequestUrl) assertEquals(11, dataStore.count(OrderPosition::class.java).get().value()) assertEquals(6L, db.checkInQueries.count().executeAsOne()) - val rlm2 = dataStore.select(ResourceSyncStatus::class.java).where(ResourceSyncStatus.RESOURCE.eq("orders_withpdfdata")).get().first() - assertEquals(rlm2.getEvent_slug(), "demo") - assertEquals(rlm2.getLast_modified(), "timestamp2") - assertEquals(rlm2.getStatus(), "complete") + val rlm2 = db.resourceSyncStatusQueries.selectByResource("orders_withpdfdata").executeAsList().first() + assertEquals(rlm2.event_slug, "demo") + assertEquals(rlm2.last_modified, "timestamp2") + assertEquals(rlm2.status, "complete") } }