Skip to content

Commit d21b594

Browse files
improve logging in MsSQLTestDatabase
1 parent 2d0c49f commit d21b594

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

airbyte-integrations/connectors/source-mssql/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ plugins {
55
airbyteJavaConnector {
66
cdkVersionRequired = '0.19.0'
77
features = ['db-sources']
8-
useLocalCdk = false
8+
useLocalCdk = true
99
}
1010

1111
java {

airbyte-integrations/connectors/source-mssql/src/testFixtures/java/io/airbyte/integrations/source/mssql/MsSQLTestDatabase.java

+22-19
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,13 @@
1111
import java.io.IOException;
1212
import java.io.UncheckedIOException;
1313
import java.sql.SQLException;
14+
import java.util.Collections;
1415
import java.util.HashMap;
1516
import java.util.List;
1617
import java.util.Map;
1718
import java.util.stream.Collectors;
1819
import java.util.stream.Stream;
20+
import org.apache.commons.lang3.StringUtils;
1921
import org.jooq.SQLDialect;
2022
import org.slf4j.Logger;
2123
import org.slf4j.LoggerFactory;
@@ -68,6 +70,7 @@ static public MsSQLTestDatabase in(final BaseImage imageName, final ContainerMod
6870

6971
public MsSQLTestDatabase(final MSSQLServerContainer<?> container) {
7072
super(container);
73+
LOGGER.info("SGX creating new database. databaseId=" + this.databaseId + ", databaseName=" + getDatabaseName());
7174
}
7275

7376
public MsSQLTestDatabase withCdc() {
@@ -103,39 +106,39 @@ public MsSQLTestDatabase withShortenedCapturePollingInterval() {
103106

104107
private void waitForAgentState(final boolean running) {
105108
final String expectedValue = running ? "Running." : "Stopped.";
106-
LOGGER.debug("Waiting for SQLServerAgent state to change to '{}'.", expectedValue);
109+
LOGGER.info(formatLogLine("Waiting for SQLServerAgent state to change to '{}'."), expectedValue);
107110
for (int i = 0; i < MAX_RETRIES; i++) {
108111
try {
109112
final var r = query(ctx -> ctx.fetch("EXEC master.dbo.xp_servicecontrol 'QueryState', N'SQLServerAGENT';").get(0));
110113
if (expectedValue.equalsIgnoreCase(r.getValue(0).toString())) {
111-
LOGGER.debug("SQLServerAgent state is '{}', as expected.", expectedValue);
114+
LOGGER.info(formatLogLine("SQLServerAgent state is '{}', as expected."), expectedValue);
112115
return;
113116
}
114-
LOGGER.debug("Retrying, SQLServerAgent state {} does not match expected '{}'.", r, expectedValue);
117+
LOGGER.info(formatLogLine("Retrying, SQLServerAgent state {} does not match expected '{}'."), r, expectedValue);
115118
} catch (final SQLException e) {
116-
LOGGER.debug("Retrying agent state query after catching exception {}.", e.getMessage());
119+
LOGGER.info(formatLogLine("Retrying agent state query after catching exception {}."), e.getMessage());
117120
}
118121
try {
119122
Thread.sleep(1_000); // Wait one second between retries.
120123
} catch (final InterruptedException e) {
121124
throw new RuntimeException(e);
122125
}
123126
}
124-
throw new RuntimeException("Exhausted retry attempts while polling for agent state");
127+
throw new RuntimeException(formatLogLine("Exhausted retry attempts while polling for agent state"));
125128
}
126129

127130
public MsSQLTestDatabase withWaitUntilMaxLsnAvailable() {
128-
LOGGER.debug("Waiting for max LSN to become available for database {}.", getDatabaseName());
131+
LOGGER.info(formatLogLine("Waiting for max LSN to become available for database {}."), getDatabaseName());
129132
for (int i = 0; i < MAX_RETRIES; i++) {
130133
try {
131134
final var maxLSN = query(ctx -> ctx.fetch("SELECT sys.fn_cdc_get_max_lsn();").get(0).get(0, byte[].class));
132135
if (maxLSN != null) {
133-
LOGGER.debug("Max LSN available for database {}: {}", getDatabaseName(), Lsn.valueOf(maxLSN));
136+
LOGGER.info(formatLogLine("Max LSN available for database {}: {}"), getDatabaseName(), Lsn.valueOf(maxLSN));
134137
return self();
135138
}
136-
LOGGER.debug("Retrying, max LSN still not available for database {}.", getDatabaseName());
139+
LOGGER.info(formatLogLine("Retrying, max LSN still not available for database {}."), getDatabaseName());
137140
} catch (final SQLException e) {
138-
LOGGER.warn("Retrying max LSN query after catching exception {}", e.getMessage());
141+
LOGGER.info(formatLogLine("Retrying max LSN query after catching exception {}"), e.getMessage());
139142
}
140143
try {
141144
Thread.sleep(1_000); // Wait one second between retries.
@@ -157,10 +160,10 @@ public String getJdbcUrl() {
157160
}
158161

159162
@Override
160-
protected Stream<Stream<String>> inContainerBootstrapCmd() {
161-
return Stream.of(
162-
mssqlCmd(Stream.of(String.format("CREATE DATABASE %s", getDatabaseName()))),
163-
mssqlCmd(Stream.of(
163+
protected List<List<String>> inContainerBootstrapCmd() {
164+
return List.of(
165+
mssqlCmd(List.of(String.format("CREATE DATABASE %s", getDatabaseName()))),
166+
mssqlCmd(List.of(
164167
String.format("USE %s", getDatabaseName()),
165168
String.format("CREATE LOGIN %s WITH PASSWORD = '%s', DEFAULT_DATABASE = %s", getUserName(), getPassword(), getDatabaseName()),
166169
String.format("ALTER SERVER ROLE [sysadmin] ADD MEMBER %s", getUserName()),
@@ -174,22 +177,22 @@ protected Stream<Stream<String>> inContainerBootstrapCmd() {
174177
* aren't really worth it.
175178
*/
176179
@Override
177-
protected Stream<String> inContainerUndoBootstrapCmd() {
178-
return Stream.empty();
180+
protected List<String> inContainerUndoBootstrapCmd() {
181+
return Collections.emptyList();
179182
}
180183

181184
public void dropDatabaseAndUser() {
182-
execInContainer(mssqlCmd(Stream.of(
185+
execInContainer(mssqlCmd(List.of(
183186
String.format("USE master"),
184187
String.format("ALTER DATABASE %s SET single_user WITH ROLLBACK IMMEDIATE", getDatabaseName()),
185188
String.format("DROP DATABASE %s", getDatabaseName()))));
186189
}
187190

188-
public Stream<String> mssqlCmd(final Stream<String> sql) {
189-
return Stream.of("/opt/mssql-tools/bin/sqlcmd",
191+
public List<String> mssqlCmd(final List<String> sql) {
192+
return List.of("/opt/mssql-tools/bin/sqlcmd",
190193
"-U", getContainer().getUsername(),
191194
"-P", getContainer().getPassword(),
192-
"-Q", sql.collect(Collectors.joining("; ")),
195+
"-Q", StringUtils.join("; "),
193196
"-b", "-e");
194197
}
195198

0 commit comments

Comments
 (0)