From f420c3989f962e54cadc3385ee710fce04059de5 Mon Sep 17 00:00:00 2001 From: Nicholas Molnar <65710+neekolas@users.noreply.github.com> Date: Thu, 25 Apr 2024 12:28:09 -0700 Subject: [PATCH] Migrate MLS DB To SQLC (#380) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bun has been bothering me for a while, so I've been working on a migration that will get rid of our ORM altogether and just use boring SQL queries for everything. [`sqlc`](https://sqlc.dev/) is a very slick tool to generate code based on plain SQL queries using placeholders for arguments. It's not perfect...I had to do some gymnastics to make a few of the query types work. But the fact that there is no runtime other than the standard SQL driver and some generated code outweighs its limitations IMO. There's no fancy ORM library to worry about mangling your queries, and the learning curve is basically just "how well do you know SQL". - No support for serializable transactions - The SQL driver is not as well maintained as PGX - High learning curve to build complex queries, even if you know SQL well - Relations system is not very powerful and ends up doing N+1 queries a lot of the time. - Configuring the database with struct tags is errorprone, and there aren't great checks to make sure the struct tags actually match the schema. - I can't find a good way to have dynamic ORDER BY expressions. So I literally have separate queries for ASC and DESC versions. It's not the end of the world, but it's very frustrating. There's an [issue to fix it](https://github.com/sqlc-dev/sqlc/issues/2061), and some hacky workarounds using CASE statements, but it's not great. - Making the filters play nice with `json_populate_recordset` is a bit of a pain. Switching to the `pgx` driver helped, since I think there was a bug in Bun's pgdriver. We use Bun in a lot of places and for a lot of things today. - It powers the `authz` database and all the migrations there - It powers the migrations for the `message` database (but not the queries) - It powers the `mls` database and all the queries in the `mlsstore`. The priority right now is to remove it from the `mlsstore`. We will still use it for migrations (`sqlc` can read Bun migrations just fine). This involves replacing the bun `pgdriver` with `pgx` (done in this PR) and replacing all the Bun ORM queries with `sqlc` queries. I have most of the queries written, but I'll split up the actual migration over several PRs. This can be done incrementally, but once the process is complete we can delete the Bun models. We aren't using any of the fancy `sqlc` cloud features and have no plans to. Ummmm. 😬. That was me. --- pkg/mls/store/queries/queries.sql.go | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/pkg/mls/store/queries/queries.sql.go b/pkg/mls/store/queries/queries.sql.go index c9556056..337eebd8 100644 --- a/pkg/mls/store/queries/queries.sql.go +++ b/pkg/mls/store/queries/queries.sql.go @@ -9,8 +9,6 @@ import ( "context" "database/sql" "encoding/json" - - "github.com/lib/pq" ) const createInstallation = `-- name: CreateInstallation :exec @@ -41,7 +39,7 @@ func (q *Queries) CreateInstallation(ctx context.Context, arg CreateInstallation const fetchKeyPackages = `-- name: FetchKeyPackages :many SELECT id, key_package FROM installations -WHERE id = ANY ($1::bytea[]) +WHERE ID IN ($1) ` type FetchKeyPackagesRow struct { @@ -49,8 +47,8 @@ type FetchKeyPackagesRow struct { KeyPackage []byte } -func (q *Queries) FetchKeyPackages(ctx context.Context, installationIds [][]byte) ([]FetchKeyPackagesRow, error) { - rows, err := q.db.QueryContext(ctx, fetchKeyPackages, pq.Array(installationIds)) +func (q *Queries) FetchKeyPackages(ctx context.Context, ids []byte) ([]FetchKeyPackagesRow, error) { + rows, err := q.db.QueryContext(ctx, fetchKeyPackages, ids) if err != nil { return nil, err } @@ -108,18 +106,18 @@ func (q *Queries) GetAllInboxLogs(ctx context.Context, inboxID string) ([]InboxL const getIdentityUpdates = `-- name: GetIdentityUpdates :many SELECT id, wallet_address, created_at, updated_at, credential_identity, revoked_at, key_package, expiration FROM installations -WHERE wallet_address = ANY ($1::text[]) +WHERE wallet_address IN ($1) AND (created_at > $2 OR revoked_at > $2) ORDER BY created_at ASC ` type GetIdentityUpdatesParams struct { - WalletAddresses []string + WalletAddresses string StartTime int64 } func (q *Queries) GetIdentityUpdates(ctx context.Context, arg GetIdentityUpdatesParams) ([]Installation, error) { - rows, err := q.db.QueryContext(ctx, getIdentityUpdates, pq.Array(arg.WalletAddresses), arg.StartTime) + rows, err := q.db.QueryContext(ctx, getIdentityUpdates, arg.WalletAddresses, arg.StartTime) if err != nil { return nil, err }