From 8be1e34e68e8de20e5ab11bf8dba3a6917823511 Mon Sep 17 00:00:00 2001 From: Nicholas Murray Date: Sat, 14 Oct 2023 10:52:17 -0700 Subject: [PATCH] backend/pkg/database: add postgres package and postgres specific db instantiation --- backend/pkg/database/postgres_repository.go | 40 +++------------------ go.mod | 9 ++++- go.sum | 8 +++++ 3 files changed, 20 insertions(+), 37 deletions(-) diff --git a/backend/pkg/database/postgres_repository.go b/backend/pkg/database/postgres_repository.go index dd384441..e440850e 100644 --- a/backend/pkg/database/postgres_repository.go +++ b/backend/pkg/database/postgres_repository.go @@ -8,8 +8,8 @@ import ( "github.com/fastenhealth/fasten-onprem/backend/pkg/event_bus" "github.com/fastenhealth/fasten-onprem/backend/pkg/models" databaseModel "github.com/fastenhealth/fasten-onprem/backend/pkg/models/database" - "github.com/glebarez/sqlite" "github.com/sirupsen/logrus" + "gorm.io/driver/postgres" "gorm.io/gorm" ) @@ -19,33 +19,10 @@ func newPostgresRepository(appConfig config.Interface, globalLogger logrus.Field //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // Gorm/PostgreSQL setup //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// - globalLogger.Infof("Trying to connect to sqlite db: %s\n", appConfig.GetString("database.location")) - - // BUSY TIMEOUT SETTING DOCS --- - // When a transaction cannot lock the database, because it is already locked by another one, - // SQLite by default throws an error: database is locked. This behavior is usually not appropriate when - // concurrent access is needed, typically when multiple processes write to the same database. - // PRAGMA busy_timeout lets you set a timeout or a handler for these events. When setting a timeout, - // SQLite will try the transaction multiple times within this timeout. - // fixes #341 - // https://rsqlite.r-dbi.org/reference/sqlitesetbusyhandler - // retrying for 30000 milliseconds, 30seconds - this would be unreasonable for a distributed multi-tenant application, - // but should be fine for local usage. - // - // JOURNAL MODE WAL DOCS --- - // - // Write-Ahead Logging or WAL (New Way) - // In this case all writes are appended to a temporary file (write-ahead log) and this file is periodically merged with the original database. When SQLite is searching for something it would first check this temporary file and if nothing is found proceed with the main database file. - // As a result, readers don’t compete with writers and performance is much better compared to the Old Way. - // https://stackoverflow.com/questions/4060772/sqlite-concurrent-access - // pragmaStr := sqlitePragmaString(map[string]string{ - // "busy_timeout": "5000", - // "foreign_keys": "ON", - // "journal_mode": "wal", - // }) - // dsn := "file:" + appConfig.GetString("database.location") + pragmaStr + globalLogger.Infof("Trying to connect to postgres db: %s\n", appConfig.GetString("database.location")) dsn := appConfig.GetString("database.location") - database, err := gorm.Open(sqlite.Open(dsn), &gorm.Config{ + + database, err := gorm.Open(postgres.Open(dsn), &gorm.Config{ //TODO: figure out how to log database queries again. //logger: logger DisableForeignKeyConstraintWhenMigrating: true, @@ -60,15 +37,6 @@ func newPostgresRepository(appConfig config.Interface, globalLogger logrus.Field } globalLogger.Infof("Successfully connected to fasten postgres db: %s\n", dsn) - ////verify journal mode - //var journalMode []map[string]interface{} - //resp := database.Raw("PRAGMA journal_mode;").Scan(&journalMode) - //if resp.Error != nil { - // return nil, fmt.Errorf("Failed to verify journal mode! - %v", resp.Error) - //} else { - // globalLogger.Infof("Journal mode: %v", journalMode) - //} - fastenRepo := GormRepository{ AppConfig: appConfig, Logger: globalLogger, diff --git a/go.mod b/go.mod index e26176df..e2669b1c 100644 --- a/go.mod +++ b/go.mod @@ -27,7 +27,13 @@ require ( golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 golang.org/x/net v0.14.0 gorm.io/datatypes v1.0.7 - gorm.io/gorm v1.24.1 + gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55 +) + +require ( + github.com/jackc/pgpassfile v1.0.0 // indirect + github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect + github.com/jackc/pgx/v5 v5.4.3 // indirect ) require ( @@ -104,6 +110,7 @@ require ( gopkg.in/yaml.v2 v2.4.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect gorm.io/driver/mysql v1.3.2 // indirect + gorm.io/driver/postgres v1.5.3 modernc.org/libc v1.19.0 // indirect modernc.org/mathutil v1.5.0 // indirect modernc.org/memory v1.4.0 // indirect diff --git a/go.sum b/go.sum index 2c26aa58..b143f585 100644 --- a/go.sum +++ b/go.sum @@ -470,6 +470,8 @@ github.com/jackc/pgproto3/v2 v2.2.0 h1:r7JypeP2D3onoQTCxWdTpCtJ4D+qpKr0TxvoyMhZ5 github.com/jackc/pgproto3/v2 v2.2.0/go.mod h1:WfJCnwN3HIg9Ish/j3sgWXnAfK8A9Y0bwXYU5xKaEdA= github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b h1:C8S2+VttkHFdOOCXJe+YGfa4vHYwlt4Zx+IVXQ97jYg= github.com/jackc/pgservicefile v0.0.0-20200714003250-2b9c44734f2b/go.mod h1:vsD4gTJCa9TptPL8sPkXrLZ+hDuNrZCnj29CQpr4X1E= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk= +github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= github.com/jackc/pgtype v0.0.0-20190421001408-4ed0de4755e0/go.mod h1:hdSHsc1V01CGwFsrv11mJRHWJ6aifDLfdV3aVjFF0zg= github.com/jackc/pgtype v0.0.0-20190824184912-ab885b375b90/go.mod h1:KcahbBH1nCMSo2DXpzsoWOAfFkdEtEJpPbVLq8eE+mc= github.com/jackc/pgtype v0.0.0-20190828014616-a8802b16cc59/go.mod h1:MWlu30kVJrUS8lot6TQqcg7mtthZ9T0EoIBFiJcmcyw= @@ -482,6 +484,8 @@ github.com/jackc/pgx/v4 v4.0.0-pre1.0.20190824185557-6972a5742186/go.mod h1:X+GQ github.com/jackc/pgx/v4 v4.12.1-0.20210724153913-640aa07df17c/go.mod h1:1QD0+tgSXP7iUjYm9C1NxKhny7lq6ee99u/z+IHFcgs= github.com/jackc/pgx/v4 v4.15.0 h1:B7dTkXsdILD3MF987WGGCcg+tvLW6bZJdEcqVFeU//w= github.com/jackc/pgx/v4 v4.15.0/go.mod h1:D/zyOyXiaM1TmVWnOM18p0xdDtdakRBa0RsVGI3U3bw= +github.com/jackc/pgx/v5 v5.4.3 h1:cxFyXhxlvAifxnkKKdlxv8XqUf59tDlYjnV5YYfsJJY= +github.com/jackc/pgx/v5 v5.4.3/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA= github.com/jackc/puddle v0.0.0-20190413234325-e4ced69a3a2b/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v0.0.0-20190608224051-11cab39313c9/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= github.com/jackc/puddle v1.1.3/go.mod h1:m4B5Dj62Y0fbyuIc15OsIqK0+JU8nkqQjsgx7dvjSWk= @@ -1286,6 +1290,8 @@ gorm.io/driver/mysql v1.3.2 h1:QJryWiqQ91EvZ0jZL48NOpdlPdMjdip1hQ8bTgo4H7I= gorm.io/driver/mysql v1.3.2/go.mod h1:ChK6AHbHgDCFZyJp0F+BmVGb06PSIoh9uVYKAlRbb2U= gorm.io/driver/postgres v1.3.4 h1:evZ7plF+Bp+Lr1mO5NdPvd6M/N98XtwHixGB+y7fdEQ= gorm.io/driver/postgres v1.3.4/go.mod h1:y0vEuInFKJtijuSGu9e5bs5hzzSzPK+LancpKpvbRBw= +gorm.io/driver/postgres v1.5.3 h1:qKGY5CPHOuj47K/VxbCXJfFvIUeqMSXXadqdCY+MbBU= +gorm.io/driver/postgres v1.5.3/go.mod h1:F+LtvlFhZT7UBiA81mC9W6Su3D4WUhSboc/36QZU0gk= gorm.io/driver/sqlite v1.3.1 h1:bwfE+zTEWklBYoEodIOIBwuWHpnx52Z9zJFW5F33WLk= gorm.io/driver/sqlite v1.3.1/go.mod h1:wJx0hJspfycZ6myN38x1O/AqLtNS6c5o9TndewFbELg= gorm.io/driver/sqlserver v1.3.1 h1:F5t6ScMzOgy1zukRTIZgLZwKahgt3q1woAILVolKpOI= @@ -1295,6 +1301,8 @@ gorm.io/gorm v1.23.6/go.mod h1:l2lP/RyAtc1ynaTjFksBde/O8v9oOGIApu2/xRitmZk= gorm.io/gorm v1.24.0/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA= gorm.io/gorm v1.24.1 h1:CgvzRniUdG67hBAzsxDGOAuq4Te1osVMYsa1eQbd4fs= gorm.io/gorm v1.24.1/go.mod h1:DVrVomtaYTbqs7gB/x2uVvqnXzv0nqjB396B8cG4dBA= +gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55 h1:sC1Xj4TYrLqg1n3AN10w871An7wJM0gzgcm8jkIkECQ= +gorm.io/gorm v1.25.2-0.20230530020048-26663ab9bf55/go.mod h1:L4uxeKpfBml98NYqVqwAdmV1a2nBtAec/cf3fpucW/k= gotest.tools v2.2.0+incompatible/go.mod h1:DsYFclhRJ6vuDpmuTbkuFWG+y2sxOXAzmJt81HFBacw= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4=