diff options
author | GrailFinder <wohilas@gmail.com> | 2024-05-18 13:27:28 +0300 |
---|---|---|
committer | GrailFinder <wohilas@gmail.com> | 2024-05-18 13:27:28 +0300 |
commit | 2e9b18944eac3dcaf8a006594cb338d94c07a447 (patch) | |
tree | 7715140a6951407d13781e0a5bbf83a6e8f9dbe9 /internal/database/sql | |
parent | ff86222fc9ab85fb4c5c5e8a063083595b323761 (diff) |
Feat: auth; login; signup; migrate to sqlite
Diffstat (limited to 'internal/database/sql')
-rw-r--r-- | internal/database/sql/main.go | 69 |
1 files changed, 8 insertions, 61 deletions
diff --git a/internal/database/sql/main.go b/internal/database/sql/main.go index 80d5f5c..5a523f6 100644 --- a/internal/database/sql/main.go +++ b/internal/database/sql/main.go @@ -1,23 +1,20 @@ package database import ( - "apjournal/internal/database/migrations" "os" "time" - "github.com/jmoiron/sqlx" - - // driver postgres for migrations "log/slog" - "github.com/golang-migrate/migrate" - _ "github.com/golang-migrate/migrate/database/postgres" - bindata "github.com/golang-migrate/migrate/source/go_bindata" - _ "github.com/jackc/pgx/v5/stdlib" // register pgx driver + "github.com/jmoiron/sqlx" + _ "github.com/mattn/go-sqlite3" "github.com/pkg/errors" ) -var log = slog.New(slog.NewJSONHandler(os.Stdout, nil)) +var ( + log = slog.New(slog.NewJSONHandler(os.Stdout, nil)) + dbDriver = "sqlite3" +) type DB struct { Conn *sqlx.DB @@ -40,38 +37,17 @@ func closeConn(conn *sqlx.DB) error { func Init(DBURI string) (*DB, error) { var result DB var err error - result.Conn, err = openDBConnection(DBURI, "pgx") + result.Conn, err = openDBConnection(DBURI, dbDriver) if err != nil { return nil, err } result.URI = DBURI - if err := testConnection(result.Conn); err != nil { return nil, err } return &result, nil } -func InitWithMigrate(DBURI string, up bool) (*DB, error) { - var ( - result DB - err error - ) - result.Conn, err = openDBConnection(DBURI, "pgx") - if err != nil { - return nil, err - } - result.URI = DBURI - - if err = testConnection(result.Conn); err != nil { - return nil, err - } - if err = result.Migrate(DBURI, up); err != nil { - return nil, err - } - return &result, nil -} - func openDBConnection(dbURI, driver string) (*sqlx.DB, error) { conn, err := sqlx.Open(driver, dbURI) if err != nil { @@ -88,38 +64,9 @@ func testConnection(conn *sqlx.DB) error { return nil } -func (db *DB) Migrate(url string, up bool) error { - source := bindata.Resource(migrations.AssetNames(), migrations.Asset) - driver, err := bindata.WithInstance(source) - if err != nil { - return errors.WithStack(errors.WithMessage(err, - "unable to instantiate driver from bindata")) - } - migration, err := migrate.NewWithSourceInstance("go-bindata", - driver, url) - if err != nil { - return errors.WithStack(errors.WithMessage(err, - "unable to start migration")) - } - if up { - if err = migration.Up(); err != nil && err.Error() != "no change" { - return errors.WithStack(errors.WithMessage(err, - "unable to migrate up")) - } - } else { - if err = migration.Down(); err != nil && - err.Error() != "no change" { - return errors.WithStack(errors.WithMessage(err, - "unable to migrate down")) - } - } - return nil -} - func (d *DB) PingRoutine(interval time.Duration) { ticker := time.NewTicker(interval) done := make(chan bool) - for { select { case <-done: @@ -131,7 +78,7 @@ func (d *DB) PingRoutine(interval time.Duration) { if err := closeConn(d.Conn); err != nil { log.Error("failed to close db connection", "error", err, "ping_at", t) } - d.Conn, err = openDBConnection(d.URI, "pgx") + d.Conn, err = openDBConnection(d.URI, dbDriver) if err != nil { log.Error("failed to reconnect", "error", err, "ping_at", t) } |