Skip to content

Commit

Permalink
use pgsql as the backend name
Browse files Browse the repository at this point in the history
  • Loading branch information
radiohertz committed Nov 13, 2024
1 parent f9b558d commit cb71b54
Show file tree
Hide file tree
Showing 12 changed files with 31 additions and 34 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/on-push-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ jobs:

- run: go test -v ./storage/mysql

psql-test:
pgsql-test:
runs-on: 'ubuntu-latest'
needs: format-build-test
services:
Expand All @@ -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
6 changes: 3 additions & 3 deletions cli/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
}
Expand Down
6 changes: 6 additions & 0 deletions docs/operations-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ Be sure to create the storage tables with the [schema.sql](../storage/mysql/sche

*Example:* `-storage mysql -dsn nanodep:nanodep/mydepdb`

* `-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
Expand Down
3 changes: 3 additions & 0 deletions storage/pgsql/generate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package pgsql

//go:generate sqlc generate
18 changes: 6 additions & 12 deletions storage/psql/psql.go → storage/pgsql/pgsql.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package psql
package pgsql

import (
"context"
Expand All @@ -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.
Expand Down Expand Up @@ -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)
Expand All @@ -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
}

Expand All @@ -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},
})
}

Expand Down Expand Up @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion storage/psql/psql_test.go → storage/pgsql/pgsql_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package psql
package pgsql

import (
"context"
Expand Down
File renamed without changes.
File renamed without changes.
6 changes: 0 additions & 6 deletions storage/psql/sqlc.yaml → storage/pgsql/sqlc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
2 changes: 1 addition & 1 deletion storage/psql/sqlc/db.go → storage/pgsql/sqlc/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions storage/psql/sqlc/models.go → storage/pgsql/sqlc/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit cb71b54

Please sign in to comment.