diff --git a/packages/modules/mysql/src/mysql-container.test.ts b/packages/modules/mysql/src/mysql-container.test.ts index 3d2f91a0a..0c202a8ea 100644 --- a/packages/modules/mysql/src/mysql-container.test.ts +++ b/packages/modules/mysql/src/mysql-container.test.ts @@ -99,5 +99,19 @@ describe("MySqlContainer", () => { await container.stop(); }); + + it("should execute a query as root user", async () => { + const container = await new MySqlContainer().withUsername("customUsername").start(); + + // Test non-root user + const queryResult = await container.executeQuery("SELECT CURRENT_USER() as user"); + expect(queryResult).toEqual(expect.stringContaining("user\ncustomUsername")); + + // Test root user + const rootQueryResult = await container.executeQuery("SELECT CURRENT_USER() as user", [], true); + expect(rootQueryResult).toEqual(expect.stringContaining("user\nroot")); + + await container.stop(); + }); // } }); diff --git a/packages/modules/mysql/src/mysql-container.ts b/packages/modules/mysql/src/mysql-container.ts index d57a111c4..3e720aaf7 100644 --- a/packages/modules/mysql/src/mysql-container.ts +++ b/packages/modules/mysql/src/mysql-container.ts @@ -94,14 +94,14 @@ export class StartedMySqlContainer extends AbstractStartedContainer { return url.toString(); } - public async executeQuery(query: string, additionalFlags: string[] = []): Promise { + public async executeQuery(query: string, additionalFlags: string[] = [], isRoot = false): Promise { const result = await this.startedTestContainer.exec([ "mysql", "-h", "127.0.0.1", "-u", - this.username, - `-p${this.userPassword}`, + isRoot ? "root" : this.username, + `-p${isRoot ? this.getRootPassword() : this.getUserPassword()}`, "-e", `${query};`, ...additionalFlags,