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 a0052a0 commit 68e87d5
Show file tree
Hide file tree
Showing 11 changed files with 21 additions and 30 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/on-push-pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ jobs:
with:
go-version: '1.21.x'

- name: psql schema
- name: pgsql schema
run: psql -h localhost -U nanodep -d nanodep -f ./storage/psql/schema.sql

- name: setup test dsn
Expand Down
4 changes: 2 additions & 2 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 @@ -37,7 +37,7 @@ func Storage(storageName, dsn, options string) (storage.AllStorage, error) {
case "mysql":
store, err = mysql.New(mysql.WithDSN(dsn))
case "psql":
store, err = psql.New(psql.WithDSN(dsn))
store, err = pgsql.New(pgsql.WithDSN(dsn))
default:
return nil, fmt.Errorf("unknown storage: %q", storageName)
}
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 68e87d5

Please sign in to comment.