From d67ef7ba4ea9efb4b10ebaea8b0dab9bd766bc0e Mon Sep 17 00:00:00 2001 From: "Jae Lo Presti (DN0)" Date: Wed, 19 Apr 2023 18:45:37 +0300 Subject: [PATCH] db: working migrations with foreign keys --- knexfile.js | 40 -------------- migrations/20230419111959_setup_base_db.ts | 54 +++---------------- .../20230419134314_add_servers_table.ts | 34 ++++++++++++ migrations/20230419152153_add_perms_table.ts | 27 ++++++++++ 4 files changed, 68 insertions(+), 87 deletions(-) delete mode 100644 knexfile.js create mode 100644 migrations/20230419134314_add_servers_table.ts create mode 100644 migrations/20230419152153_add_perms_table.ts diff --git a/knexfile.js b/knexfile.js deleted file mode 100644 index 87d5dea..0000000 --- a/knexfile.js +++ /dev/null @@ -1,40 +0,0 @@ -// Update with your config settings. - -/** - * @type { Object. } - */ -module.exports = { - - staging: { - client: 'postgresql', - connection: { - database: 'my_db', - user: 'username', - password: 'password' - }, - pool: { - min: 2, - max: 10 - }, - migrations: { - tableName: 'knex_migrations' - } - }, - - production: { - client: 'postgresql', - connection: { - database: 'my_db', - user: 'username', - password: 'password' - }, - pool: { - min: 2, - max: 10 - }, - migrations: { - tableName: 'knex_migrations' - } - } - -}; diff --git a/migrations/20230419111959_setup_base_db.ts b/migrations/20230419111959_setup_base_db.ts index 7a313f0..74b9449 100644 --- a/migrations/20230419111959_setup_base_db.ts +++ b/migrations/20230419111959_setup_base_db.ts @@ -1,10 +1,10 @@ import {Knex} from "knex"; -import {neosUserPerms} from "../src/types/dbTypes"; +import {SCHEMA_NAME} from "../src/environment"; export async function up(knex: Knex): Promise { - await knex.schema.createSchemaIfNotExists('neosmanager'); + await knex.schema.createSchemaIfNotExists(SCHEMA_NAME); - await knex.schema.withSchema('neosmanager').createTable('images', (table) => { + await knex.schema.withSchema(SCHEMA_NAME).createTable('images', (table) => { table .uuid('id') .comment('Image ID') @@ -14,58 +14,18 @@ export async function up(knex: Knex): Promise { table .text('image_id') .comment('ID of the image') - .notNullable(); + .notNullable() + .unique(); table .text('image_tag') .comment('Tag of the image') .notNullable(); }); - - await knex.schema.withSchema('neosmanager').createTable('servers', (table) => { - table - .uuid('id') - .primary() - .comment('UUID of the row') - .defaultTo(knex.raw('gen_random_uuid()')); - - table - .text('server_name') - .comment('name of server'); - - table - .text('comment') - .comment('Comment on the server'); - - table - .text('container_id') - .comment('Container ID of the server') - .notNullable(); - - table - .foreign('image_id').references('images.id').deferrable('deferred') - .notNullable(); - }); - - await knex.schema.withSchema('neosmanager').createTable('neos_permissions', (table) => { - table - .uuid('id') - .primary() - .comment('UUID of the row') - .defaultTo(knex.raw('gen_random_uuid()')); - - table - .text('username') - .comment('username of the user') - .notNullable(); - - table - .enum('permissions', ['Admin', 'Builder', 'Moderator', 'Guest', 'Spectator']) - .notNullable(); - }); } -export async function down(knex: Knex): Promise { +export function down(): void { + return; } diff --git a/migrations/20230419134314_add_servers_table.ts b/migrations/20230419134314_add_servers_table.ts new file mode 100644 index 0000000..b1dac62 --- /dev/null +++ b/migrations/20230419134314_add_servers_table.ts @@ -0,0 +1,34 @@ +import { Knex } from "knex"; +import {SCHEMA_NAME} from "../src/environment"; + + +export async function up(knex: Knex): Promise { + await knex.schema.withSchema(SCHEMA_NAME).createTable('servers', (table) => { + table + .uuid('id') + .primary() + .comment('UUID of the row') + .defaultTo(knex.raw('gen_random_uuid()')); + + table + .text('server_name') + .comment('name of server'); + + table + .text('comment') + .comment('Comment on the server'); + + table + .text('container_id') + .comment('Container ID of the server') + .notNullable(); + + table + .text('image_id').references('image_id').inTable('images'); + }); +} + + +export async function down(knex: Knex): Promise { +} + diff --git a/migrations/20230419152153_add_perms_table.ts b/migrations/20230419152153_add_perms_table.ts new file mode 100644 index 0000000..e2dc9dc --- /dev/null +++ b/migrations/20230419152153_add_perms_table.ts @@ -0,0 +1,27 @@ +import { Knex } from "knex"; +import { SCHEMA_NAME } from "../src/environment"; + + +export async function up(knex: Knex): Promise { + await knex.schema.withSchema(SCHEMA_NAME).createTable('neos_permissions', (table) => { + table + .uuid('id') + .primary() + .comment('UUID of the row') + .defaultTo(knex.raw('gen_random_uuid()')); + + table + .text('username') + .comment('username of the user') + .notNullable(); + + table + .enum('permissions', ['Admin', 'Builder', 'Moderator', 'Guest', 'Spectator']) + .notNullable(); + }); +} + + +export async function down(knex: Knex): Promise { +} + -- 2.45.2