5
5
package io .airbyte .config .persistence ;
6
6
7
7
import static io .airbyte .db .instance .configs .jooq .Tables .ACTOR ;
8
+ import static io .airbyte .db .instance .configs .jooq .Tables .ACTOR_CATALOG ;
9
+ import static io .airbyte .db .instance .configs .jooq .Tables .ACTOR_CATALOG_FETCH_EVENT ;
8
10
import static io .airbyte .db .instance .configs .jooq .Tables .ACTOR_DEFINITION ;
9
11
import static io .airbyte .db .instance .configs .jooq .Tables .ACTOR_OAUTH_PARAMETER ;
10
12
import static io .airbyte .db .instance .configs .jooq .Tables .CONNECTION ;
23
25
import io .airbyte .commons .json .Jsons ;
24
26
import io .airbyte .commons .util .MoreIterators ;
25
27
import io .airbyte .commons .version .AirbyteVersion ;
28
+ import io .airbyte .config .ActorCatalog ;
29
+ import io .airbyte .config .ActorCatalogFetchEvent ;
26
30
import io .airbyte .config .AirbyteConfig ;
27
31
import io .airbyte .config .ConfigSchema ;
28
32
import io .airbyte .config .ConfigWithMetadata ;
@@ -114,6 +118,10 @@ public <T> T getConfig(final AirbyteConfig configType, final String configId, fi
114
118
return (T ) getStandardSync (configId );
115
119
} else if (configType == ConfigSchema .STANDARD_SYNC_STATE ) {
116
120
return (T ) getStandardSyncState (configId );
121
+ } else if (configType == ConfigSchema .ACTOR_CATALOG ) {
122
+ return (T ) getActorCatalog (configId );
123
+ } else if (configType == ConfigSchema .ACTOR_CATALOG_FETCH_EVENT ) {
124
+ return (T ) getActorCatalogFetchEvent (configId );
117
125
} else {
118
126
throw new IllegalArgumentException ("Unknown Config Type " + configType );
119
127
}
@@ -181,6 +189,18 @@ private StandardSyncState getStandardSyncState(final String configId) throws IOE
181
189
return result .get (0 ).getConfig ();
182
190
}
183
191
192
+ private ActorCatalog getActorCatalog (final String configId ) throws IOException , ConfigNotFoundException {
193
+ final List <ConfigWithMetadata <ActorCatalog >> result = listActorCatalogWithMetadata (Optional .of (UUID .fromString (configId )));
194
+ validate (configId , result , ConfigSchema .ACTOR_CATALOG );
195
+ return result .get (0 ).getConfig ();
196
+ }
197
+
198
+ private ActorCatalogFetchEvent getActorCatalogFetchEvent (final String configId ) throws IOException , ConfigNotFoundException {
199
+ final List <ConfigWithMetadata <ActorCatalogFetchEvent >> result = listActorCatalogFetchEventWithMetadata (Optional .of (UUID .fromString (configId )));
200
+ validate (configId , result , ConfigSchema .ACTOR_CATALOG_FETCH_EVENT );
201
+ return result .get (0 ).getConfig ();
202
+ }
203
+
184
204
private List <UUID > connectionOperationIds (final UUID connectionId ) throws IOException {
185
205
final Result <Record > result = database .query (ctx -> ctx .select (asterisk ())
186
206
.from (CONNECTION_OPERATION )
@@ -243,6 +263,10 @@ public <T> ConfigWithMetadata<T> getConfigWithMetadata(final AirbyteConfig confi
243
263
return (ConfigWithMetadata <T >) validateAndReturn (configId , listStandardSyncWithMetadata (configIdOpt ), configType );
244
264
} else if (configType == ConfigSchema .STANDARD_SYNC_STATE ) {
245
265
return (ConfigWithMetadata <T >) validateAndReturn (configId , listStandardSyncStateWithMetadata (configIdOpt ), configType );
266
+ } else if (configType == ConfigSchema .ACTOR_CATALOG ) {
267
+ return (ConfigWithMetadata <T >) validateAndReturn (configId , listActorCatalogWithMetadata (configIdOpt ), configType );
268
+ } else if (configType == ConfigSchema .ACTOR_CATALOG_FETCH_EVENT ) {
269
+ return (ConfigWithMetadata <T >) validateAndReturn (configId , listActorCatalogFetchEventWithMetadata (configIdOpt ), configType );
246
270
} else {
247
271
throw new IllegalArgumentException ("Unknown Config Type " + configType );
248
272
}
@@ -271,6 +295,10 @@ public <T> List<ConfigWithMetadata<T>> listConfigsWithMetadata(final AirbyteConf
271
295
listStandardSyncWithMetadata ().forEach (c -> configWithMetadata .add ((ConfigWithMetadata <T >) c ));
272
296
} else if (configType == ConfigSchema .STANDARD_SYNC_STATE ) {
273
297
listStandardSyncStateWithMetadata ().forEach (c -> configWithMetadata .add ((ConfigWithMetadata <T >) c ));
298
+ } else if (configType == ConfigSchema .ACTOR_CATALOG ) {
299
+ listActorCatalogWithMetadata ().forEach (c -> configWithMetadata .add ((ConfigWithMetadata <T >) c ));
300
+ } else if (configType == ConfigSchema .ACTOR_CATALOG_FETCH_EVENT ) {
301
+ listActorCatalogFetchEventWithMetadata ().forEach (c -> configWithMetadata .add ((ConfigWithMetadata <T >) c ));
274
302
} else {
275
303
throw new IllegalArgumentException ("Unknown Config Type " + configType );
276
304
}
@@ -672,6 +700,72 @@ private StandardSyncState buildStandardSyncState(final Record record) {
672
700
.withState (Jsons .deserialize (record .get (STATE .STATE_ ).data (), State .class ));
673
701
}
674
702
703
+ private List <ConfigWithMetadata <ActorCatalog >> listActorCatalogWithMetadata () throws IOException {
704
+ return listActorCatalogWithMetadata (Optional .empty ());
705
+ }
706
+
707
+ private List <ConfigWithMetadata <ActorCatalog >> listActorCatalogWithMetadata (final Optional <UUID > configId ) throws IOException {
708
+ final Result <Record > result = database .query (ctx -> {
709
+ final SelectJoinStep <Record > query = ctx .select (asterisk ()).from (ACTOR_CATALOG );
710
+ if (configId .isPresent ()) {
711
+ return query .where (ACTOR_CATALOG .ID .eq (configId .get ())).fetch ();
712
+ }
713
+ return query .fetch ();
714
+ });
715
+ final List <ConfigWithMetadata <ActorCatalog >> actorCatalogs = new ArrayList <>();
716
+ for (final Record record : result ) {
717
+ final ActorCatalog actorCatalog = buildActorCatalog (record );
718
+ actorCatalogs .add (new ConfigWithMetadata <>(
719
+ record .get (ACTOR_CATALOG .ID ).toString (),
720
+ ConfigSchema .ACTOR_CATALOG .name (),
721
+ record .get (ACTOR_CATALOG .CREATED_AT ).toInstant (),
722
+ record .get (ACTOR_CATALOG .MODIFIED_AT ).toInstant (),
723
+ actorCatalog ));
724
+ }
725
+ return actorCatalogs ;
726
+ }
727
+
728
+ private ActorCatalog buildActorCatalog (final Record record ) {
729
+ return new ActorCatalog ()
730
+ .withId (record .get (ACTOR_CATALOG .ID ))
731
+ .withCatalog (Jsons .deserialize (record .get (ACTOR_CATALOG .CATALOG ).toString ()))
732
+ .withCatalogHash (record .get (ACTOR_CATALOG .CATALOG_HASH ));
733
+ }
734
+
735
+ private List <ConfigWithMetadata <ActorCatalogFetchEvent >> listActorCatalogFetchEventWithMetadata () throws IOException {
736
+ return listActorCatalogFetchEventWithMetadata (Optional .empty ());
737
+ }
738
+
739
+ private List <ConfigWithMetadata <ActorCatalogFetchEvent >> listActorCatalogFetchEventWithMetadata (final Optional <UUID > configId ) throws IOException {
740
+ final Result <Record > result = database .query (ctx -> {
741
+ final SelectJoinStep <Record > query = ctx .select (asterisk ()).from (ACTOR_CATALOG_FETCH_EVENT );
742
+ if (configId .isPresent ()) {
743
+ return query .where (ACTOR_CATALOG_FETCH_EVENT .ID .eq (configId .get ())).fetch ();
744
+ }
745
+ return query .fetch ();
746
+ });
747
+ final List <ConfigWithMetadata <ActorCatalogFetchEvent >> actorCatalogFetchEvents = new ArrayList <>();
748
+ for (final Record record : result ) {
749
+ final ActorCatalogFetchEvent actorCatalogFetchEvent = buildActorCatalogFetchEvent (record );
750
+ actorCatalogFetchEvents .add (new ConfigWithMetadata <>(
751
+ record .get (ACTOR_CATALOG_FETCH_EVENT .ID ).toString (),
752
+ ConfigSchema .ACTOR_CATALOG_FETCH_EVENT .name (),
753
+ record .get (ACTOR_CATALOG_FETCH_EVENT .CREATED_AT ).toInstant (),
754
+ record .get (ACTOR_CATALOG_FETCH_EVENT .MODIFIED_AT ).toInstant (),
755
+ actorCatalogFetchEvent ));
756
+ }
757
+ return actorCatalogFetchEvents ;
758
+ }
759
+
760
+ private ActorCatalogFetchEvent buildActorCatalogFetchEvent (final Record record ) {
761
+ return new ActorCatalogFetchEvent ()
762
+ .withId (record .get (ACTOR_CATALOG_FETCH_EVENT .ID ))
763
+ .withActorCatalogId (record .get (ACTOR_CATALOG_FETCH_EVENT .ACTOR_CATALOG_ID ))
764
+ .withConfigHash (record .get (ACTOR_CATALOG_FETCH_EVENT .CONFIG_HASH ))
765
+ .withConnectorVersion (record .get (ACTOR_CATALOG_FETCH_EVENT .ACTOR_VERSION ))
766
+ .withActorId (record .get (ACTOR_CATALOG_FETCH_EVENT .ACTOR_ID ));
767
+ }
768
+
675
769
@ Override
676
770
public <T > void writeConfig (final AirbyteConfig configType , final String configId , final T config ) throws JsonValidationException , IOException {
677
771
if (configType == ConfigSchema .STANDARD_WORKSPACE ) {
@@ -694,6 +788,10 @@ public <T> void writeConfig(final AirbyteConfig configType, final String configI
694
788
writeStandardSync (Collections .singletonList ((StandardSync ) config ));
695
789
} else if (configType == ConfigSchema .STANDARD_SYNC_STATE ) {
696
790
writeStandardSyncState (Collections .singletonList ((StandardSyncState ) config ));
791
+ } else if (configType == ConfigSchema .ACTOR_CATALOG ) {
792
+ writeActorCatalog (Collections .singletonList ((ActorCatalog ) config ));
793
+ } else if (configType == ConfigSchema .ACTOR_CATALOG_FETCH_EVENT ) {
794
+ writeActorCatalogFetchEvent (Collections .singletonList ((ActorCatalogFetchEvent ) config ));
697
795
} else {
698
796
throw new IllegalArgumentException ("Unknown Config Type " + configType );
699
797
}
@@ -1200,6 +1298,60 @@ private void writeStandardSyncState(final List<StandardSyncState> configs, final
1200
1298
});
1201
1299
}
1202
1300
1301
+ private void writeActorCatalog (final List <ActorCatalog > configs ) throws IOException {
1302
+ database .transaction (ctx -> {
1303
+ writeActorCatalog (configs , ctx );
1304
+ return null ;
1305
+ });
1306
+ }
1307
+
1308
+ private void writeActorCatalog (final List <ActorCatalog > configs , final DSLContext ctx ) {
1309
+ final OffsetDateTime timestamp = OffsetDateTime .now ();
1310
+ configs .forEach ((actorCatalog ) -> {
1311
+ final boolean isExistingConfig = ctx .fetchExists (select ()
1312
+ .from (ACTOR_CATALOG )
1313
+ .where (ACTOR_CATALOG .ID .eq (actorCatalog .getId ())));
1314
+
1315
+ if (isExistingConfig ) {} else {
1316
+ ctx .insertInto (ACTOR_CATALOG )
1317
+ .set (ACTOR_CATALOG .ID , actorCatalog .getId ())
1318
+ .set (ACTOR_CATALOG .CATALOG , JSONB .valueOf (Jsons .serialize (actorCatalog .getCatalog ())))
1319
+ .set (ACTOR_CATALOG .CATALOG_HASH , actorCatalog .getCatalogHash ())
1320
+ .set (ACTOR_CATALOG .CREATED_AT , timestamp )
1321
+ .set (ACTOR_CATALOG .MODIFIED_AT , timestamp )
1322
+ .execute ();
1323
+ }
1324
+ });
1325
+ }
1326
+
1327
+ private void writeActorCatalogFetchEvent (final List <ActorCatalogFetchEvent > configs ) throws IOException {
1328
+ database .transaction (ctx -> {
1329
+ writeActorCatalogFetchEvent (configs , ctx );
1330
+ return null ;
1331
+ });
1332
+ }
1333
+
1334
+ private void writeActorCatalogFetchEvent (final List <ActorCatalogFetchEvent > configs , final DSLContext ctx ) {
1335
+ final OffsetDateTime timestamp = OffsetDateTime .now ();
1336
+ configs .forEach ((actorCatalogFetchEvent ) -> {
1337
+ final boolean isExistingConfig = ctx .fetchExists (select ()
1338
+ .from (ACTOR_CATALOG_FETCH_EVENT )
1339
+ .where (ACTOR_CATALOG_FETCH_EVENT .ID .eq (actorCatalogFetchEvent .getId ())));
1340
+
1341
+ if (isExistingConfig ) {} else {
1342
+ ctx .insertInto (ACTOR_CATALOG_FETCH_EVENT )
1343
+ .set (ACTOR_CATALOG_FETCH_EVENT .ID , actorCatalogFetchEvent .getId ())
1344
+ .set (ACTOR_CATALOG_FETCH_EVENT .CONFIG_HASH , actorCatalogFetchEvent .getConfigHash ())
1345
+ .set (ACTOR_CATALOG_FETCH_EVENT .ACTOR_CATALOG_ID , actorCatalogFetchEvent .getActorCatalogId ())
1346
+ .set (ACTOR_CATALOG_FETCH_EVENT .ACTOR_ID , actorCatalogFetchEvent .getActorId ())
1347
+ .set (ACTOR_CATALOG_FETCH_EVENT .ACTOR_VERSION , actorCatalogFetchEvent .getConnectorVersion ())
1348
+ .set (ACTOR_CATALOG_FETCH_EVENT .CREATED_AT , timestamp )
1349
+ .set (ACTOR_CATALOG_FETCH_EVENT .MODIFIED_AT , timestamp )
1350
+ .execute ();
1351
+ }
1352
+ });
1353
+ }
1354
+
1203
1355
@ Override
1204
1356
public <T > void writeConfigs (final AirbyteConfig configType , final Map <String , T > configs ) throws IOException , JsonValidationException {
1205
1357
if (configType == ConfigSchema .STANDARD_WORKSPACE ) {
@@ -1222,6 +1374,10 @@ public <T> void writeConfigs(final AirbyteConfig configType, final Map<String, T
1222
1374
writeStandardSync (configs .values ().stream ().map (c -> (StandardSync ) c ).collect (Collectors .toList ()));
1223
1375
} else if (configType == ConfigSchema .STANDARD_SYNC_STATE ) {
1224
1376
writeStandardSyncState (configs .values ().stream ().map (c -> (StandardSyncState ) c ).collect (Collectors .toList ()));
1377
+ } else if (configType == ConfigSchema .ACTOR_CATALOG ) {
1378
+ writeActorCatalog (configs .values ().stream ().map (c -> (ActorCatalog ) c ).collect (Collectors .toList ()));
1379
+ } else if (configType == ConfigSchema .ACTOR_CATALOG_FETCH_EVENT ) {
1380
+ writeActorCatalogFetchEvent (configs .values ().stream ().map (c -> (ActorCatalogFetchEvent ) c ).collect (Collectors .toList ()));
1225
1381
} else {
1226
1382
throw new IllegalArgumentException ("Unknown Config Type " + configType );
1227
1383
}
0 commit comments