From e26abc31568988c729e75666bf9312368fc54d48 Mon Sep 17 00:00:00 2001 From: Louis Lam Date: Sat, 1 Jul 2023 02:48:42 +0800 Subject: [PATCH] Improve the setup database --- db/knex_init_db.js | 97 +++++++++++++++++++ .../2023-06-30-1348-http-body-encoding.js | 22 ----- ...2023-06-30-1354-add-description-monitor.js | 12 --- .../2023-06-30-1357-api-key-table.js | 30 ------ .../2023-06-30-1400-monitor-tls.js | 25 ----- .../2023-06-30-1401-maintenance-cron.js | 25 ----- .../2023-06-30-1413-add-parent-monitor.js | 18 ---- db/knex_migrations/README.md | 5 + server/database.js | 1 + server/setup-database.js | 18 ++++ src/pages/SetupDatabase.vue | 5 +- 11 files changed, 122 insertions(+), 136 deletions(-) delete mode 100644 db/knex_migrations/2023-06-30-1348-http-body-encoding.js delete mode 100644 db/knex_migrations/2023-06-30-1354-add-description-monitor.js delete mode 100644 db/knex_migrations/2023-06-30-1357-api-key-table.js delete mode 100644 db/knex_migrations/2023-06-30-1400-monitor-tls.js delete mode 100644 db/knex_migrations/2023-06-30-1401-maintenance-cron.js delete mode 100644 db/knex_migrations/2023-06-30-1413-add-parent-monitor.js diff --git a/db/knex_init_db.js b/db/knex_init_db.js index c5ea03972..405b022e2 100644 --- a/db/knex_init_db.js +++ b/db/knex_init_db.js @@ -350,6 +350,103 @@ async function createTables() { table.string("domain").notNullable().unique().collate("utf8_general_ci"); }); + /********************* + * Converted Patch here + *********************/ + + // 2023-06-30-1348-http-body-encoding.js + // ALTER TABLE monitor ADD http_body_encoding VARCHAR(25); + // UPDATE monitor SET http_body_encoding = 'json' WHERE (type = 'http' or type = 'keyword') AND http_body_encoding IS NULL; + await knex.schema.table("monitor", function (table) { + table.string("http_body_encoding", 25); + }); + + await knex("monitor") + .where(function () { + this.where("type", "http").orWhere("type", "keyword"); + }) + .whereNull("http_body_encoding") + .update({ + http_body_encoding: "json", + }); + + // 2023-06-30-1354-add-description-monitor.js + // ALTER TABLE monitor ADD description TEXT default null; + await knex.schema.table("monitor", function (table) { + table.text("description").defaultTo(null); + }); + + // 2023-06-30-1357-api-key-table.js + /* + CREATE TABLE [api_key] ( + [id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, + [key] VARCHAR(255) NOT NULL, + [name] VARCHAR(255) NOT NULL, + [user_id] INTEGER NOT NULL, + [created_date] DATETIME DEFAULT (DATETIME('now')) NOT NULL, + [active] BOOLEAN DEFAULT 1 NOT NULL, + [expires] DATETIME DEFAULT NULL, + CONSTRAINT FK_user FOREIGN KEY ([user_id]) REFERENCES [user]([id]) ON DELETE CASCADE ON UPDATE CASCADE + ); + */ + await knex.schema.createTable("api_key", function (table) { + table.increments("id").primary(); + table.string("key", 255).notNullable(); + table.string("name", 255).notNullable(); + table.integer("user_id").unsigned().notNullable() + .references("id").inTable("user") + .onDelete("CASCADE") + .onUpdate("CASCADE"); + table.dateTime("created_date").defaultTo(knex.fn.now()).notNullable(); + table.boolean("active").defaultTo(1).notNullable(); + table.dateTime("expires").defaultTo(null); + }); + + // 2023-06-30-1400-monitor-tls.js + /* + ALTER TABLE monitor + ADD tls_ca TEXT default null; + + ALTER TABLE monitor + ADD tls_cert TEXT default null; + + ALTER TABLE monitor + ADD tls_key TEXT default null; + */ + await knex.schema.table("monitor", function (table) { + table.text("tls_ca").defaultTo(null); + table.text("tls_cert").defaultTo(null); + table.text("tls_key").defaultTo(null); + }); + + // 2023-06-30-1401-maintenance-cron.js + /* + -- 999 characters. https://stackoverflow.com/questions/46134830/maximum-length-for-cron-job + DROP TABLE maintenance_timeslot; + ALTER TABLE maintenance ADD cron TEXT; + ALTER TABLE maintenance ADD timezone VARCHAR(255); + ALTER TABLE maintenance ADD duration INTEGER; + */ + await knex.schema + .dropTableIfExists("maintenance_timeslot") + .table("maintenance", function (table) { + table.text("cron"); + table.string("timezone", 255); + table.integer("duration"); + }); + + // 2023-06-30-1413-add-parent-monitor.js. + /* + ALTER TABLE monitor + ADD parent INTEGER REFERENCES [monitor] ([id]) ON DELETE SET NULL ON UPDATE CASCADE; + */ + await knex.schema.table("monitor", function (table) { + table.integer("parent").unsigned() + .references("id").inTable("monitor") + .onDelete("SET NULL") + .onUpdate("CASCADE"); + }); + log.info("mariadb", "Created basic tables for MariaDB"); } diff --git a/db/knex_migrations/2023-06-30-1348-http-body-encoding.js b/db/knex_migrations/2023-06-30-1348-http-body-encoding.js deleted file mode 100644 index c4cc7d941..000000000 --- a/db/knex_migrations/2023-06-30-1348-http-body-encoding.js +++ /dev/null @@ -1,22 +0,0 @@ -// ALTER TABLE monitor ADD http_body_encoding VARCHAR(25); -// UPDATE monitor SET http_body_encoding = 'json' WHERE (type = 'http' or type = 'keyword') AND http_body_encoding IS NULL; -exports.up = function (knex) { - return knex.schema.table("monitor", function (table) { - table.string("http_body_encoding", 25); - }).then(function () { - knex("monitor") - .where(function () { - this.where("type", "http").orWhere("type", "keyword"); - }) - .whereNull("http_body_encoding") - .update({ - http_body_encoding: "json", - }); - }); -}; - -exports.down = function (knex) { - return knex.schema.table("monitor", function (table) { - table.dropColumn("http_body_encoding"); - }); -}; diff --git a/db/knex_migrations/2023-06-30-1354-add-description-monitor.js b/db/knex_migrations/2023-06-30-1354-add-description-monitor.js deleted file mode 100644 index 4b291777d..000000000 --- a/db/knex_migrations/2023-06-30-1354-add-description-monitor.js +++ /dev/null @@ -1,12 +0,0 @@ -// ALTER TABLE monitor ADD description TEXT default null; -exports.up = function (knex) { - return knex.schema.table("monitor", function (table) { - table.text("description").defaultTo(null); - }); -}; - -exports.down = function (knex) { - return knex.schema.table("monitor", function (table) { - table.dropColumn("description"); - }); -}; diff --git a/db/knex_migrations/2023-06-30-1357-api-key-table.js b/db/knex_migrations/2023-06-30-1357-api-key-table.js deleted file mode 100644 index d22721ed5..000000000 --- a/db/knex_migrations/2023-06-30-1357-api-key-table.js +++ /dev/null @@ -1,30 +0,0 @@ -/* -CREATE TABLE [api_key] ( - [id] INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, - [key] VARCHAR(255) NOT NULL, - [name] VARCHAR(255) NOT NULL, - [user_id] INTEGER NOT NULL, - [created_date] DATETIME DEFAULT (DATETIME('now')) NOT NULL, - [active] BOOLEAN DEFAULT 1 NOT NULL, - [expires] DATETIME DEFAULT NULL, - CONSTRAINT FK_user FOREIGN KEY ([user_id]) REFERENCES [user]([id]) ON DELETE CASCADE ON UPDATE CASCADE -); - */ -exports.up = function (knex) { - return knex.schema.createTable("api_key", function (table) { - table.increments("id").primary(); - table.string("key", 255).notNullable(); - table.string("name", 255).notNullable(); - table.integer("user_id").unsigned().notNullable() - .references("id").inTable("user") - .onDelete("CASCADE") - .onUpdate("CASCADE"); - table.dateTime("created_date").defaultTo(knex.fn.now()).notNullable(); - table.boolean("active").defaultTo(1).notNullable(); - table.dateTime("expires").defaultTo(null); - }); -}; - -exports.down = function (knex) { - return knex.schema.dropTable("api_key"); -}; diff --git a/db/knex_migrations/2023-06-30-1400-monitor-tls.js b/db/knex_migrations/2023-06-30-1400-monitor-tls.js deleted file mode 100644 index 95d66bab1..000000000 --- a/db/knex_migrations/2023-06-30-1400-monitor-tls.js +++ /dev/null @@ -1,25 +0,0 @@ -/* -ALTER TABLE monitor - ADD tls_ca TEXT default null; - -ALTER TABLE monitor - ADD tls_cert TEXT default null; - -ALTER TABLE monitor - ADD tls_key TEXT default null; - */ -exports.up = function (knex) { - return knex.schema.table("monitor", function (table) { - table.text("tls_ca").defaultTo(null); - table.text("tls_cert").defaultTo(null); - table.text("tls_key").defaultTo(null); - }); -}; - -exports.down = function (knex) { - return knex.schema.table("monitor", function (table) { - table.dropColumn("tls_ca"); - table.dropColumn("tls_cert"); - table.dropColumn("tls_key"); - }); -}; diff --git a/db/knex_migrations/2023-06-30-1401-maintenance-cron.js b/db/knex_migrations/2023-06-30-1401-maintenance-cron.js deleted file mode 100644 index 51ae7a9b1..000000000 --- a/db/knex_migrations/2023-06-30-1401-maintenance-cron.js +++ /dev/null @@ -1,25 +0,0 @@ -/* --- 999 characters. https://stackoverflow.com/questions/46134830/maximum-length-for-cron-job -DROP TABLE maintenance_timeslot; -ALTER TABLE maintenance ADD cron TEXT; -ALTER TABLE maintenance ADD timezone VARCHAR(255); -ALTER TABLE maintenance ADD duration INTEGER; - */ -exports.up = function (knex) { - return knex.schema - .dropTableIfExists("maintenance_timeslot") - .table("maintenance", function (table) { - table.text("cron"); - table.string("timezone", 255); - table.integer("duration"); - }); -}; - -exports.down = function (knex) { - return knex.schema - .table("maintenance", function (table) { - table.dropColumn("cron"); - table.dropColumn("timezone"); - table.dropColumn("duration"); - }); -}; diff --git a/db/knex_migrations/2023-06-30-1413-add-parent-monitor.js b/db/knex_migrations/2023-06-30-1413-add-parent-monitor.js deleted file mode 100644 index 2d417b8ca..000000000 --- a/db/knex_migrations/2023-06-30-1413-add-parent-monitor.js +++ /dev/null @@ -1,18 +0,0 @@ -/* -ALTER TABLE monitor - ADD parent INTEGER REFERENCES [monitor] ([id]) ON DELETE SET NULL ON UPDATE CASCADE; - */ -exports.up = function (knex) { - return knex.schema.table("monitor", function (table) { - table.integer("parent").unsigned() - .references("id").inTable("monitor") - .onDelete("SET NULL") - .onUpdate("CASCADE"); - }); -}; - -exports.down = function (knex) { - return knex.schema.table("monitor", function (table) { - table.dropColumn("parent"); - }); -}; diff --git a/db/knex_migrations/README.md b/db/knex_migrations/README.md index 5789307b0..45dc0e96c 100644 --- a/db/knex_migrations/README.md +++ b/db/knex_migrations/README.md @@ -37,6 +37,11 @@ exports.up = function(knex) { table.increments('id'); table.decimal('price').notNullable(); table.string('name', 1000).notNullable(); + }).then(() => { + knex("products").insert([ + { price: 10, name: "Apple" }, + { price: 20, name: "Orange" }, + ]); }); }; diff --git a/server/database.js b/server/database.js index 6c5d71ee2..2e8a01eb8 100644 --- a/server/database.js +++ b/server/database.js @@ -209,6 +209,7 @@ class Database { }); await connection.execute("CREATE DATABASE IF NOT EXISTS " + dbConfig.dbName + " CHARACTER SET utf8mb4"); + connection.end(); config = { client: "mysql2", diff --git a/server/setup-database.js b/server/setup-database.js index 26a55ab34..e28adf39a 100644 --- a/server/setup-database.js +++ b/server/setup-database.js @@ -5,6 +5,7 @@ const fs = require("fs"); const path = require("path"); const Database = require("./database"); const { allowDevAllOrigin } = require("./util-server"); +const mysql = require("mysql2/promise"); /** * A standalone express app that is used to setup database @@ -145,6 +146,7 @@ class SetupDatabase { return; } + // External MariaDB if (dbConfig.type === "mariadb") { if (!dbConfig.hostname) { response.status(400).json("Hostname is required"); @@ -175,6 +177,22 @@ class SetupDatabase { this.runningSetup = false; return; } + + // Test connection + try { + const connection = await mysql.createConnection({ + host: dbConfig.hostname, + port: dbConfig.port, + user: dbConfig.username, + password: dbConfig.password, + }); + await connection.execute("SELECT 1"); + connection.end(); + } catch (e) { + response.status(400).json("Cannot connect to the database: " + e.message); + this.runningSetup = false; + return; + } } // Write db-config.json diff --git a/src/pages/SetupDatabase.vue b/src/pages/SetupDatabase.vue index 74cf88cfe..1aacf6636 100644 --- a/src/pages/SetupDatabase.vue +++ b/src/pages/SetupDatabase.vue @@ -90,8 +90,6 @@ - - @@ -145,10 +143,9 @@ export default { this.info.runningSetup = true; try { - let res = await axios.post("/setup-database", { + await axios.post("/setup-database", { dbConfig: this.dbConfig, }); - await sleep(2000); await this.goToMainServerWhenReady(); } catch (e) {