Update db migration and dockerfile

This commit is contained in:
Louis Lam
2023-06-30 17:26:37 +08:00
parent 16a1a66e09
commit 7975caf29e
10 changed files with 171 additions and 25 deletions

View File

@@ -0,0 +1,22 @@
// 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");
});
};

View File

@@ -0,0 +1,12 @@
// 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");
});
};

View File

@@ -0,0 +1,30 @@
/*
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");
};

View File

@@ -0,0 +1,25 @@
/*
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");
});
};

View File

@@ -0,0 +1,25 @@
/*
-- 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");
});
};

View File

@@ -0,0 +1,18 @@
/*
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");
});
};

View File

@@ -21,7 +21,9 @@ exports.down = function(knex) {
## Example
20230211120000_create_users_products.js
YYYY-MM-DD-HHMM-create-users-products.js
2023-06-30-1348-create-users-products.js
```js
exports.up = function(knex) {
@@ -44,3 +46,5 @@ exports.down = function(knex) {
.dropTable("users");
};
```
https://knexjs.org/guide/migrations.html#transactions-in-migrations