Skip to content

Commit

Permalink
Expose whether MySQL version used for db is installed on the system o…
Browse files Browse the repository at this point in the history
…r was downloaded (#163)

* add installedOnSystem boolean to DownloadedMySQLVersion type

* resolve with mysql info object

* add documentation

* add message in cli for whether the MySQL version is already installed or not

* update message

* update message
  • Loading branch information
Sebastian-Webster authored Dec 3, 2024
1 parent ea81430 commit e48f40c
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 12 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ The name of the user to use to login to the database
If on Windows, this is the name of the named pipe that MySQL is listening on. If not on Windows, this is the path to the socket that MySQL is listening on.
- `xSocket: string`
If on Windows, this is the name of the named pipe that the MySQL X Plugin is listening on. If not on Windows, this is the path that the MySQL X Plugin is listening on.
- `mysql: {version: string, versionIsInstalledOnSystem: boolean}`
An object with two properties. The first one, version, is the version of MySQL Server that is being used for the database. The second one, versionIsInstalledOnSystem, will be true if the MySQL Server that is being used is already installed on the system. versionIsInstalledOnSystem will be false if the MySQL Server version had to be downloaded from the MySQL CDN.
- `stop: () => Promise<void>`
The method to stop the database. The returned promise resolves when the database has successfully stopped.

Expand Down
2 changes: 1 addition & 1 deletion src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ async function main() {
}
console.log('Creating ephemeral MySQL database...')
const db = await createDB(options);
console.log(`A MySQL database has been successfully created with the following parameters:\n\nMySQL Version: ${db.version} \nUsername: ${db.username} \nDatabase Name: ${db.dbName} \nPort: ${db.port} \nX Plugin Port: ${db.xPort} \nSocket: ${db.socket} \nX Plugin Socket: ${db.xSocket}\n`)
console.log(`A MySQL database has been successfully created with the following parameters:\n\nMySQL Version: ${db.mysql.version} (${db.mysql.versionIsInstalledOnSystem ? 'installed on this system' : 'not installed on this system - downloaded from the MySQL CDN'}) \nUsername: ${db.username} \nDatabase Name: ${db.dbName} \nPort: ${db.port} \nX Plugin Port: ${db.xPort} \nSocket: ${db.socket} \nX Plugin Socket: ${db.xSocket}\n`)
if (process.platform === 'win32') {
//The connection information logs will be different for Windows compared to other platforms.
//Windows uses mysqlsh instead of mysql to invoke the client shell, needs a --sql flag to be put into SQL mode, and also does not have a protocol flag.
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export async function createDB(opts?: ServerOptions) {
}

logger.log('Running downloaded binary')
return await executor.startMySQL(options, {path: binaryFilepath, version: binaryInfo.version})
return await executor.startMySQL(options, {path: binaryFilepath, version: binaryInfo.version, installedOnSystem: false})
} else {
logger.log(version)
return await executor.startMySQL(options, version)
Expand Down
19 changes: 12 additions & 7 deletions src/libraries/Executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import * as fsPromises from 'fs/promises';
import * as fs from 'fs';
import Logger from "./Logger";
import { GenerateRandomPort } from "./Port";
import { ExecuteFileReturn, InstalledMySQLVersion, InternalServerOptions, MySQLDB } from "../../types";
import { ExecuteFileReturn, DownloadedMySQLVersion, InternalServerOptions, MySQLDB } from "../../types";
import {normalize as normalizePath, resolve as resolvePath} from 'path'
import { lockFile, waitForLock } from "./FileLock";
import { onExit } from "signal-exit";
Expand All @@ -16,6 +16,7 @@ class Executor {
DBDestroySignal = new AbortController();
removeExitHandler: () => void;
version: string;
versionInstalledOnSystem: boolean;

constructor(logger: Logger) {
this.logger = logger;
Expand Down Expand Up @@ -191,7 +192,10 @@ class Executor {
xSocket,
dbName: options.dbName,
username: options.username,
version: this.version,
mysql: {
version: this.version,
versionIsInstalledOnSystem: this.versionInstalledOnSystem
},
stop: () => {
return new Promise(async (resolve, reject) => {
resolveFunction = resolve;
Expand All @@ -212,7 +216,7 @@ class Executor {
})
}

getMySQLVersion(preferredVersion?: string): Promise<InstalledMySQLVersion | null> {
getMySQLVersion(preferredVersion?: string): Promise<DownloadedMySQLVersion | null> {
return new Promise(async (resolve, reject) => {
if (process.platform === 'win32') {
try {
Expand All @@ -225,7 +229,7 @@ class Executor {

this.logger.log(servers)

const versions: {version: string, path: string}[] = []
const versions: DownloadedMySQLVersion[] = []

for (const dir of servers) {
const path = `${process.env.PROGRAMFILES}\\MySQL\\${dir}\\bin\\mysqld`
Expand All @@ -241,7 +245,7 @@ class Executor {
if (version === null) {
return reject('Could not get MySQL version')
} else {
versions.push({version: version.version, path})
versions.push({version: version.version, path, installedOnSystem: true})
}
}

Expand All @@ -266,7 +270,7 @@ class Executor {
if (version === null) {
reject('Could not get installed MySQL version')
} else {
resolve({version: version.version, path: 'mysqld'})
resolve({version: version.version, path: 'mysqld', installedOnSystem: true})
}
}
}
Expand Down Expand Up @@ -422,8 +426,9 @@ class Executor {
this.logger.log('Finished writing init file')
}

async startMySQL(options: InternalServerOptions, installedMySQLBinary: InstalledMySQLVersion): Promise<MySQLDB> {
async startMySQL(options: InternalServerOptions, installedMySQLBinary: DownloadedMySQLVersion): Promise<MySQLDB> {
this.version = installedMySQLBinary.version
this.versionInstalledOnSystem = installedMySQLBinary.installedOnSystem
this.removeExitHandler = onExit(() => {
if (options._DO_NOT_USE_cli) {
console.log('\nShutting down the ephemeral MySQL database and cleaning all related files...')
Expand Down
10 changes: 7 additions & 3 deletions types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,10 @@ export type MySQLDB = {
xSocket: string,
dbName: string,
username: string,
version: string,
mysql: {
version: string,
versionIsInstalledOnSystem: boolean
},
stop: () => Promise<void>
}

Expand All @@ -73,9 +76,10 @@ export type MySQLVersion = {
url: string
}

export type InstalledMySQLVersion = {
export type DownloadedMySQLVersion = {
version: string,
path: string
path: string,
installedOnSystem: boolean
}

export type BinaryInfo = {
Expand Down

0 comments on commit e48f40c

Please sign in to comment.