62
62
import java .nio .file .Files ;
63
63
import java .nio .file .Path ;
64
64
import java .util .Collection ;
65
- import java .util .Collections ;
66
65
import java .util .Comparator ;
67
66
import java .util .HashMap ;
68
- import java .util .HashSet ;
69
67
import java .util .LinkedHashMap ;
70
68
import java .util .List ;
71
69
import java .util .Map ;
72
- import java .util .Map .Entry ;
73
70
import java .util .Optional ;
74
71
import java .util .Set ;
75
72
import java .util .Spliterator ;
@@ -131,7 +128,7 @@ public void importDataWithSeed(String targetVersion, File archive, ConfigPersist
131
128
// 2. dry run
132
129
try {
133
130
checkImport (targetVersion , sourceRoot );
134
- importConfigsFromArchive (sourceRoot , seedPersistence , true );
131
+ importConfigsFromArchive (sourceRoot , true );
135
132
} catch (Exception e ) {
136
133
LOGGER .error ("Dry run failed." , e );
137
134
throw e ;
@@ -140,8 +137,9 @@ public void importDataWithSeed(String targetVersion, File archive, ConfigPersist
140
137
// 3. Import Postgres content
141
138
importDatabaseFromArchive (sourceRoot , targetVersion );
142
139
143
- // 4. Import Configs
144
- importConfigsFromArchive (sourceRoot , seedPersistence , false );
140
+ // 4. Import Configs and update connector definitions
141
+ importConfigsFromArchive (sourceRoot , false );
142
+ configRepository .loadData (seedPersistence );
145
143
146
144
// 5. Set DB version
147
145
LOGGER .info ("Setting the DB Airbyte version to : " + targetVersion );
@@ -158,7 +156,7 @@ public void importDataWithSeed(String targetVersion, File archive, ConfigPersist
158
156
configRepository .listStandardWorkspaces (true ).forEach (workspace -> TrackingClientSingleton .get ().identify (workspace .getWorkspaceId ()));
159
157
}
160
158
161
- private void checkImport (String targetVersion , Path tempFolder ) throws IOException , JsonValidationException {
159
+ private void checkImport (String targetVersion , Path tempFolder ) throws IOException {
162
160
final Path versionFile = tempFolder .resolve (VERSION_FILE_NAME );
163
161
final String importVersion = Files .readString (versionFile , Charset .defaultCharset ())
164
162
.replace ("\n " , "" ).strip ();
@@ -179,21 +177,10 @@ private List<String> listDirectories(Path sourceRoot) throws IOException {
179
177
}
180
178
}
181
179
182
- private <T > void importConfigsFromArchive (final Path sourceRoot , ConfigPersistence seedPersistence , final boolean dryRun )
183
- throws IOException , JsonValidationException {
184
- final Set <String > sourceDefinitionsInUse = new HashSet <>();
185
- final Set <String > destinationDefinitionsInUse = new HashSet <>();
186
- final boolean [] sourceProcessed = {false };
187
- final boolean [] destinationProcessed = {false };
180
+ private void importConfigsFromArchive (final Path sourceRoot , final boolean dryRun ) throws IOException {
188
181
final List <String > directories = listDirectories (sourceRoot );
189
- // We sort the directories because we want to process SOURCE_CONNECTION before
190
- // STANDARD_SOURCE_DEFINITION and DESTINATION_CONNECTION before STANDARD_DESTINATION_DEFINITION
191
- // so that we can identify which definitions should not be upgraded to the latest version
192
- Collections .sort (directories );
193
182
final Map <AirbyteConfig , Stream <?>> data = new LinkedHashMap <>();
194
183
195
- final Map <ConfigSchema , Map <String , ?>> seeds = getSeeds (seedPersistence );
196
-
197
184
for (final String directory : directories ) {
198
185
final Optional <ConfigSchema > configSchemaOptional = Enums .toEnum (directory .replace (".yaml" , "" ), ConfigSchema .class );
199
186
@@ -202,90 +189,11 @@ private <T> void importConfigsFromArchive(final Path sourceRoot, ConfigPersisten
202
189
}
203
190
204
191
final ConfigSchema configSchema = configSchemaOptional .get ();
205
- Stream <?> configs = readConfigsFromArchive (sourceRoot , configSchema );
206
-
207
- // If there is no source or destination connection, mark them as processed respectively.
208
- if (configSchema == ConfigSchema .STANDARD_SOURCE_DEFINITION && !data .containsKey (ConfigSchema .SOURCE_CONNECTION )) {
209
- sourceProcessed [0 ] = true ;
210
- } else if (configSchema == ConfigSchema .STANDARD_DESTINATION_DEFINITION && !data .containsKey (ConfigSchema .DESTINATION_CONNECTION )) {
211
- destinationProcessed [0 ] = true ;
212
- }
213
-
214
- configs = streamWithAdditionalOperation (
215
- sourceDefinitionsInUse ,
216
- destinationDefinitionsInUse ,
217
- sourceProcessed ,
218
- destinationProcessed ,
219
- configSchema ,
220
- configs ,
221
- seeds );
222
- data .put (configSchema , configs );
192
+ data .put (configSchema , readConfigsFromArchive (sourceRoot , configSchema ));
223
193
}
224
194
configRepository .replaceAllConfigs (data , dryRun );
225
195
}
226
196
227
- /**
228
- * Convert config dumps from {@link ConfigPersistence#dumpConfigs} to the desired format.
229
- */
230
- @ SuppressWarnings ("unchecked" )
231
- private static Map <ConfigSchema , Map <String , ?>> getSeeds (ConfigPersistence configSeedPersistence ) throws IOException {
232
- Map <ConfigSchema , Map <String , ?>> allData = new HashMap <>(2 );
233
- for (Map .Entry <String , Stream <JsonNode >> configStream : configSeedPersistence .dumpConfigs ().entrySet ()) {
234
- ConfigSchema configSchema = ConfigSchema .valueOf (configStream .getKey ());
235
- Map <String , ?> configSeeds = configStream .getValue ()
236
- .map (node -> Jsons .object (node , configSchema .getClassName ()))
237
- .collect (Collectors .toMap (
238
- configSchema ::getId ,
239
- object -> object ));
240
- allData .put (configSchema , configSeeds );
241
- }
242
- return allData ;
243
- }
244
-
245
- private Stream <?> streamWithAdditionalOperation (Set <String > sourceDefinitionsInUse ,
246
- Set <String > destinationDefinitionsInUse ,
247
- boolean [] sourceProcessed ,
248
- boolean [] destinationProcessed ,
249
- ConfigSchema configSchema ,
250
- Stream <?> configs ,
251
- Map <ConfigSchema , Map <String , ?>> latestSeeds ) {
252
- if (configSchema == ConfigSchema .SOURCE_CONNECTION ) {
253
- sourceProcessed [0 ] = true ;
254
- configs = configs .peek (config -> sourceDefinitionsInUse .add (((SourceConnection ) config ).getSourceDefinitionId ().toString ()));
255
- } else if (configSchema == ConfigSchema .DESTINATION_CONNECTION ) {
256
- destinationProcessed [0 ] = true ;
257
- configs = configs .peek (config -> destinationDefinitionsInUse .add (((DestinationConnection ) config ).getDestinationDefinitionId ().toString ()));
258
- } else if (configSchema == ConfigSchema .STANDARD_SOURCE_DEFINITION ) {
259
- Map <String , ?> sourceDefinitionSeeds = latestSeeds .get (configSchema );
260
- configs = getDefinitionStream (sourceDefinitionsInUse , sourceProcessed [0 ], configSchema , configs , sourceDefinitionSeeds );
261
- } else if (configSchema == ConfigSchema .STANDARD_DESTINATION_DEFINITION ) {
262
- Map <String , ?> destinationDefinitionSeeds = latestSeeds .get (configSchema );
263
- configs = getDefinitionStream (destinationDefinitionsInUse , destinationProcessed [0 ], configSchema , configs , destinationDefinitionSeeds );
264
- }
265
- return configs ;
266
- }
267
-
268
- /**
269
- * This method combines the latest definitions with existing ones. If a connector is being used by
270
- * user, it will continue to be at the same version, otherwise it will be migrated to the latest
271
- * version
272
- */
273
- private Stream <?> getDefinitionStream (Set <String > definitionsInUse ,
274
- boolean definitionsPopulated ,
275
- ConfigSchema configSchema ,
276
- Stream <?> currentDefinitions ,
277
- Map <String , ?> latestDefinitions ) {
278
- if (!definitionsPopulated ) {
279
- throw new RuntimeException ("Trying to process " + configSchema + " without populating the definitions in use" );
280
- }
281
-
282
- return Streams .concat (
283
- // Keep all the definitions in use
284
- currentDefinitions .filter (c -> definitionsInUse .contains (configSchema .getId (c ))),
285
- // Upgrade all the definitions not in use
286
- latestDefinitions .entrySet ().stream ().filter (c -> !definitionsInUse .contains (c .getKey ())).map (Entry ::getValue ));
287
- }
288
-
289
197
private <T > Stream <T > readConfigsFromArchive (final Path storageRoot , final ConfigSchema schemaType )
290
198
throws IOException {
291
199
0 commit comments