Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix StatePersistence Legacy read/write #14129

Merged
merged 3 commits into from
Jun 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.fasterxml.jackson.databind.JsonNode;
import io.airbyte.commons.enums.Enums;
import io.airbyte.commons.json.Jsons;
import io.airbyte.config.State;
import io.airbyte.config.StateType;
import io.airbyte.config.StateWrapper;
import io.airbyte.db.Database;
Expand Down Expand Up @@ -158,7 +159,9 @@ static void writeStateToDb(final DSLContext ctx,
isNullOrEquals(STATE.NAMESPACE, namespace))
.fetch().isNotEmpty();

final JSONB jsonbState = JSONB.valueOf(Jsons.serialize(state));
// NOTE: the legacy code was storing a State object instead of just the State data field. We kept
// the same behavior for consistency.
final JSONB jsonbState = JSONB.valueOf(Jsons.serialize(stateType != StateType.LEGACY ? state : new State().withState(state)));
final OffsetDateTime now = OffsetDateTime.now();

if (!hasState) {
Expand Down Expand Up @@ -292,9 +295,10 @@ record -> new AirbyteStateMessage()
* Build a StateWrapper for Legacy state
*/
private static StateWrapper buildLegacyState(final List<StateRecord> records) {
final State legacyState = Jsons.convertValue(records.get(0).state, State.class);
return new StateWrapper()
.withStateType(StateType.LEGACY)
.withLegacyState(records.get(0).state);
.withLegacyState(legacyState.getState());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import io.airbyte.config.StandardSourceDefinition;
import io.airbyte.config.StandardSync;
import io.airbyte.config.StandardWorkspace;
import io.airbyte.config.State;
import io.airbyte.config.StateType;
import io.airbyte.config.StateWrapper;
import io.airbyte.config.persistence.split_secrets.JsonSecretsProcessor;
Expand Down Expand Up @@ -45,6 +46,7 @@

public class StatePersistenceTest extends BaseDatabaseConfigPersistenceTest {

private ConfigRepository configRepository;
private StatePersistence statePersistence;
private UUID connectionId;

Expand Down Expand Up @@ -489,6 +491,27 @@ public void testEnumsConversion() {
io.airbyte.config.StateType.class));
}

@Test
public void testStatePersistenceLegacyReadConsistency() throws IOException {
final JsonNode jsonState = Jsons.deserialize("{\"my\": \"state\"}");
final State state = new State().withState(jsonState);
configRepository.updateConnectionState(connectionId, state);

final StateWrapper readStateWrapper = statePersistence.getCurrentState(connectionId).orElseThrow();
Assertions.assertEquals(StateType.LEGACY, readStateWrapper.getStateType());
Assertions.assertEquals(state.getState(), readStateWrapper.getLegacyState());
}

@Test
public void testStatePersistenceLegacyWriteConsistency() throws IOException {
final JsonNode jsonState = Jsons.deserialize("{\"my\": \"state\"}");
final StateWrapper stateWrapper = new StateWrapper().withStateType(StateType.LEGACY).withLegacyState(jsonState);
statePersistence.updateOrCreateState(connectionId, stateWrapper);

final State readState = configRepository.getConnectionState(connectionId).orElseThrow();
Assertions.assertEquals(readState.getState(), stateWrapper.getLegacyState());
}

@BeforeEach
public void beforeEach() throws DatabaseInitializationException, IOException, JsonValidationException {
dataSource = DatabaseConnectionHelper.createDataSource(container);
Expand All @@ -510,7 +533,7 @@ public void afterEach() {
}

private void setupTestData() throws JsonValidationException, IOException {
ConfigRepository configRepository = new ConfigRepository(
configRepository = new ConfigRepository(
new DatabaseConfigPersistence(database, mock(JsonSecretsProcessor.class)),
database);

Expand Down