Skip to content

Commit 054afd6

Browse files
xiaohansongjatinyadav-cc
authored andcommitted
[source-postgres] Add test for legacy version of postgres (airbytehq#35329)
1 parent e398525 commit 054afd6

File tree

3 files changed

+64
-14
lines changed

3 files changed

+64
-14
lines changed

airbyte-integrations/connectors/source-postgres/src/test/java/io/airbyte/integrations/source/postgres/CdcPostgresSourceLegacyCtidTest.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,28 @@
44

55
package io.airbyte.integrations.source.postgres;
66

7+
import io.airbyte.integrations.source.postgres.PostgresTestDatabase.BaseImage;
8+
import org.junit.jupiter.api.Disabled;
79
import org.junit.jupiter.api.Order;
810

911
@Order(2)
1012
public class CdcPostgresSourceLegacyCtidTest extends CdcPostgresSourceTest {
1113

12-
protected static String getServerImageName() {
13-
return "debezium/postgres:13-bullseye";
14+
@Override
15+
protected void setBaseImage() {
16+
this.postgresImage = BaseImage.POSTGRES_12;
17+
}
18+
19+
@Override
20+
@Disabled("https://github.com/airbytehq/airbyte/issues/35267")
21+
public void newTableSnapshotTest() {
22+
23+
}
24+
25+
@Override
26+
@Disabled("https://github.com/airbytehq/airbyte/issues/35267")
27+
public void syncShouldIncrementLSN() {
28+
1429
}
1530

1631
}

airbyte-integrations/connectors/source-postgres/src/test/java/io/airbyte/integrations/source/postgres/CdcPostgresSourceTest.java

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,16 @@
7373
@Order(1)
7474
public class CdcPostgresSourceTest extends CdcSourceTest<PostgresSource, PostgresTestDatabase> {
7575

76+
protected BaseImage postgresImage;
77+
78+
protected void setBaseImage() {
79+
this.postgresImage = getServerImage();
80+
}
81+
7682
@Override
7783
protected PostgresTestDatabase createTestDatabase() {
78-
return PostgresTestDatabase.in(getServerImage(), ContainerModifier.CONF).withReplicationSlot();
84+
setBaseImage();
85+
return PostgresTestDatabase.in(this.postgresImage, ContainerModifier.CONF).withReplicationSlot();
7986
}
8087

8188
@Override
@@ -101,6 +108,15 @@ protected void setup() {
101108
testdb.withPublicationForAllTables();
102109
}
103110

111+
// For legacy Postgres we will call advanceLsn() after we retrieved target LSN, so that debezium
112+
// would not drop any record.
113+
// However, that might cause unexpected state and cause failure in the test. Thus we need to bypass
114+
// some check if they are on legacy postgres
115+
// versions.
116+
private boolean isOnLegacyPostgres() {
117+
return postgresImage.majorVersion < 15;
118+
}
119+
104120
@Test
105121
void testDebugMode() {
106122
final JsonNode invalidDebugConfig = testdb.testConfigBuilder()
@@ -196,7 +212,12 @@ private void assertStateTypes(final List<AirbyteStateMessage> stateMessages, fin
196212
if (Objects.isNull(sharedState)) {
197213
sharedState = global.getSharedState();
198214
} else {
199-
assertEquals(sharedState, global.getSharedState());
215+
// This validation is only true for versions on or after postgres 15. We execute
216+
// EPHEMERAL_HEARTBEAT_CREATE_STATEMENTS for earlier versions of
217+
// Postgres. See https://github.com/airbytehq/airbyte/pull/33605 for details.
218+
if (!isOnLegacyPostgres()) {
219+
assertEquals(sharedState, global.getSharedState());
220+
}
200221
}
201222
assertEquals(1, global.getStreamStates().size());
202223
final AirbyteStreamState streamState = global.getStreamStates().get(0);
@@ -324,7 +345,11 @@ public void testTwoStreamSync() throws Exception {
324345
if (Objects.isNull(sharedState)) {
325346
sharedState = global.getSharedState();
326347
} else {
327-
assertEquals(sharedState, global.getSharedState());
348+
// LSN will be advanced for postgres version before 15. See
349+
// https://github.com/airbytehq/airbyte/pull/33605
350+
if (!isOnLegacyPostgres()) {
351+
assertEquals(sharedState, global.getSharedState());
352+
}
328353
}
329354

330355
if (Objects.isNull(firstStreamInState)) {
@@ -755,7 +780,11 @@ protected void assertLsnPositionForSyncShouldIncrementLSN(final Long lsnPosition
755780
if (syncNumber == 1) {
756781
assertEquals(1, lsnPosition2.compareTo(lsnPosition1));
757782
} else if (syncNumber == 2) {
758-
assertEquals(0, lsnPosition2.compareTo(lsnPosition1));
783+
// Earlier Postgres version will advance lsn even if there is no sync records. See
784+
// https://github.com/airbytehq/airbyte/pull/33605.
785+
if (!isOnLegacyPostgres()) {
786+
assertEquals(0, lsnPosition2.compareTo(lsnPosition1));
787+
}
759788
} else {
760789
throw new RuntimeException("Unknown sync number " + syncNumber);
761790
}
@@ -791,7 +820,9 @@ protected void verifyCheckpointStatesByRecords() throws Exception {
791820
.toListAndClose(secondBatchIterator);
792821
assertEquals(recordsToCreate, extractRecordMessages(dataFromSecondBatch).size());
793822
final List<AirbyteStateMessage> stateMessagesCDC = extractStateMessages(dataFromSecondBatch);
794-
assertTrue(stateMessagesCDC.size() > 1, "Generated only the final state.");
823+
if (!isOnLegacyPostgres()) {
824+
assertTrue(stateMessagesCDC.size() > 1, "Generated only the final state.");
825+
}
795826
assertEquals(stateMessagesCDC.size(), stateMessagesCDC.stream().distinct().count(), "There are duplicated states.");
796827
}
797828

@@ -830,7 +861,9 @@ protected void verifyCheckpointStatesBySeconds() throws Exception {
830861

831862
assertEquals(recordsToCreate, extractRecordMessages(dataFromSecondBatch).size());
832863
final List<AirbyteStateMessage> stateMessagesCDC = extractStateMessages(dataFromSecondBatch);
833-
assertTrue(stateMessagesCDC.size() > 1, "Generated only the final state.");
864+
if (!isOnLegacyPostgres()) {
865+
assertTrue(stateMessagesCDC.size() > 1, "Generated only the final state.");
866+
}
834867
assertEquals(stateMessagesCDC.size(), stateMessagesCDC.stream().distinct().count(), "There are duplicated states.");
835868
}
836869

airbyte-integrations/connectors/source-postgres/src/testFixtures/java/io/airbyte/integrations/source/postgres/PostgresTestDatabase.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,17 @@ public class PostgresTestDatabase extends
2121

2222
public static enum BaseImage {
2323

24-
POSTGRES_16("postgres:16-bullseye"),
25-
POSTGRES_12("postgres:12-bullseye"),
26-
POSTGRES_9("postgres:9-alpine"),
27-
POSTGRES_SSL_DEV("marcosmarxm/postgres-ssl:dev");
24+
POSTGRES_16("postgres:16-bullseye", 16),
25+
POSTGRES_12("postgres:12-bullseye", 12),
26+
POSTGRES_9("postgres:9-alpine", 9),
27+
POSTGRES_SSL_DEV("marcosmarxm/postgres-ssl:dev", 16);
2828

29-
private final String reference;
29+
public final String reference;
30+
public final int majorVersion;
3031

31-
private BaseImage(String reference) {
32+
private BaseImage(String reference, int majorVersion) {
3233
this.reference = reference;
34+
this.majorVersion = majorVersion;
3335
};
3436

3537
}

0 commit comments

Comments
 (0)