From 358984b474e4b5086afc77e6498c63dc14eeabd3 Mon Sep 17 00:00:00 2001 From: "Jill \"oatmealine\" Monoids" Date: Sun, 12 Nov 2023 21:45:23 +0300 Subject: [PATCH] switch to knex migrations --- knexfile.js | 23 +++++++++++++++ migrations/20231112182439_subscriptions.js | 27 +++++++++++++++++ migrations/20231112182746_counters.js | 34 ++++++++++++++++++++++ src/index.ts | 3 -- src/lib/db.ts | 34 +++++----------------- 5 files changed, 91 insertions(+), 30 deletions(-) create mode 100644 knexfile.js create mode 100644 migrations/20231112182439_subscriptions.js create mode 100644 migrations/20231112182746_counters.js diff --git a/knexfile.js b/knexfile.js new file mode 100644 index 0000000..2d33c02 --- /dev/null +++ b/knexfile.js @@ -0,0 +1,23 @@ +// Update with your config settings. + +/** + * @type { Object. } + */ +module.exports = { + development: { + client: 'sqlite3', + connection: { + filename: './jillo.sqlite' + }, + useNullAsDefault: true, + }, + + production: { + // meh who needs postgres for now lole + client: 'sqlite3', + connection: { + filename: './jillo.sqlite' + }, + useNullAsDefault: true, + } +}; diff --git a/migrations/20231112182439_subscriptions.js b/migrations/20231112182439_subscriptions.js new file mode 100644 index 0000000..fdb40f4 --- /dev/null +++ b/migrations/20231112182439_subscriptions.js @@ -0,0 +1,27 @@ +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.up = function(knex) { + return knex.schema + .createTable('scheduledSubscriptions', table => { + table.string('name').primary(); + table.timestamp('next').defaultTo(knex.fn.now()); + }) + .createTable('subscriptions', table => { + table.string('key') + .references('name').inTable('scheduledSubscriptions'); + table.string('channel'); + table.string('guild').nullable(); + }); +}; + +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.down = function(knex) { + return knex.schema + .dropTable('scheduledSubscriptions') + .dropTable('subscriptions'); +}; diff --git a/migrations/20231112182746_counters.js b/migrations/20231112182746_counters.js new file mode 100644 index 0000000..9ff59f2 --- /dev/null +++ b/migrations/20231112182746_counters.js @@ -0,0 +1,34 @@ +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.up = function(knex) { + return knex.schema + .createTable('counters', table => { + table.string('key').notNullable(); + table.string('name').notNullable(); + table.string('emoji').notNullable(); + table.integer('value').defaultTo(0); + table.string('channel').notNullable(); + table.string('guild').notNullable(); + table.string('message'); + table.string('messageTemplate'); + table.boolean('allowlistConsumer'); + table.boolean('allowlistProducer'); + }) + .createTable('counterUserLink', table => { + table.string('key').references('key').inTable('counters'); + table.string('user').notNullable(); + table.boolean('producer'); + }); +}; + +/** + * @param { import("knex").Knex } knex + * @returns { Promise } + */ +exports.down = function(knex) { + return knex.schema + .dropTable('counters') + .dropTable('counterUserLink'); +}; diff --git a/src/index.ts b/src/index.ts index c8b9e58..fd08898 100644 --- a/src/index.ts +++ b/src/index.ts @@ -3,7 +3,6 @@ import * as fs from 'fs'; const { token } = JSON.parse(fs.readFileSync('./config.json', 'utf8')); import * as path from 'path'; import { initializeAnnouncements } from './lib/subscriptions'; -import { initTables } from './lib/db'; import * as log from './lib/log'; import chalk from 'chalk'; import prettyBytes from 'pretty-bytes'; @@ -23,8 +22,6 @@ const bot = new Client({ async function init() { log.nonsense('booting chip...'); - await initTables(); - log.nonsense('setting up connection...'); try { diff --git a/src/lib/db.ts b/src/lib/db.ts index af160fa..9624ba8 100644 --- a/src/lib/db.ts +++ b/src/lib/db.ts @@ -32,32 +32,12 @@ export interface Counter { channel: string, guild: string, message?: string, - allowlist: boolean + messageTemplate?: string, + allowlistConsumer: boolean, + allowlistProducer: boolean } - -export async function initTables() { - await db.schema.createTableIfNotExists('scheduledSubscriptions', table => { - table.string('name').primary(); - table.timestamp('next').defaultTo(db.fn.now()); - }); - await db.schema.createTableIfNotExists('subscriptions', table => { - table.string('key') - .references('name').inTable('scheduledSubscriptions'); - table.string('channel'); - table.string('guild').nullable(); - }); - await db.schema.createTableIfNotExists('counters', table => { - table.string('key').notNullable(); - table.string('name').notNullable(); - table.string('emoji').notNullable(); - table.integer('value').defaultTo(0); - table.string('channel').notNullable(); - table.string('guild').notNullable(); - table.string('message'); - table.boolean('allowlist'); - }); - await db.schema.createTableIfNotExists('counterUserLink', table => { - table.string('key').references('key').inTable('counters'); - table.string('user').notNullable(); - }); +export interface CounterUserLink { + key: string, + user: string, + producer: boolean } \ No newline at end of file