@@ -123,6 +123,86 @@ class MySQLControllerSpec extends AnyFreeSpec with DockerControllerSpecSupport {
123123
124124```
125125
126+ If you'd like to use ` flyway ` module, you can use ` docker-controller-scala-flyway ` .
127+
128+ ``` scala
129+ class MySQLControllerSpec extends AnyFreeSpec with DockerControllerSpecSupport with FlywaySpecSupport {
130+ val testTimeFactor : Int = sys.env.getOrElse(" TEST_TIME_FACTOR" , " 1" ).toInt
131+ logger.debug(s " testTimeFactor = $testTimeFactor" )
132+
133+ val hostPort : Int = RandomPortUtil .temporaryServerPort()
134+ val dbName : String . = " test"
135+ val rootUserName : String = " root"
136+ val rootPassword : String = " test"
137+
138+ override protected def flywayDriverClassName : String = classOf [com.mysql.cj.jdbc.Driver ].getName
139+ override protected def flywayDbHost : String = dockerHost
140+ override protected def flywayDbHostPort : Int = hostPort
141+ override protected def flywayDbName : String = dbName
142+ override protected def flywayDbUserName : String = rootUserName
143+ override protected def flywayDbPassword : String = rootPassword
144+
145+ override protected def flywayJDBCUrl : String =
146+ s " jdbc:mysql:// $flywayDbHost: $flywayDbHostPort/ $flywayDbName?useSSL=false&user= $flywayDbUserName&password= $flywayDbPassword"
147+
148+ val controller : MySQLController = MySQLController (dockerClient)(hostPort, rootPassword, databaseName = Some (dbName))
149+
150+ override protected val dockerControllers : Vector [DockerController ] = Vector (controller)
151+
152+ override protected val waitPredicatesSettings : Map [DockerController , WaitPredicateSetting ] =
153+ Map (
154+ controller -> WaitPredicateSetting (
155+ Duration .Inf ,
156+ WaitPredicates .forListeningHostTcpPort(
157+ dockerHost,
158+ hostPort,
159+ (1 * testTimeFactor).seconds,
160+ Some ((5 * testTimeFactor).seconds)
161+ )
162+ )
163+ )
164+
165+ override protected def afterStartContainers (): Unit = {
166+ val flywayContext = createFlywayContext(FlywayConfig (Seq (" flyway" )))
167+ // Execute flywayMigrate command
168+ flywayContext.flyway.migrate()
169+ }
170+
171+ " MySQLController" - {
172+ " run" in {
173+ var conn : Connection = null
174+ var stmt : Statement = null
175+ var resultSet : ResultSet = null
176+ try {
177+ Class .forName(flywayDriverClassName)
178+ conn = DriverManager .getConnection(flywayJDBCUrl)
179+ stmt = conn.createStatement
180+ val result = stmt.executeUpdate(" INSERT INTO users VALUES(1, 'kato')" )
181+ assert(result == 1 )
182+ resultSet = stmt.executeQuery(" SELECT * FROM users" )
183+ while (resultSet.next()) {
184+ val id = resultSet.getInt(" id" )
185+ val name = resultSet.getString(" name" )
186+ println(s " id = $id, name = $name" )
187+ }
188+ } catch {
189+ case NonFatal (ex) =>
190+ ex.printStackTrace()
191+ fail(" occurred error" , ex)
192+ } finally {
193+ if (resultSet != null )
194+ resultSet.close()
195+ if (stmt != null )
196+ stmt.close()
197+ if (conn != null )
198+ conn.close()
199+ }
200+ }
201+ }
202+
203+ }
204+ ```
205+
126206### How to test with DockerController your customized
127207
128208To launch a docker container for testing
0 commit comments