From b16faf3e1b5cfe8baac718012fe1b7d761f9b451 Mon Sep 17 00:00:00 2001 From: Alexey Yarmosh Date: Tue, 12 Dec 2023 16:48:10 +0100 Subject: [PATCH 01/11] feat: initially add seed and migration files --- config/init.sql | 81 ------------------------------------------------ migrations/v1.js | 40 ++++++++++++++++++++++++ seeds/v1.js | 24 ++++++++++++++ test/setup.ts | 19 ++++++++++-- 4 files changed, 80 insertions(+), 84 deletions(-) delete mode 100644 config/init.sql create mode 100644 migrations/v1.js create mode 100644 seeds/v1.js diff --git a/config/init.sql b/config/init.sql deleted file mode 100644 index 9f1c9289..00000000 --- a/config/init.sql +++ /dev/null @@ -1,81 +0,0 @@ -CREATE DATABASE IF NOT EXISTS directus; -USE directus; - -CREATE TABLE IF NOT EXISTS adopted_probes ( - id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY, - user_created CHAR(36), - date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - user_updated CHAR(36), - date_updated TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, - userId VARCHAR(255) NOT NULL, - ip VARCHAR(255) NOT NULL, - uuid VARCHAR(255), - lastSyncDate DATE NOT NULL, - isCustomCity TINYINT DEFAULT 0, - tags LONGTEXT, - status VARCHAR(255) NOT NULL, - version VARCHAR(255) NOT NULL, - country VARCHAR(255) NOT NULL, - city VARCHAR(255), - state VARCHAR(255), - latitude FLOAT(10, 5), - longitude FLOAT(10, 5), - asn INTEGER NOT NULL, - network VARCHAR(255) NOT NULL, - countryOfCustomCity VARCHAR(255) -); - -CREATE TABLE IF NOT EXISTS directus_users ( - id CHAR(36), - github VARCHAR(255) -); - -CREATE TABLE IF NOT EXISTS directus_notifications ( - id CHAR(10), - recipient CHAR(36), - timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP, - subject VARCHAR(255), - message TEXT -); - -INSERT IGNORE INTO adopted_probes ( - userId, - lastSyncDate, - ip, - uuid, - isCustomCity, - tags, - status, - version, - country, - city, - latitude, - longitude, - network, - asn, - countryOfCustomCity -) VALUES ( - '89da69bd-a236-4ab7-9c5d-b5f52ce09959', - CURRENT_DATE, - '51.158.22.211', - 'c77f021d-23ff-440a-aa96-35e82c73e731', - 1, - '["mytag1"]', - 'ready', - '0.26.0', - 'FR', - 'Marseille', - '43.29695', - '5.38107', - 'SCALEWAY S.A.S.', - 12876, - 'FR' -); - -INSERT IGNORE INTO directus_users ( - id, - github -) VALUES ( - '89da69bd-a236-4ab7-9c5d-b5f52ce09959', - 'jimaek' -); diff --git a/migrations/v1.js b/migrations/v1.js new file mode 100644 index 00000000..b27bf1a4 --- /dev/null +++ b/migrations/v1.js @@ -0,0 +1,40 @@ +export const up = async (db) => { + await db.schema.createTable('directus_users', (table) => { + table.specificType('id', `CHAR(36)`); + table.string('github', 255); + }); + + await db.schema.createTable('directus_notifications', (table) => { + table.specificType('id', 'CHAR(10)'); + table.specificType('recipient', 'CHAR(36)'); + table.timestamp('timestamp').defaultTo(db.fn.now()); + table.string('subject', 255); + table.text('message'); + }); + + await db.schema.createTable('adopted_probes', (table) => { + table.increments('id').unsigned().primary(); + table.specificType('user_created', 'CHAR(36)'); + table.timestamp('date_created').defaultTo(db.fn.now()); + table.specificType('user_updated', 'CHAR(36)'); + table.timestamp('date_updated').defaultTo(db.raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')); + table.string('userId', 255).notNullable(); + table.string('ip', 255).notNullable(); + table.string('uuid', 255); + table.date('lastSyncDate').notNullable(); + table.boolean('isCustomCity').defaultTo(0); + table.text('tags', 'longtext'); + table.string('status', 255).notNullable(); + table.string('version', 255).notNullable(); + table.string('country', 255).notNullable(); + table.string('city', 255); + table.string('state', 255); + table.float('latitude', 10, 5); + table.float('longitude', 10, 5); + table.integer('asn').notNullable(); + table.string('network', 255).notNullable(); + table.string('countryOfCustomCity', 255); + }); +}; + +export const down = () => {}; diff --git a/seeds/v1.js b/seeds/v1.js new file mode 100644 index 00000000..b3a0cd86 --- /dev/null +++ b/seeds/v1.js @@ -0,0 +1,24 @@ +export const seed = async (db) => { + await db('directus_users').insert({ + id: '89da69bd-a236-4ab7-9c5d-b5f52ce09959', + github: 'jimaek', + }); + + await db('adopted_probes').insert({ + userId: '89da69bd-a236-4ab7-9c5d-b5f52ce09959', + lastSyncDate: db.fn.now(), + ip: '51.158.22.211', + uuid: 'c77f021d-23ff-440a-aa96-35e82c73e731', + isCustomCity: 1, + tags: '["mytag1"]', + status: 'ready', + version: '0.26.0', + country: 'FR', + city: 'Marseille', + latitude: '43.29695', + longitude: '5.38107', + network: 'SCALEWAY S.A.S.', + asn: 12876, + countryOfCustomCity: 'FR', + }); +}; diff --git a/test/setup.ts b/test/setup.ts index c196d38a..f3214712 100644 --- a/test/setup.ts +++ b/test/setup.ts @@ -1,3 +1,4 @@ +import Bluebird from 'bluebird'; import chai from 'chai'; import nock from 'nock'; import path from 'node:path'; @@ -13,21 +14,33 @@ import { import chaiOas from './plugins/oas/index.js'; import { getRedisClient, initRedis } from '../src/lib/redis/client.js'; -import { client } from '../src/lib/sql/client.js'; -import { USERS_TABLE } from '../src/lib/adopted-probes.js'; +import { client as sql } from '../src/lib/sql/client.js'; before(async () => { chai.use(await chaiOas({ specPath: path.join(fileURLToPath(new URL('.', import.meta.url)), '../public/v1/spec.yaml') })); + await initRedis(); const redis = getRedisClient(); await redis.flushDb(); - await client(USERS_TABLE).insert({ id: '89da69bd-a236-4ab7-9c5d-b5f52ce09959', github: 'jimaek' }).onConflict().ignore(); + + await dropAllTables(sql); + await sql.migrate.latest(); + await sql.seed.run(); nock.disableNetConnect(); nock.enableNetConnect('127.0.0.1'); + await populateIpList(); await populateDomainList(); await populateIpRangeList(); await populateMemList(); await populateNockCitiesList(); }); + +const dropAllTables = async (sql) => { + const allTables = (await sql('information_schema.tables') + .whereRaw(`table_schema = database()`) + .select(`table_name as table`) + ).map(({ table }) => table); + await Bluebird.map(allTables, table => sql.schema.raw(`drop table \`${table}\``)); +}; From 86ae0f7312f6bda909ebf813851c6d33bdbb8e5c Mon Sep 17 00:00:00 2001 From: Alexey Yarmosh Date: Tue, 12 Dec 2023 16:55:20 +0100 Subject: [PATCH 02/11] feat: place seeds and migrations in folders --- knexfile.js | 6 ++++++ migrations/{v1.js => test/index.js} | 0 seeds/{v1.js => test/index.js} | 0 3 files changed, 6 insertions(+) rename migrations/{v1.js => test/index.js} (100%) rename seeds/{v1.js => test/index.js} (100%) diff --git a/knexfile.js b/knexfile.js index bf51bf0e..9c84a47c 100644 --- a/knexfile.js +++ b/knexfile.js @@ -18,6 +18,12 @@ export default _.merge({}, ...[ 'development', 'production', 'staging', 'test' ] propagateCreateError: false, }, acquireConnectionTimeout: 10000, + seeds: { + directory: `./seeds/${environment}`, + }, + migrations: { + directory: `./migrations/${environment}`, + }, }, }; })); diff --git a/migrations/v1.js b/migrations/test/index.js similarity index 100% rename from migrations/v1.js rename to migrations/test/index.js diff --git a/seeds/v1.js b/seeds/test/index.js similarity index 100% rename from seeds/v1.js rename to seeds/test/index.js From 06df9a24be43c3d625853ad389ca6e2358670d09 Mon Sep 17 00:00:00 2001 From: Alexey Yarmosh Date: Tue, 12 Dec 2023 17:11:33 +0100 Subject: [PATCH 03/11] feat: remove not used inserts --- seeds/test/index.js | 18 ------------------ src/lib/adopted-probes.ts | 4 ++-- 2 files changed, 2 insertions(+), 20 deletions(-) diff --git a/seeds/test/index.js b/seeds/test/index.js index b3a0cd86..e1cd6010 100644 --- a/seeds/test/index.js +++ b/seeds/test/index.js @@ -3,22 +3,4 @@ export const seed = async (db) => { id: '89da69bd-a236-4ab7-9c5d-b5f52ce09959', github: 'jimaek', }); - - await db('adopted_probes').insert({ - userId: '89da69bd-a236-4ab7-9c5d-b5f52ce09959', - lastSyncDate: db.fn.now(), - ip: '51.158.22.211', - uuid: 'c77f021d-23ff-440a-aa96-35e82c73e731', - isCustomCity: 1, - tags: '["mytag1"]', - status: 'ready', - version: '0.26.0', - country: 'FR', - city: 'Marseille', - latitude: '43.29695', - longitude: '5.38107', - network: 'SCALEWAY S.A.S.', - asn: 12876, - countryOfCustomCity: 'FR', - }); }; diff --git a/src/lib/adopted-probes.ts b/src/lib/adopted-probes.ts index 993f52d3..bc891a4e 100644 --- a/src/lib/adopted-probes.ts +++ b/src/lib/adopted-probes.ts @@ -276,8 +276,8 @@ export class AdoptedProbes { private async sendNotification (adoptedProbe: AdoptedProbe, connectedProbe: Probe) { await this.sql.raw(` - INSERT INTO directus_notifications (recipient, subject, message) SELECT :recipient, :subject, :message - WHERE NOT EXISTS (SELECT 1 FROM directus_notifications WHERE recipient = :recipient AND message = :message AND DATE(timestamp) = CURRENT_DATE) + INSERT INTO ${NOTIFICATIONS_TABLE} (recipient, subject, message) SELECT :recipient, :subject, :message + WHERE NOT EXISTS (SELECT 1 FROM ${NOTIFICATIONS_TABLE} WHERE recipient = :recipient AND message = :message AND DATE(timestamp) = CURRENT_DATE) `, { recipient: adoptedProbe.userId, subject: 'Adopted probe country change', From b02450adc8e846bd359acc997d0573a8f31b68c3 Mon Sep 17 00:00:00 2001 From: Alexey Yarmosh Date: Tue, 12 Dec 2023 17:24:23 +0100 Subject: [PATCH 04/11] fix: fix adoption code test --- docker-compose.yml | 2 -- test/setup.ts | 6 +++--- test/tests/integration/adoption-code.test.ts | 15 +++++++++------ 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 690bd6f2..82a166ea 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,5 +18,3 @@ services: - MARIADB_ROOT_PASSWORD=root ports: - "3306:3306" - volumes: - - ./config/init.sql:/docker-entrypoint-initdb.d/init.sql diff --git a/test/setup.ts b/test/setup.ts index f3214712..b4ddb132 100644 --- a/test/setup.ts +++ b/test/setup.ts @@ -3,6 +3,7 @@ import chai from 'chai'; import nock from 'nock'; import path from 'node:path'; import { fileURLToPath } from 'node:url'; +import type { Knex } from 'knex'; import { populateMemList } from '../src/lib/geoip/whitelist.js'; import { @@ -11,7 +12,6 @@ import { populateIpRangeList, populateNockCitiesList, } from './utils/populate-static-files.js'; - import chaiOas from './plugins/oas/index.js'; import { getRedisClient, initRedis } from '../src/lib/redis/client.js'; import { client as sql } from '../src/lib/sql/client.js'; @@ -37,10 +37,10 @@ before(async () => { await populateNockCitiesList(); }); -const dropAllTables = async (sql) => { +const dropAllTables = async (sql: Knex) => { const allTables = (await sql('information_schema.tables') .whereRaw(`table_schema = database()`) .select(`table_name as table`) - ).map(({ table }) => table); + ).map(({ table }: { table: string }) => table); await Bluebird.map(allTables, table => sql.schema.raw(`drop table \`${table}\``)); }; diff --git a/test/tests/integration/adoption-code.test.ts b/test/tests/integration/adoption-code.test.ts index 13ba3c17..2c62a8b0 100644 --- a/test/tests/integration/adoption-code.test.ts +++ b/test/tests/integration/adoption-code.test.ts @@ -1,18 +1,21 @@ +import type { Server } from 'node:http'; import nock from 'nock'; import { expect } from 'chai'; import * as sinon from 'sinon'; import type { Socket } from 'socket.io-client'; -import request from 'supertest'; +import request, { type SuperTest, type Test } from 'supertest'; import { getTestServer, addFakeProbe, deleteFakeProbe } from '../../utils/server.js'; import nockGeoIpProviders from '../../utils/nock-geo-ip.js'; -let probe: Socket; -const app = await getTestServer(); -const requestAgent = request(app); -const adoptionCodeStub = sinon.stub(); - describe('Adoption code', () => { + let app: Server; + let requestAgent: SuperTest; + let probe: Socket; + const adoptionCodeStub = sinon.stub(); + before(async () => { + app = await getTestServer(); + requestAgent = request(app); nockGeoIpProviders(); probe = await addFakeProbe({ From f400cf70b41abb1b892e394a5d4803e319c1d67f Mon Sep 17 00:00:00 2001 From: Alexey Yarmosh Date: Tue, 12 Dec 2023 17:31:42 +0100 Subject: [PATCH 05/11] fix: remove init mariadb script from gh ci --- .github/workflows/ci.yml | 4 ---- 1 file changed, 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 85d10434..86beac35 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -41,10 +41,6 @@ jobs: steps: - uses: actions/checkout@v3 - - name: Init MariaDB - run: | - mysql -u directus -ppassword --database=directus --protocol=tcp < config/init.sql - - uses: actions/setup-node@v3 with: node-version: 18.x From 620d09d1b9b1585966863a5007e4653c9278a8da Mon Sep 17 00:00:00 2001 From: Alexey Yarmosh Date: Tue, 12 Dec 2023 17:49:33 +0100 Subject: [PATCH 06/11] feat: add node env check --- test/setup.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test/setup.ts b/test/setup.ts index b4ddb132..460b99ba 100644 --- a/test/setup.ts +++ b/test/setup.ts @@ -16,6 +16,10 @@ import chaiOas from './plugins/oas/index.js'; import { getRedisClient, initRedis } from '../src/lib/redis/client.js'; import { client as sql } from '../src/lib/sql/client.js'; +if (process.env['NODE_ENV'] !== 'test') { + throw new Error(`NODE_ENV is not 'test' but '${process.env['NODE_ENV']}'.`); +} + before(async () => { chai.use(await chaiOas({ specPath: path.join(fileURLToPath(new URL('.', import.meta.url)), '../public/v1/spec.yaml') })); From 083c22c5f75b4c9ba85442c45e3b52225b7e894e Mon Sep 17 00:00:00 2001 From: Alexey Yarmosh Date: Tue, 12 Dec 2023 19:09:34 +0100 Subject: [PATCH 07/11] fix: use migration both in tests and in docker compose --- docker-compose.yml | 2 ++ knexfile.js | 3 --- migrations/index.js | 13 +++++++++++++ migrations/init.sql | 36 ++++++++++++++++++++++++++++++++++++ migrations/test/index.js | 40 ---------------------------------------- 5 files changed, 51 insertions(+), 43 deletions(-) create mode 100644 migrations/index.js create mode 100644 migrations/init.sql delete mode 100644 migrations/test/index.js diff --git a/docker-compose.yml b/docker-compose.yml index 82a166ea..796af65f 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,3 +18,5 @@ services: - MARIADB_ROOT_PASSWORD=root ports: - "3306:3306" + volumes: + - ./migrations/init.sql:/docker-entrypoint-initdb.d/init.sql diff --git a/knexfile.js b/knexfile.js index 9c84a47c..1ac72c2f 100644 --- a/knexfile.js +++ b/knexfile.js @@ -21,9 +21,6 @@ export default _.merge({}, ...[ 'development', 'production', 'staging', 'test' ] seeds: { directory: `./seeds/${environment}`, }, - migrations: { - directory: `./migrations/${environment}`, - }, }, }; })); diff --git a/migrations/index.js b/migrations/index.js new file mode 100644 index 00000000..29471e80 --- /dev/null +++ b/migrations/index.js @@ -0,0 +1,13 @@ +import Bluebird from 'bluebird'; +import fs from 'node:fs'; +import path from 'path'; +import { fileURLToPath } from 'url'; + +export const up = async (db) => { + const __dirname = path.dirname(fileURLToPath(import.meta.url)); + const sql = fs.readFileSync(path.join(__dirname, 'init.sql'), 'utf8'); + const queries = sql.split('\n\n'); + await Bluebird.map(queries, query => db.schema.raw(query)); +}; + +export const down = () => {}; diff --git a/migrations/init.sql b/migrations/init.sql new file mode 100644 index 00000000..54da6bb8 --- /dev/null +++ b/migrations/init.sql @@ -0,0 +1,36 @@ +CREATE TABLE IF NOT EXISTS adopted_probes ( + id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY, + user_created CHAR(36), + date_created TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + user_updated CHAR(36), + date_updated TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + userId VARCHAR(255) NOT NULL, + ip VARCHAR(255) NOT NULL, + uuid VARCHAR(255), + lastSyncDate DATE NOT NULL, + isCustomCity TINYINT DEFAULT 0, + tags LONGTEXT, + status VARCHAR(255) NOT NULL, + version VARCHAR(255) NOT NULL, + country VARCHAR(255) NOT NULL, + city VARCHAR(255), + state VARCHAR(255), + latitude FLOAT(10, 5), + longitude FLOAT(10, 5), + asn INTEGER NOT NULL, + network VARCHAR(255) NOT NULL, + countryOfCustomCity VARCHAR(255) +); + +CREATE TABLE IF NOT EXISTS directus_users ( + id CHAR(36), + github VARCHAR(255) +); + +CREATE TABLE IF NOT EXISTS directus_notifications ( + id CHAR(10), + recipient CHAR(36), + timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP, + subject VARCHAR(255), + message TEXT +); diff --git a/migrations/test/index.js b/migrations/test/index.js deleted file mode 100644 index b27bf1a4..00000000 --- a/migrations/test/index.js +++ /dev/null @@ -1,40 +0,0 @@ -export const up = async (db) => { - await db.schema.createTable('directus_users', (table) => { - table.specificType('id', `CHAR(36)`); - table.string('github', 255); - }); - - await db.schema.createTable('directus_notifications', (table) => { - table.specificType('id', 'CHAR(10)'); - table.specificType('recipient', 'CHAR(36)'); - table.timestamp('timestamp').defaultTo(db.fn.now()); - table.string('subject', 255); - table.text('message'); - }); - - await db.schema.createTable('adopted_probes', (table) => { - table.increments('id').unsigned().primary(); - table.specificType('user_created', 'CHAR(36)'); - table.timestamp('date_created').defaultTo(db.fn.now()); - table.specificType('user_updated', 'CHAR(36)'); - table.timestamp('date_updated').defaultTo(db.raw('CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP')); - table.string('userId', 255).notNullable(); - table.string('ip', 255).notNullable(); - table.string('uuid', 255); - table.date('lastSyncDate').notNullable(); - table.boolean('isCustomCity').defaultTo(0); - table.text('tags', 'longtext'); - table.string('status', 255).notNullable(); - table.string('version', 255).notNullable(); - table.string('country', 255).notNullable(); - table.string('city', 255); - table.string('state', 255); - table.float('latitude', 10, 5); - table.float('longitude', 10, 5); - table.integer('asn').notNullable(); - table.string('network', 255).notNullable(); - table.string('countryOfCustomCity', 255); - }); -}; - -export const down = () => {}; From 16d9f52e7b1803f2073f414c8ccc2a1e6e9c574f Mon Sep 17 00:00:00 2001 From: Alexey Yarmosh Date: Mon, 18 Dec 2023 12:04:53 +0100 Subject: [PATCH 08/11] refactor: use sql connection as object --- config/default.cjs | 8 +++++++- config/test.cjs | 5 +++++ migrations/index.js | 6 ++---- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/config/default.cjs b/config/default.cjs index 8afe2228..19802f7e 100644 --- a/config/default.cjs +++ b/config/default.cjs @@ -13,7 +13,13 @@ module.exports = { }, db: { type: 'mysql', - connection: 'mysql://directus:password@localhost:3306/directus', + connection: { + host: 'localhost', + user: 'directus', + password: 'password', + database: 'directus', + port: 3306, + }, }, admin: { key: '', diff --git a/config/test.cjs b/config/test.cjs index f8f2980f..9e54650a 100644 --- a/config/test.cjs +++ b/config/test.cjs @@ -4,6 +4,11 @@ module.exports = { tls: false, }, }, + db: { + connection: { + multipleStatements: true, + }, + }, admin: { key: 'admin', }, diff --git a/migrations/index.js b/migrations/index.js index 29471e80..de34f073 100644 --- a/migrations/index.js +++ b/migrations/index.js @@ -1,13 +1,11 @@ -import Bluebird from 'bluebird'; import fs from 'node:fs'; import path from 'path'; import { fileURLToPath } from 'url'; export const up = async (db) => { const __dirname = path.dirname(fileURLToPath(import.meta.url)); - const sql = fs.readFileSync(path.join(__dirname, 'init.sql'), 'utf8'); - const queries = sql.split('\n\n'); - await Bluebird.map(queries, query => db.schema.raw(query)); + const query = fs.readFileSync(path.join(__dirname, 'init.sql'), 'utf8'); + await db.schema.raw(query); }; export const down = () => {}; From 4fb31cef26972f459a2eb45f3a57b78796a71919 Mon Sep 17 00:00:00 2001 From: Alexey Yarmosh Date: Mon, 18 Dec 2023 12:20:19 +0100 Subject: [PATCH 09/11] feat: separate dev and test dbs --- .github/workflows/ci.yml | 2 +- config/create-dbs.sql | 5 +++++ config/test.cjs | 1 + docker-compose.yml | 3 ++- migrations/{init.sql => create-tables.sql} | 0 migrations/index.js | 4 ++-- test/setup.ts | 8 ++++++-- 7 files changed, 17 insertions(+), 6 deletions(-) create mode 100644 config/create-dbs.sql rename migrations/{init.sql => create-tables.sql} (100%) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 86beac35..e5a35042 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -29,7 +29,7 @@ jobs: ports: - 3306:3306 env: - MARIADB_DATABASE: directus + MARIADB_DATABASE: directus-test MARIADB_USER: directus MARIADB_PASSWORD: password MARIADB_RANDOM_ROOT_PASSWORD: 1 diff --git a/config/create-dbs.sql b/config/create-dbs.sql new file mode 100644 index 00000000..038a729f --- /dev/null +++ b/config/create-dbs.sql @@ -0,0 +1,5 @@ +CREATE DATABASE IF NOT EXISTS directus; +GRANT ALL PRIVILEGES ON directus.* to 'directus'@'%'; + +CREATE DATABASE IF NOT EXISTS `directus-test`; +GRANT ALL PRIVILEGES ON `directus-test`.* to 'directus'@'%'; diff --git a/config/test.cjs b/config/test.cjs index 9e54650a..d8ee24f1 100644 --- a/config/test.cjs +++ b/config/test.cjs @@ -6,6 +6,7 @@ module.exports = { }, db: { connection: { + database: 'directus-test', multipleStatements: true, }, }, diff --git a/docker-compose.yml b/docker-compose.yml index 796af65f..c5340303 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -19,4 +19,5 @@ services: ports: - "3306:3306" volumes: - - ./migrations/init.sql:/docker-entrypoint-initdb.d/init.sql + - ./config/create-dbs.sql:/docker-entrypoint-initdb.d/01-create-dbs.sql + - ./migrations/create-tables.sql:/docker-entrypoint-initdb.d/02-create-tables.sql diff --git a/migrations/init.sql b/migrations/create-tables.sql similarity index 100% rename from migrations/init.sql rename to migrations/create-tables.sql diff --git a/migrations/index.js b/migrations/index.js index de34f073..e5f3b6c7 100644 --- a/migrations/index.js +++ b/migrations/index.js @@ -3,8 +3,8 @@ import path from 'path'; import { fileURLToPath } from 'url'; export const up = async (db) => { - const __dirname = path.dirname(fileURLToPath(import.meta.url)); - const query = fs.readFileSync(path.join(__dirname, 'init.sql'), 'utf8'); + const __filename = fileURLToPath(import.meta.url); + const query = fs.readFileSync(path.join(__filename + '.sql'), 'utf8'); await db.schema.raw(query); }; diff --git a/test/setup.ts b/test/setup.ts index 460b99ba..298cbf2c 100644 --- a/test/setup.ts +++ b/test/setup.ts @@ -1,3 +1,4 @@ +import config from 'config'; import Bluebird from 'bluebird'; import chai from 'chai'; import nock from 'nock'; @@ -16,8 +17,11 @@ import chaiOas from './plugins/oas/index.js'; import { getRedisClient, initRedis } from '../src/lib/redis/client.js'; import { client as sql } from '../src/lib/sql/client.js'; -if (process.env['NODE_ENV'] !== 'test') { - throw new Error(`NODE_ENV is not 'test' but '${process.env['NODE_ENV']}'.`); +const dbConfig = config.get<{ connection: { database: string, host: string } }>('db'); +console.log(dbConfig); + +if (!dbConfig.connection.database.endsWith('-test') && dbConfig.connection.host !== 'localhost') { + throw new Error(`Database name for test env needs to end with "-test" or the host must be "localhost". Got "${dbConfig.connection.database}"@"${dbConfig.connection.host}".`); } before(async () => { From be2d86217eeef2663dab461f8701f9d3790219cc Mon Sep 17 00:00:00 2001 From: Alexey Yarmosh Date: Mon, 18 Dec 2023 12:20:58 +0100 Subject: [PATCH 10/11] fix: rename migration script --- migrations/{index.js => create-tables.js} | 0 migrations/{create-tables.sql => create-tables.js.sql} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename migrations/{index.js => create-tables.js} (100%) rename migrations/{create-tables.sql => create-tables.js.sql} (100%) diff --git a/migrations/index.js b/migrations/create-tables.js similarity index 100% rename from migrations/index.js rename to migrations/create-tables.js diff --git a/migrations/create-tables.sql b/migrations/create-tables.js.sql similarity index 100% rename from migrations/create-tables.sql rename to migrations/create-tables.js.sql From 013310ea732e5fdd72ba4ed6929ffff5a9f01d53 Mon Sep 17 00:00:00 2001 From: Alexey Yarmosh Date: Mon, 18 Dec 2023 12:26:09 +0100 Subject: [PATCH 11/11] fix: fix docker compose file --- docker-compose.yml | 2 +- test/setup.ts | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index c5340303..084b5b23 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -20,4 +20,4 @@ services: - "3306:3306" volumes: - ./config/create-dbs.sql:/docker-entrypoint-initdb.d/01-create-dbs.sql - - ./migrations/create-tables.sql:/docker-entrypoint-initdb.d/02-create-tables.sql + - ./migrations/create-tables.js.sql:/docker-entrypoint-initdb.d/02-create-tables.sql diff --git a/test/setup.ts b/test/setup.ts index 298cbf2c..4cdb74cb 100644 --- a/test/setup.ts +++ b/test/setup.ts @@ -18,7 +18,6 @@ import { getRedisClient, initRedis } from '../src/lib/redis/client.js'; import { client as sql } from '../src/lib/sql/client.js'; const dbConfig = config.get<{ connection: { database: string, host: string } }>('db'); -console.log(dbConfig); if (!dbConfig.connection.database.endsWith('-test') && dbConfig.connection.host !== 'localhost') { throw new Error(`Database name for test env needs to end with "-test" or the host must be "localhost". Got "${dbConfig.connection.database}"@"${dbConfig.connection.host}".`);