11
11
import java .io .IOException ;
12
12
import java .io .UncheckedIOException ;
13
13
import java .sql .SQLException ;
14
+ import java .util .Collections ;
14
15
import java .util .HashMap ;
15
16
import java .util .List ;
16
17
import java .util .Map ;
17
18
import java .util .stream .Collectors ;
18
19
import java .util .stream .Stream ;
20
+ import org .apache .commons .lang3 .StringUtils ;
19
21
import org .jooq .SQLDialect ;
20
22
import org .slf4j .Logger ;
21
23
import org .slf4j .LoggerFactory ;
@@ -68,6 +70,7 @@ static public MsSQLTestDatabase in(final BaseImage imageName, final ContainerMod
68
70
69
71
public MsSQLTestDatabase (final MSSQLServerContainer <?> container ) {
70
72
super (container );
73
+ LOGGER .info ("SGX creating new database. databaseId=" + this .databaseId + ", databaseName=" + getDatabaseName ());
71
74
}
72
75
73
76
public MsSQLTestDatabase withCdc () {
@@ -103,39 +106,39 @@ public MsSQLTestDatabase withShortenedCapturePollingInterval() {
103
106
104
107
private void waitForAgentState (final boolean running ) {
105
108
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 );
107
110
for (int i = 0 ; i < MAX_RETRIES ; i ++) {
108
111
try {
109
112
final var r = query (ctx -> ctx .fetch ("EXEC master.dbo.xp_servicecontrol 'QueryState', N'SQLServerAGENT';" ).get (0 ));
110
113
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 );
112
115
return ;
113
116
}
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 );
115
118
} 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 ());
117
120
}
118
121
try {
119
122
Thread .sleep (1_000 ); // Wait one second between retries.
120
123
} catch (final InterruptedException e ) {
121
124
throw new RuntimeException (e );
122
125
}
123
126
}
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" ) );
125
128
}
126
129
127
130
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 ());
129
132
for (int i = 0 ; i < MAX_RETRIES ; i ++) {
130
133
try {
131
134
final var maxLSN = query (ctx -> ctx .fetch ("SELECT sys.fn_cdc_get_max_lsn();" ).get (0 ).get (0 , byte [].class ));
132
135
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 ));
134
137
return self ();
135
138
}
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 ());
137
140
} 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 ());
139
142
}
140
143
try {
141
144
Thread .sleep (1_000 ); // Wait one second between retries.
@@ -157,10 +160,10 @@ public String getJdbcUrl() {
157
160
}
158
161
159
162
@ 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 (
164
167
String .format ("USE %s" , getDatabaseName ()),
165
168
String .format ("CREATE LOGIN %s WITH PASSWORD = '%s', DEFAULT_DATABASE = %s" , getUserName (), getPassword (), getDatabaseName ()),
166
169
String .format ("ALTER SERVER ROLE [sysadmin] ADD MEMBER %s" , getUserName ()),
@@ -174,22 +177,22 @@ protected Stream<Stream<String>> inContainerBootstrapCmd() {
174
177
* aren't really worth it.
175
178
*/
176
179
@ Override
177
- protected Stream <String > inContainerUndoBootstrapCmd () {
178
- return Stream . empty ();
180
+ protected List <String > inContainerUndoBootstrapCmd () {
181
+ return Collections . emptyList ();
179
182
}
180
183
181
184
public void dropDatabaseAndUser () {
182
- execInContainer (mssqlCmd (Stream .of (
185
+ execInContainer (mssqlCmd (List .of (
183
186
String .format ("USE master" ),
184
187
String .format ("ALTER DATABASE %s SET single_user WITH ROLLBACK IMMEDIATE" , getDatabaseName ()),
185
188
String .format ("DROP DATABASE %s" , getDatabaseName ()))));
186
189
}
187
190
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" ,
190
193
"-U" , getContainer ().getUsername (),
191
194
"-P" , getContainer ().getPassword (),
192
- "-Q" , sql . collect ( Collectors . joining ( "; " ) ),
195
+ "-Q" , StringUtils . join ( "; " ),
193
196
"-b" , "-e" );
194
197
}
195
198
0 commit comments