Skip to content

Commit

Permalink
MySQL add isRoot parameter to executeQuery (#882)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeengbe authored Jan 11, 2025
1 parent 12caddb commit fa6edeb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
14 changes: 14 additions & 0 deletions packages/modules/mysql/src/mysql-container.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});
// }
});
6 changes: 3 additions & 3 deletions packages/modules/mysql/src/mysql-container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,14 +94,14 @@ export class StartedMySqlContainer extends AbstractStartedContainer {
return url.toString();
}

public async executeQuery(query: string, additionalFlags: string[] = []): Promise<string> {
public async executeQuery(query: string, additionalFlags: string[] = [], isRoot = false): Promise<string> {
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,
Expand Down

0 comments on commit fa6edeb

Please sign in to comment.