From d6a0ced73af65abfc17835645e8c394f20725dec Mon Sep 17 00:00:00 2001 From: Inal Arsanukaev Date: Wed, 15 Feb 2023 11:58:14 +0300 Subject: [PATCH] Ft/last version (#87) * Added last_version_id table * Added last_version_id table * Fix script * Fix script (2) --------- Co-authored-by: Inal Arsanukaev --- .../dao/dominant/iface/DominantDao.java | 1 + .../dao/dominant/impl/DominantDaoImpl.java | 36 +++++----------- .../handler/dominant/DominantPoller.java | 1 + .../newway/service/DominantService.java | 5 +++ .../V100__dominant_last_version_id.sql | 42 +++++++++++++++++++ .../java/dev/vality/newway/dao/DaoTests.java | 5 ++- 6 files changed, 62 insertions(+), 28 deletions(-) create mode 100644 src/main/resources/db/migration/V100__dominant_last_version_id.sql diff --git a/src/main/java/dev/vality/newway/dao/dominant/iface/DominantDao.java b/src/main/java/dev/vality/newway/dao/dominant/iface/DominantDao.java index 420e860..3c493ec 100644 --- a/src/main/java/dev/vality/newway/dao/dominant/iface/DominantDao.java +++ b/src/main/java/dev/vality/newway/dao/dominant/iface/DominantDao.java @@ -5,4 +5,5 @@ import dev.vality.newway.exception.DaoException; public interface DominantDao extends GenericDao { Long getLastVersionId() throws DaoException; + void updateLastVersionId(Long versionId) throws DaoException; } diff --git a/src/main/java/dev/vality/newway/dao/dominant/impl/DominantDaoImpl.java b/src/main/java/dev/vality/newway/dao/dominant/impl/DominantDaoImpl.java index 773dd2b..48e6aed 100644 --- a/src/main/java/dev/vality/newway/dao/dominant/impl/DominantDaoImpl.java +++ b/src/main/java/dev/vality/newway/dao/dominant/impl/DominantDaoImpl.java @@ -5,12 +5,10 @@ import dev.vality.newway.dao.dominant.iface.DominantDao; import dev.vality.newway.domain.Tables; import dev.vality.newway.exception.DaoException; import org.jooq.Query; -import org.jooq.impl.DSL; import org.springframework.stereotype.Component; import javax.sql.DataSource; -import static org.jooq.impl.DSL.max; @Component public class DominantDaoImpl extends AbstractGenericDao implements DominantDao { @@ -21,30 +19,16 @@ public class DominantDaoImpl extends AbstractGenericDao implements DominantDao { @Override public Long getLastVersionId() throws DaoException { - Query query = getDslContext().select(max(DSL.field("max"))).from( - getDslContext().select(max(Tables.CALENDAR.VERSION_ID)).from(Tables.CALENDAR) - .unionAll(getDslContext().select(max(Tables.CATEGORY.VERSION_ID)).from(Tables.CATEGORY)) - .unionAll(getDslContext().select(max(Tables.COUNTRY.VERSION_ID)).from(Tables.COUNTRY)) - .unionAll(getDslContext().select(max(Tables.CURRENCY.VERSION_ID)).from(Tables.CURRENCY)) - .unionAll(getDslContext().select(max(Tables.INSPECTOR.VERSION_ID)).from(Tables.INSPECTOR)) - .unionAll(getDslContext().select(max(Tables.PAYMENT_INSTITUTION.VERSION_ID)) - .from(Tables.PAYMENT_INSTITUTION)) - .unionAll(getDslContext().select(max(Tables.PAYMENT_METHOD.VERSION_ID)) - .from(Tables.PAYMENT_METHOD)) - .unionAll(getDslContext().select(max(Tables.PAYOUT_METHOD.VERSION_ID)) - .from(Tables.PAYOUT_METHOD)) - .unionAll(getDslContext().select(max(Tables.PROVIDER.VERSION_ID)).from(Tables.PROVIDER)) - .unionAll(getDslContext().select(max(Tables.PROXY.VERSION_ID)).from(Tables.PROXY)) - .unionAll(getDslContext().select(max(Tables.TERMINAL.VERSION_ID)).from(Tables.TERMINAL)) - .unionAll(getDslContext().select(max(Tables.TERM_SET_HIERARCHY.VERSION_ID)) - .from(Tables.TERM_SET_HIERARCHY)) - .unionAll(getDslContext().select(max(Tables.TRADE_BLOC.VERSION_ID)) - .from(Tables.TRADE_BLOC)) - .unionAll(getDslContext().select(max(Tables.WITHDRAWAL_PROVIDER.VERSION_ID)) - .from(Tables.WITHDRAWAL_PROVIDER)) - .unionAll(getDslContext().select(max(Tables.PAYMENT_ROUTING_RULE.VERSION_ID)) - .from(Tables.PAYMENT_ROUTING_RULE)) - ); + Query query = getDslContext() + .select(Tables.DOMINANT_LAST_VERSION_ID.VERSION_ID) + .from(Tables.DOMINANT_LAST_VERSION_ID); return fetchOne(query, Long.class); } + + @Override + public void updateLastVersionId(Long versionId) throws DaoException { + Query query = getDslContext().update(Tables.DOMINANT_LAST_VERSION_ID) + .set(Tables.DOMINANT_LAST_VERSION_ID.VERSION_ID, versionId); + executeOne(query); + } } diff --git a/src/main/java/dev/vality/newway/handler/dominant/DominantPoller.java b/src/main/java/dev/vality/newway/handler/dominant/DominantPoller.java index faf11e8..b265ce8 100644 --- a/src/main/java/dev/vality/newway/handler/dominant/DominantPoller.java +++ b/src/main/java/dev/vality/newway/handler/dominant/DominantPoller.java @@ -45,6 +45,7 @@ public class DominantPoller { try { versionId.set(e.getKey()); dominantService.processCommit(versionId.get(), e); + dominantService.updateLastVersionId(versionId.get()); after.set(versionId.get()); } catch (RuntimeException ex) { throw new RuntimeException( diff --git a/src/main/java/dev/vality/newway/service/DominantService.java b/src/main/java/dev/vality/newway/service/DominantService.java index 3390695..4390821 100644 --- a/src/main/java/dev/vality/newway/service/DominantService.java +++ b/src/main/java/dev/vality/newway/service/DominantService.java @@ -51,4 +51,9 @@ public class DominantService { log.info("Last dominant versionId={}", lastVersionId); return lastVersionId; } + + public void updateLastVersionId(Long lastVersionId) { + dominantDao.updateLastVersionId(lastVersionId); + log.info("Last dominant versionId={} is updated", lastVersionId); + } } diff --git a/src/main/resources/db/migration/V100__dominant_last_version_id.sql b/src/main/resources/db/migration/V100__dominant_last_version_id.sql new file mode 100644 index 0000000..1ab7480 --- /dev/null +++ b/src/main/resources/db/migration/V100__dominant_last_version_id.sql @@ -0,0 +1,42 @@ +CREATE TABLE nw.dominant_last_version_id +( + version_id BIGINT NOT NULL, + wtime TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT (now() at time zone 'utc') +); + +insert into nw.dominant_last_version_id(version_id) values((with max_ids as ( +select max(version_id) as id from nw.CALENDAR +union all +select max(version_id) as id from nw.CATEGORY +union all +select max(version_id) as id from nw.COUNTRY +union all +select max(version_id) as id from nw.CURRENCY +union all +select max(version_id) as id from nw.INSPECTOR +union all +select max(version_id) as id from nw.PAYMENT_INSTITUTION +union all +select max(version_id) as id from nw.PAYMENT_METHOD +union all +select max(version_id) as id from nw.PAYOUT_METHOD +union all +select max(version_id) as id from nw.PROVIDER +union all +select max(version_id) as id from nw.PROXY +union all +select max(version_id) as id from nw.TERMINAL +union all +select max(version_id) as id from nw.TERM_SET_HIERARCHY +union all +select max(version_id) as id from nw.TRADE_BLOC +union all +select max(version_id) as id from nw.WITHDRAWAL_PROVIDER +union all +select max(version_id) as id from nw.PAYMENT_ROUTING_RULE +union all +select max(version_id) as id from nw.CATEGORY) + +select coalesce (max(id),0) from max_ids)) +; + diff --git a/src/test/java/dev/vality/newway/dao/DaoTests.java b/src/test/java/dev/vality/newway/dao/DaoTests.java index 5ec5779..5318548 100644 --- a/src/test/java/dev/vality/newway/dao/DaoTests.java +++ b/src/test/java/dev/vality/newway/dao/DaoTests.java @@ -174,8 +174,6 @@ public class DaoTests { termSetHierarchyDao.save(termSetHierarchy); termSetHierarchyDao.updateNotCurrent(termSetHierarchy.getTermSetHierarchyRefId()); - Long lastVersionId = dominantDao.getLastVersionId(); - OptionalLong maxVersionId = LongStream.of( calendar.getVersionId(), category.getVersionId(), @@ -190,6 +188,9 @@ public class DaoTests { terminal.getVersionId(), termSetHierarchy.getVersionId()).max(); + dominantDao.updateLastVersionId(maxVersionId.getAsLong()); + Long lastVersionId = dominantDao.getLastVersionId(); + Assertions.assertEquals(maxVersionId.getAsLong(), lastVersionId.longValue()); }