-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: add test env setup script (#708)
* fix: return error when space does not exist * chore: add task to setup test env
- Loading branch information
Showing
6 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { default as db, sequencerDB } from '../src/helpers/mysql'; | ||
|
||
const setup = async () => { | ||
try { | ||
await db.queryAsync('SELECT 1 + 1'); | ||
await sequencerDB.queryAsync('SELECT 1 + 1'); | ||
} catch (e: any) { | ||
if (e.code === 'ER_BAD_DB_ERROR') { | ||
console.error('Test database not setup, please run `yarn test:setup`'); | ||
throw new Error('Test database not setup'); | ||
} | ||
} | ||
}; | ||
|
||
export default setup; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import mysql from 'mysql'; | ||
import Pool from 'mysql/lib/Pool'; | ||
import Connection from 'mysql/lib/Connection'; | ||
import bluebird from 'bluebird'; | ||
import parse from 'connection-string'; | ||
import fs from 'fs'; | ||
|
||
// @ts-ignore | ||
const config = parse(process.env.HUB_DATABASE_URL); | ||
config.connectionLimit = 5; | ||
config.host = config.hosts[0].name; | ||
config.port = config.hosts[0].port; | ||
bluebird.promisifyAll([Pool, Connection]); | ||
const db = mysql.createPool(config); | ||
const dbName = config.path[0]; | ||
|
||
if (!dbName.endsWith('_test')) { | ||
console.error('Invalid test database name. Must end with _test'); | ||
process.exit(1); | ||
} | ||
|
||
async function run() { | ||
const splitToken = ');'; | ||
|
||
console.log('Start test database setup'); | ||
|
||
console.info(`- Dropping existing database: ${dbName}`); | ||
await db.queryAsync(`DROP DATABASE IF EXISTS ${dbName}`); | ||
|
||
console.info(`- Creating new database: ${dbName}`); | ||
await db.queryAsync(`CREATE DATABASE ${dbName}`); | ||
|
||
const schema = fs | ||
.readFileSync('./src/helpers/schema.sql', 'utf8') | ||
.replaceAll('CREATE TABLE ', `CREATE TABLE ${dbName}.`) | ||
.split(splitToken) | ||
.filter(s => s.trim().length > 0); | ||
|
||
console.info(`- Importing the schema (${schema.length} tables)`); | ||
|
||
for (const statement of schema) { | ||
await db.queryAsync(`${statement}${splitToken}`); | ||
} | ||
|
||
console.info(`Setting the permissions`); | ||
await db.queryAsync( | ||
`ALTER USER '${config.user}'@'localhost' IDENTIFIED WITH mysql_native_password BY '${config.password}';` | ||
); | ||
await db.queryAsync('FLUSH PRIVILEGES;'); | ||
|
||
console.log('Setup complete!'); | ||
} | ||
|
||
(async () => { | ||
try { | ||
await run(); | ||
process.exit(0); | ||
} catch (e) { | ||
console.error(e); | ||
process.exit(1); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { default as db, sequencerDB } from '../src/helpers/mysql'; | ||
|
||
const teardown = async () => { | ||
await db.endAsync(); | ||
await sequencerDB.endAsync(); | ||
}; | ||
|
||
export default teardown; |