From 932cc1a9a57b9ba110a3fbd45b540b22aa277542 Mon Sep 17 00:00:00 2001 From: Alexey Yarmosh Date: Tue, 12 Dec 2023 19:09:34 +0100 Subject: [PATCH] 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..f90a1dc9 --- /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 = () => {};