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

feat: PostgreSQL support #922

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft

feat: PostgreSQL support #922

wants to merge 17 commits into from

Conversation

rdmitr
Copy link
Collaborator

@rdmitr rdmitr commented Dec 24, 2024

TODOs:

  • fix timestamps
  • github workflows should run tests on both DB types
  • how do existing cloud users migrate from sqlite to postgres? -> run migration from getalby.com as part of update process?
  • what changes do we need to make in the backup & restore process? disable migrations for now if postgres is enabled?
    • cloud -> self-hosted
    • self-hosted -> cloud
  • Roland's testing
    • brand new postgres DB
    • brand new sqlite DB
    • existing sqlite DB
    • migrate existing sqlite DB
    • migrate postgres DB to sqlite

db/db.go Show resolved Hide resolved
@rolznz
Copy link
Contributor

rolznz commented Jan 8, 2025

@rdmitr do we need to update our github workflows to also test in Postgres mode? ideally we test in both modes

@rdmitr
Copy link
Collaborator Author

rdmitr commented Jan 9, 2025

@rdmitr do we need to update our github workflows to also test in Postgres mode? ideally we test in both modes

Absolutely! I shall figure it out

db/db.go Show resolved Hide resolved
@@ -16,7 +16,7 @@ var _202405302121_store_decrypted_request = &gormigrate.Migration{
return err
}

err = tx.Exec("CREATE INDEX `idx_request_events_method` ON `request_events`(`method`);").Error
err = tx.Exec("CREATE INDEX \"idx_request_events_method\" ON \"request_events\"(\"method\");").Error
Copy link
Contributor

@rolznz rolznz Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rdmitr would it be better to be consistent in the format here?

The migration above uses ` and '

This migration escapes using \

@@ -19,8 +20,11 @@ var _202406061259_delete_content = &gormigrate.Migration{
return err
}

if err := tx.Exec("VACUUM").Error; err != nil {
return err
// Cannot run when testing with txdb: VACUUM must be run outside of transaction.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we probably don't need this migration any more. Could we just comment out the content?

if err := tx.Exec("VACUUM").Error; err != nil {
return err
// Cannot run when testing with txdb: VACUUM must be run outside of transaction.
if !testing.Testing() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we probably don't need this migration any more. Could we just comment out the content?

Migrate: func(tx *gorm.DB) error {
if tx.Dialector.Name() == "postgres" {
err := tx.Exec(`
CREATE SEQUENCE user_configs_id_seq;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add comment: // due to not explicitly setting the user_configs table to use auto increment in previous migrations


var txSerializer = make(chan struct{}, 1)

func RunTransaction(db *gorm.DB, txFunc func(tx *gorm.DB) error) error {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I saw only some instances of db.Transaction have been updated to use this. Is that intentional?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels a bit hacky if it is done just for tests, is there a way we can isolate the changes to the test DB instead?

@@ -0,0 +1,124 @@
package main
Copy link
Contributor

@rolznz rolznz Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just thinking of the filename migrate we have different types of migrations. Could we be more clear e.g. db_migrate or something? (or cmd/db/migrate)

return fmt.Errorf("failed to start transaction: %w", err)
}

// Table migration order matters: referenced tables must be migrated
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we fail the migration if the number of tables in the DB are not expected (e.g. we made a new table and forgot to update this migrate command?)

return nil
}

if err := to.Create(data).Error; err != nil {
Copy link
Contributor

@rolznz rolznz Jan 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I got an error when I tried to create a new app after migrating from sqlite to postgres. It says duplicate ID not allowed.

My current apps:

 id | name | description |                            app_pubkey                            |          
created_at          |          updated_at           | isolated | metadata |                          wa
llet_pubkey                           
----+------+-------------+------------------------------------------------------------------+------------------------------+-------------------------------+----------+----------+------------------------------------------------------------------
  1 | test |             | a0aa432130518b7bf921e6d55cce725f172693196fa1b8b8b103f2bcd2225c87 | 2025-01-10 15:29:08.26065+07 | 2025-01-10 15:29:08.268216+07 | f        | {}       | 19c16a92888d3ebbb594fa7fa814886b9bfd014395ca9e09f98165010ec01dd9
(1 row)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants