Skip to content

Commit

Permalink
Merge pull request #131 from Sebastian-Webster/130-add-initsqlstring-…
Browse files Browse the repository at this point in the history
…option

Add initSQLString option
  • Loading branch information
Sebastian-Webster authored Oct 24, 2024
2 parents 55886ac + f19e19e commit deff88f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 4 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ Default: 3

Description: The number of times to try to download a MySQL binary before giving up and rejecting the `createDB()` promise.

- `initSQLString: string`

Required: No

Default: ""

Description: A string with MySQL queries to run before the database starts to accept connections. This option can be used for things like initialising tables without having to first connect to the database to do that.The queries in the string get executed after ```mysql-memory-server```'s queries run. Uses the ```--init-file``` MySQL server option under the hood. Learn more at the [--init-file MySQL Documentation](https://dev.mysql.com/doc/refman/8.4/en/server-system-variables.html#sysvar_init_file)

The internal queries that are ran before the queries in ```initSQLString``` are renaming the user from ```root``` to the username specified in the ```username``` option if it's set, and creating a database with the name from the ```dbName``` option.

***
### :warning: Internal Options :warning:

Expand Down
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export async function createDB(opts?: ServerOptions) {
port: 0,
xPort: 0,
binaryDirectoryPath: `${os.tmpdir()}/mysqlmsn/binaries`,
downloadRetries: 10
downloadRetries: 10,
initSQLString: ''
}

const options: InternalServerOptions = {...defaultOptions, ...opts}
Expand Down
4 changes: 4 additions & 0 deletions src/libraries/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,10 @@ class Executor {
initText += `\nRENAME USER 'root'@'localhost' TO '${options.username}'@'localhost';`
}

if (options.initSQLString.length > 0) {
initText += `\n${options.initSQLString}`
}

this.logger.log('Writing init file')

await fsPromises.writeFile(`${options.dbPath}/init.sql`, initText, {encoding: 'utf8'})
Expand Down
13 changes: 12 additions & 1 deletion tests/versions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,15 @@ for (const version of versions) {
for (const username of usernames) {
test(`running on version ${version} with username ${username}`, async () => {
Error.stackTraceLimit = Infinity
const options: ServerOptions = {version, dbName: 'testingdata', username: username, logLevel: 'LOG', deleteDBAfterStopped: !process.env.useCIDBPath, ignoreUnsupportedSystemVersion: true}
const options: ServerOptions = {
version,
dbName: 'testingdata',
username: username,
logLevel: 'LOG',
deleteDBAfterStopped: !process.env.useCIDBPath,
ignoreUnsupportedSystemVersion: true,
initSQLString: 'CREATE DATABASE mytestdb;'
}

if (process.env.useCIDBPath) {
options.dbPath = `${dbPath}/${randomUUID()}`
Expand All @@ -34,6 +42,9 @@ for (const version of versions) {
})

const mySQLVersion = (await connection.query('SELECT VERSION()'))[0][0]["VERSION()"]

//If this does not fail, it means initSQLString works as expected and the database was successfully created.
await connection.query('USE mytestdb;')

await connection.end();
await db.stop();
Expand Down
6 changes: 4 additions & 2 deletions types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ export type ServerOptions = {
port?: number | undefined,
xPort?: number | undefined,
binaryDirectoryPath?: string | undefined,
downloadRetries?: number | undefined
downloadRetries?: number | undefined,
initSQLString?: string | undefined
}

export type InternalServerOptions = {
Expand All @@ -35,7 +36,8 @@ export type InternalServerOptions = {
port: number,
xPort: number,
binaryDirectoryPath: string,
downloadRetries: number
downloadRetries: number,
initSQLString: string
}

export type ExecutorOptions = {
Expand Down

0 comments on commit deff88f

Please sign in to comment.