import { GenericContainer } from "../../generic-container/generic-container" ; import { StartedTestContainer } from "../../test-container" ; import { RandomUuid } from "../../uuid" ; import { AbstractStartedContainer } from "../abstract-started-container" ; import { Port } from "../../port" ; export class MySqlContainer extends GenericContainer { private database = "test" ; private username = new RandomUuid ( ) . nextUuid ( ) ; private userPassword = new RandomUuid ( ) . nextUuid ( ) ; private rootPassword = new RandomUuid ( ) . nextUuid ( ) ; constructor ( image = "mysql:8.0.26" ) { super ( image ) ; } public withDatabase ( database ) { this . database = database ; return this ; } public withUsername ( username ) { this . username = username ; return this ; } public withRootPassword ( rootPassword ) { this . rootPassword = rootPassword ; return this ; } public withUserPassword ( userPassword ) { this . userPassword = userPassword ; return this ; } public async start ( ) { this . withExposedPorts ( 3306 ) . withEnv ( "MYSQL_DATABASE" , this . database ) . withEnv ( "MYSQL_ROOT_PASSWORD" , this . rootPassword ) . withEnv ( "MYSQL_USER" , this . username ) . withEnv ( "MYSQL_PASSWORD" , this . userPassword ) . withStartupTimeout ( 120000 ) ; return new StartedMySqlContainer ( await super . start ( ) , this . database , this . username , this . userPassword , this . rootPassword ) ; } } export class StartedMySqlContainer extends AbstractStartedContainer { private readonly port : Port ; constructor ( startedTestContainer , private readonly database , private readonly username , private readonly userPassword , private readonly rootPassword ) { super ( startedTestContainer ) ; this . port = startedTestContainer . getMappedPort ( 3306 ) ; } public getPort ( ) { return this . port ; } public getDatabase ( ) { return this . database ; } public getUsername ( ) { return this . username ; } public getUserPassword ( ) { return this . userPassword ; } public getRootPassword ( ) { return this . rootPassword ; } public async executeQuery ( query , additionalFlags = [ ] ) { const result = await this . startedTestContainer . exec ( [ "mysql" , "-u" , this . username , `-p${ this . userPassword }` , "-e" , `${ query };` , ... additionalFlags , ] ) ; if ( result . exitCode !== 0 ) { throw new Error ( `executeQuery failed with exit code ${ result . exitCode } for query: ${ query }` ) ; } return result . output ; } }