From 67bf72cfd4c80ad7ea07957a9390d7002cfabef5 Mon Sep 17 00:00:00 2001 From: David John Date: Wed, 13 Nov 2024 10:09:45 +0530 Subject: [PATCH] use pgsql as the backend name --- .github/workflows/on-push-pr.yml | 8 ++++---- cli/storage.go | 6 +++--- docs/operations-guide.md | 8 ++++++++ storage/pgsql/generate.go | 3 +++ storage/{psql/psql.go => pgsql/pgsql.go} | 18 ++++++------------ .../{psql/psql_test.go => pgsql/pgsql_test.go} | 2 +- storage/{psql => pgsql}/query.sql | 0 storage/{psql => pgsql}/schema.sql | 0 storage/{psql => pgsql}/sqlc.yaml | 6 ------ storage/{psql => pgsql}/sqlc/db.go | 2 +- storage/{psql => pgsql}/sqlc/models.go | 6 +++--- storage/{psql => pgsql}/sqlc/query.sql.go | 8 ++++---- 12 files changed, 33 insertions(+), 34 deletions(-) create mode 100644 storage/pgsql/generate.go rename storage/{psql/psql.go => pgsql/pgsql.go} (93%) rename storage/{psql/psql_test.go => pgsql/pgsql_test.go} (96%) rename storage/{psql => pgsql}/query.sql (100%) rename storage/{psql => pgsql}/schema.sql (100%) rename storage/{psql => pgsql}/sqlc.yaml (75%) rename storage/{psql => pgsql}/sqlc/db.go (96%) rename storage/{psql => pgsql}/sqlc/models.go (86%) rename storage/{psql => pgsql}/sqlc/query.sql.go (98%) diff --git a/.github/workflows/on-push-pr.yml b/.github/workflows/on-push-pr.yml index 0e3f0c5..c77fda5 100644 --- a/.github/workflows/on-push-pr.yml +++ b/.github/workflows/on-push-pr.yml @@ -127,7 +127,7 @@ jobs: - run: go test -v ./storage/mysql - psql-test: + pgsql-test: runs-on: 'ubuntu-latest' needs: format-build-test services: @@ -154,10 +154,10 @@ jobs: with: go-version: '1.21.x' - - name: psql schema - run: psql -h localhost -U nanodep -d nanodep -f ./storage/psql/schema.sql + - name: pgsql schema + run: psql -h localhost -U nanodep -d nanodep -f ./storage/pgsql/schema.sql - name: setup test dsn run: echo "NANODEP_PSQL_STORAGE_TEST_DSN=postgres://nanodep:@localhost/nanodep?sslmode=disable" >> $GITHUB_ENV - - run: go test -v ./storage/psql + - run: go test -v ./storage/pgsql diff --git a/cli/storage.go b/cli/storage.go index 484bc2e..8943053 100644 --- a/cli/storage.go +++ b/cli/storage.go @@ -11,7 +11,7 @@ import ( "github.com/micromdm/nanodep/storage/file" "github.com/micromdm/nanodep/storage/inmem" "github.com/micromdm/nanodep/storage/mysql" - "github.com/micromdm/nanodep/storage/psql" + "github.com/micromdm/nanodep/storage/pgsql" ) // Storage parses a storage name and dsn to determine which and return a storage backend. @@ -36,8 +36,8 @@ func Storage(storageName, dsn, options string) (storage.AllStorage, error) { store = inmem.New() case "mysql": store, err = mysql.New(mysql.WithDSN(dsn)) - case "psql": - store, err = psql.New(psql.WithDSN(dsn)) + case "pgsql": + store, err = pgsql.New(pgsql.WithDSN(dsn)) default: return nil, fmt.Errorf("unknown storage: %q", storageName) } diff --git a/docs/operations-guide.md b/docs/operations-guide.md index 44946ca..9929dff 100644 --- a/docs/operations-guide.md +++ b/docs/operations-guide.md @@ -90,6 +90,14 @@ Configures the MySQL storage backend. The `-dsn` flag should be in the [format t *Example:* `-storage mysql -dsn nanodep:nanodep/mydepdb` +##### pgsql storage backend + +* `-storage pgsql` + +Configures the PostgreSQL storage backend. The `-storage-dsn` flag should be in the [format the SQL driver expects](https://pkg.go.dev/github.com/lib/pq#pkg-overview). Be sure to create your tables with the [schema.sql](../storage/pgsql/schema.sql) file that corresponds to your NanoDEP version. Also make sure you apply any schema changes for each updated version (i.e. execute the numbered schema change files). PostgreSQL 9.5 or later is required. + +*Example*: -storage pgsql -storage-dsn postgres://postgres:toor@localhost:5432/nanodep + #### -version * print version diff --git a/storage/pgsql/generate.go b/storage/pgsql/generate.go new file mode 100644 index 0000000..f41591e --- /dev/null +++ b/storage/pgsql/generate.go @@ -0,0 +1,3 @@ +package pgsql + +//go:generate sqlc generate diff --git a/storage/psql/psql.go b/storage/pgsql/pgsql.go similarity index 93% rename from storage/psql/psql.go rename to storage/pgsql/pgsql.go index 62ac2bd..da0ed7c 100644 --- a/storage/psql/psql.go +++ b/storage/pgsql/pgsql.go @@ -1,4 +1,4 @@ -package psql +package pgsql import ( "context" @@ -11,7 +11,7 @@ import ( _ "github.com/lib/pq" "github.com/micromdm/nanodep/client" "github.com/micromdm/nanodep/storage" - "github.com/micromdm/nanodep/storage/psql/sqlc" + "github.com/micromdm/nanodep/storage/pgsql/sqlc" ) // PSQL implements storage.AllStorage using PSQL. @@ -72,8 +72,6 @@ func New(opts ...Option) (*PSQLStorage, error) { } -const timestampFormat = "2006-01-02T15:04:05Z" - // RetrieveAuthTokens reads the DEP OAuth tokens for name (DEP name). func (s *PSQLStorage) RetrieveAuthTokens(ctx context.Context, name string) (*client.OAuth1Tokens, error) { tokenRow, err := s.q.GetAuthTokens(ctx, name) @@ -86,17 +84,13 @@ func (s *PSQLStorage) RetrieveAuthTokens(ctx context.Context, name string) (*cli if !tokenRow.ConsumerKey.Valid { // all auth token fields are set together return nil, fmt.Errorf("consumer key not valid: %w", storage.ErrNotFound) } - fmt.Println(tokenRow.AccessTokenExpiry.String) - accessTokenExpiryTime, err := time.Parse(timestampFormat, tokenRow.AccessTokenExpiry.String) - if err != nil { - return nil, err - } + return &client.OAuth1Tokens{ ConsumerKey: tokenRow.ConsumerKey.String, ConsumerSecret: tokenRow.ConsumerSecret.String, AccessToken: tokenRow.AccessToken.String, AccessSecret: tokenRow.AccessSecret.String, - AccessTokenExpiry: accessTokenExpiryTime, + AccessTokenExpiry: tokenRow.AccessTokenExpiry.Time, }, nil } @@ -108,7 +102,7 @@ func (s *PSQLStorage) StoreAuthTokens(ctx context.Context, name string, tokens * ConsumerSecret: sql.NullString{String: tokens.ConsumerSecret, Valid: true}, AccessToken: sql.NullString{String: tokens.AccessToken, Valid: true}, AccessSecret: sql.NullString{String: tokens.AccessSecret, Valid: true}, - AccessTokenExpiry: sql.NullString{String: tokens.AccessTokenExpiry.Format(timestampFormat), Valid: true}, + AccessTokenExpiry: sql.NullTime{Time: tokens.AccessTokenExpiry, Valid: true}, }) } @@ -158,7 +152,7 @@ func (s *PSQLStorage) RetrieveAssignerProfile(ctx context.Context, name string) profileUUID = assignerRow.AssignerProfileUuid.String } if assignerRow.AssignerProfileUuidAt.Valid { - modTime, err = time.Parse(timestampFormat, assignerRow.AssignerProfileUuidAt.String) + modTime = assignerRow.AssignerProfileUuidAt.Time } return } diff --git a/storage/psql/psql_test.go b/storage/pgsql/pgsql_test.go similarity index 96% rename from storage/psql/psql_test.go rename to storage/pgsql/pgsql_test.go index 2cefac8..f4a7829 100644 --- a/storage/psql/psql_test.go +++ b/storage/pgsql/pgsql_test.go @@ -1,4 +1,4 @@ -package psql +package pgsql import ( "context" diff --git a/storage/psql/query.sql b/storage/pgsql/query.sql similarity index 100% rename from storage/psql/query.sql rename to storage/pgsql/query.sql diff --git a/storage/psql/schema.sql b/storage/pgsql/schema.sql similarity index 100% rename from storage/psql/schema.sql rename to storage/pgsql/schema.sql diff --git a/storage/psql/sqlc.yaml b/storage/pgsql/sqlc.yaml similarity index 75% rename from storage/psql/sqlc.yaml rename to storage/pgsql/sqlc.yaml index 4dbc2de..334bf67 100644 --- a/storage/psql/sqlc.yaml +++ b/storage/pgsql/sqlc.yaml @@ -24,9 +24,3 @@ sql: go_type: type: "byte" slice: true - - column: "dep_names.access_token_expiry" - go_type: - type: "sql.NullString" - - column: "dep_names.assigner_profile_uuid_at" - go_type: - type: "sql.NullString" diff --git a/storage/psql/sqlc/db.go b/storage/pgsql/sqlc/db.go similarity index 96% rename from storage/psql/sqlc/db.go rename to storage/pgsql/sqlc/db.go index 2248616..c5852e0 100644 --- a/storage/psql/sqlc/db.go +++ b/storage/pgsql/sqlc/db.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.27.0 +// sqlc v1.26.0 package sqlc diff --git a/storage/psql/sqlc/models.go b/storage/pgsql/sqlc/models.go similarity index 86% rename from storage/psql/sqlc/models.go rename to storage/pgsql/sqlc/models.go index a5669e4..b634a2d 100644 --- a/storage/psql/sqlc/models.go +++ b/storage/pgsql/sqlc/models.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.27.0 +// sqlc v1.26.0 package sqlc @@ -14,7 +14,7 @@ type DepName struct { ConsumerSecret sql.NullString AccessToken sql.NullString AccessSecret sql.NullString - AccessTokenExpiry sql.NullString + AccessTokenExpiry sql.NullTime ConfigBaseUrl sql.NullString TokenpkiCertPem []byte TokenpkiKeyPem []byte @@ -22,7 +22,7 @@ type DepName struct { TokenpkiStagingKeyPem []byte SyncerCursor sql.NullString AssignerProfileUuid sql.NullString - AssignerProfileUuidAt sql.NullString + AssignerProfileUuidAt sql.NullTime CreatedAt sql.NullTime UpdatedAt sql.NullTime } diff --git a/storage/psql/sqlc/query.sql.go b/storage/pgsql/sqlc/query.sql.go similarity index 98% rename from storage/psql/sqlc/query.sql.go rename to storage/pgsql/sqlc/query.sql.go index 20d369a..0632f5d 100644 --- a/storage/psql/sqlc/query.sql.go +++ b/storage/pgsql/sqlc/query.sql.go @@ -1,6 +1,6 @@ // Code generated by sqlc. DO NOT EDIT. // versions: -// sqlc v1.27.0 +// sqlc v1.26.0 // source: query.sql package sqlc @@ -22,7 +22,7 @@ WHERE type GetAssignerProfileRow struct { AssignerProfileUuid sql.NullString - AssignerProfileUuidAt sql.NullString + AssignerProfileUuidAt sql.NullTime } func (q *Queries) GetAssignerProfile(ctx context.Context, name string) (GetAssignerProfileRow, error) { @@ -50,7 +50,7 @@ type GetAuthTokensRow struct { ConsumerSecret sql.NullString AccessToken sql.NullString AccessSecret sql.NullString - AccessTokenExpiry sql.NullString + AccessTokenExpiry sql.NullTime } func (q *Queries) GetAuthTokens(ctx context.Context, name string) (GetAuthTokensRow, error) { @@ -174,7 +174,7 @@ type StoreAuthTokensParams struct { ConsumerSecret sql.NullString AccessToken sql.NullString AccessSecret sql.NullString - AccessTokenExpiry sql.NullString + AccessTokenExpiry sql.NullTime } func (q *Queries) StoreAuthTokens(ctx context.Context, arg StoreAuthTokensParams) error {