Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature: implement PostgreSQL for storage of per-server settings #19

Closed
wants to merge 37 commits into from

Conversation

RalphORama
Copy link
Member

@RalphORama RalphORama commented Dec 28, 2023

Summary

As LinkFix gets added to more servers, several people have reached out requesting features that would require per-server configuration. We need to store those configurations.

Methodology

Firstly, I am going to try to avoid using ORMs as much as possible. the pg package offers the ability to execute raw queries which should be good enough for our requirements. If, in the future, the database gets more complicated, I wouldn't have an issue moving to something like Sequelize.

Secondly, I want LinkFix to store as little data as possible. I think we shouldn't create records for guilds until someone in the guild actually sets configuration options. This keeps us in the clear for crap like GDPR, plus I personally believe in data privacy myself and don't want to become a vector of attack for other people.

Database Structire

https://dbdiagram.io/d/LinkFix-658b15e289dea6279992fb74

Screenshot 2023-12-31 at 2 43 45 AM

To-Do

  • Define schema
  • Add infrastructure to docker compose
  • Write /configure command to get and store settings
    • Add permission checks - admins only?
  • Write /forget command to wipe guild from database
    • Add permission checks - admins only?
  • Have replacements check their respective fields
  • Check if we need to suppress embeds on reply
  • Check if we need to mention the user on reply
  • Check if we need to delete the original message on reply
  • Write tests for all database logic

After we move into production:

  • Benchmark/optimize queries
  • Check for pool leaks

RalphORama and others added 14 commits December 26, 2023 12:45
- added postgres service to docker-compose
- started using Docker secrets to manage database password, bot token
- add barebones "configure" command to experiment with commands
- installed and configured pgtyped
realized I accidentally deleted .eslintrc with the previous commit so I
took some time to re-configure it to properly work with all the files in
our repo

- update .eslintrc.js
- add prettier config and ignore files
- update pre-commit configuration to parity with eslintrc and prettierrc
- move prettier and eslint to separate jobs within the same workflow
- use actions/setup-node's built in caching instead of a manual step
using .mts was a folley... instead, it appears we need to prepend import
statements in *.queries.ts files with

`// @ts-expect-error moduleResolution`
we were using this functionality for getting the Discord token, we will
also use this for getting the database password.
- make `id` columns properly autoincrement
- flesh out table relations
- add "pg" and "@types/pg" dependencies
I *think* this will allow for easy access to the database with the IP
`postgres.docker`
- dropped PgTyped and related files
- set up pg for raw Postgres queries
- catch Ctrl+C SIGTERM so we can cleanly shut down database connections
- add pgPool member to Discord Client instance
- update linter configs to reflect dropping PgTyped
- docker-compose: Use a shared array of environment variables in docker compose
- discord-bot: Run a test command on startup to check our database
@RalphORama RalphORama added enhancement New feature or request in progress Work in progress labels Dec 28, 2023
@RalphORama RalphORama added this to the v2.0.0 milestone Dec 28, 2023
@RalphORama RalphORama self-assigned this Dec 28, 2023
@RalphORama RalphORama marked this pull request as draft December 28, 2023 08:10
we're going to store all the database logic in src/bot/database. also
flesh out the CustomPool object to add event handlers.
- delete seed/index.ts (don't need to initialize db in software)
- rename seed/ to initdb/
- change initdb path in docker-compose
- use smaller postgres:16-alpine image in docker-compose
man oh man it's ugly as sin but I got some database crap working. next
step is to abstract db logic out into its own files to simplify
application command code.
makes merging in from main easier
@RalphORama RalphORama added the database PostgreSQL and all its glory label Dec 29, 2023
problems to solve:
  - abstract database logic out to src/bot/database
  - build paramaterized query strings with DEFAULT (cannot pass DEFAULT
    in a parameter... just pass same value we got back from the db?)
  - finally get around to parsing all those boolean options from the
    command
wholly crap lois

whole lotta bug testing I gotta do on this. also introduced some issues
with AppCommandsCLI but I should really break that out into its own
module anyway
@shoegaze
Copy link

shoegaze commented May 19, 2024

Add permission checks - admins only?

It's possible to set permissions for a command using SlashCommandBuilder#setDefaultMemberPermissions(): https://discordjs.guide/slash-commands/permissions.html#member-permissions

e.g.

new SlashCommandBuilder()
  .setName(...)
  // ...
  .setDefaultMemberPermissions(PermissionFlagsBits.Administrator)

should make the command admin-only.

@shoegaze
Copy link

shoegaze commented May 19, 2024

Should settings.guild be an FK (with REFERENCES guilds(id))?
Additionally, should it have ON DELETE CASCADE so that the settings rows are deleted when /forget is run? (Or should we keep the rows so that we can restore settings on /configure ?)

Also FYI: guilds.id is a bigint while settings.guild is an integer

@RalphORama RalphORama closed this Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
database PostgreSQL and all its glory enhancement New feature or request in progress Work in progress
Projects
None yet
2 participants