Skip to content

Commit

Permalink
Migrate remaining usage of ResourceSyncStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
antweb committed Oct 4, 2024
1 parent 1e4cfe1 commit 8a1d24b
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -161,7 +160,10 @@ class OrderCleanup(val db: SyncDatabase, val store: BlockingEntityStore<Persista
val slug = t.get<String>(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<Tuple> = store.select(Order.ID).where(Order.EVENT_SLUG.eq(slug)).limit(200).get().toList()
if (ordersToDelete.isEmpty()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
selectByResource:
SELECT *
FROM ResourceSyncStatus
WHERE resource = ?;

selectByResourceAndEventSlug:
SELECT *
FROM ResourceSyncStatus
Expand All @@ -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
(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ SELECT currval('quota_id_seq') AS Long;
truncateCheckIn:
TRUNCATE CheckIn;

truncateResourceSyncStatus:
TRUNCATE ResourceSyncStatus;

truncateReusableMedium:
TRUNCATE ReusableMedium;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ SELECT last_insert_rowid();
truncateCheckIn:
DELETE FROM CheckIn;

truncateResourceSyncStatus:
DELETE FROM ResourceSyncStatus;

truncateReusableMedium:
DELETE FROM ReusableMedium;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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")
}
}

0 comments on commit 8a1d24b

Please sign in to comment.