From e48f40cda23e791de6e81326ba83d15f15c5db34 Mon Sep 17 00:00:00 2001 From: Sebastian Webster <84299475+Sebastian-Webster@users.noreply.github.com> Date: Wed, 4 Dec 2024 04:38:59 +1300 Subject: [PATCH] Expose whether MySQL version used for db is installed on the system or 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 --- README.md | 2 ++ src/cli.ts | 2 +- src/index.ts | 2 +- src/libraries/Executor.ts | 19 ++++++++++++------- types/index.ts | 10 +++++++--- 5 files changed, 23 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index fb109ed0..9c86b08c 100644 --- a/README.md +++ b/README.md @@ -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` The method to stop the database. The returned promise resolves when the database has successfully stopped. diff --git a/src/cli.ts b/src/cli.ts index 510263ae..3e807cf3 100644 --- a/src/cli.ts +++ b/src/cli.ts @@ -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. diff --git a/src/index.ts b/src/index.ts index 5811c780..4ecf4c8f 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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) diff --git a/src/libraries/Executor.ts b/src/libraries/Executor.ts index 95d42946..21a148f3 100644 --- a/src/libraries/Executor.ts +++ b/src/libraries/Executor.ts @@ -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"; @@ -16,6 +16,7 @@ class Executor { DBDestroySignal = new AbortController(); removeExitHandler: () => void; version: string; + versionInstalledOnSystem: boolean; constructor(logger: Logger) { this.logger = logger; @@ -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; @@ -212,7 +216,7 @@ class Executor { }) } - getMySQLVersion(preferredVersion?: string): Promise { + getMySQLVersion(preferredVersion?: string): Promise { return new Promise(async (resolve, reject) => { if (process.platform === 'win32') { try { @@ -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` @@ -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}) } } @@ -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}) } } } @@ -422,8 +426,9 @@ class Executor { this.logger.log('Finished writing init file') } - async startMySQL(options: InternalServerOptions, installedMySQLBinary: InstalledMySQLVersion): Promise { + async startMySQL(options: InternalServerOptions, installedMySQLBinary: DownloadedMySQLVersion): Promise { 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...') diff --git a/types/index.ts b/types/index.ts index 9124e903..5a701f92 100644 --- a/types/index.ts +++ b/types/index.ts @@ -61,7 +61,10 @@ export type MySQLDB = { xSocket: string, dbName: string, username: string, - version: string, + mysql: { + version: string, + versionIsInstalledOnSystem: boolean + }, stop: () => Promise } @@ -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 = {