Skip to content

Commit 885f5cc

Browse files
committed
use pgsql as the backend name
1 parent e468061 commit 885f5cc

12 files changed

+36
-34
lines changed

.github/workflows/on-push-pr.yml

+4-4
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ jobs:
127127

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

130-
psql-test:
130+
pgsql-test:
131131
runs-on: 'ubuntu-latest'
132132
needs: format-build-test
133133
services:
@@ -154,10 +154,10 @@ jobs:
154154
with:
155155
go-version: '1.21.x'
156156

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

160160
- name: setup test dsn
161161
run: echo "NANODEP_PSQL_STORAGE_TEST_DSN=postgres://nanodep:@localhost/nanodep?sslmode=disable" >> $GITHUB_ENV
162162

163-
- run: go test -v ./storage/psql
163+
- run: go test -v ./storage/pgsql

cli/storage.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
"github.com/micromdm/nanodep/storage/file"
1212
"github.com/micromdm/nanodep/storage/inmem"
1313
"github.com/micromdm/nanodep/storage/mysql"
14-
"github.com/micromdm/nanodep/storage/psql"
14+
"github.com/micromdm/nanodep/storage/pgsql"
1515
)
1616

1717
// 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) {
3636
store = inmem.New()
3737
case "mysql":
3838
store, err = mysql.New(mysql.WithDSN(dsn))
39-
case "psql":
40-
store, err = psql.New(psql.WithDSN(dsn))
39+
case "pgsql":
40+
store, err = pgsql.New(pgsql.WithDSN(dsn))
4141
default:
4242
return nil, fmt.Errorf("unknown storage: %q", storageName)
4343
}

docs/operations-guide.md

+11
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,17 @@ Configures the MySQL storage backend. The `-dsn` flag should be in the [format t
9090
9191
*Example:* `-storage mysql -dsn nanodep:nanodep/mydepdb`
9292

93+
##### pgsql storage backend
94+
95+
* `-storage pgsql`
96+
97+
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). PostgreSQL 9.5 or later is required.
98+
99+
> [!TIP]
100+
> Be sure to create the storage tables with the [schema.sql](../storage/pgsql/schema.sql) file.
101+
102+
*Example:* `-storage pgsql -storage-dsn postgres://postgres:toor@localhost:5432/nanodep`
103+
93104
#### -version
94105

95106
* print version

storage/pgsql/generate.go

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package pgsql
2+
3+
//go:generate sqlc generate

storage/psql/psql.go storage/pgsql/pgsql.go

+6-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package psql
1+
package pgsql
22

33
import (
44
"context"
@@ -11,7 +11,7 @@ import (
1111
_ "github.com/lib/pq"
1212
"github.com/micromdm/nanodep/client"
1313
"github.com/micromdm/nanodep/storage"
14-
"github.com/micromdm/nanodep/storage/psql/sqlc"
14+
"github.com/micromdm/nanodep/storage/pgsql/sqlc"
1515
)
1616

1717
// PSQL implements storage.AllStorage using PSQL.
@@ -72,8 +72,6 @@ func New(opts ...Option) (*PSQLStorage, error) {
7272

7373
}
7474

75-
const timestampFormat = "2006-01-02T15:04:05Z"
76-
7775
// RetrieveAuthTokens reads the DEP OAuth tokens for name (DEP name).
7876
func (s *PSQLStorage) RetrieveAuthTokens(ctx context.Context, name string) (*client.OAuth1Tokens, error) {
7977
tokenRow, err := s.q.GetAuthTokens(ctx, name)
@@ -86,17 +84,13 @@ func (s *PSQLStorage) RetrieveAuthTokens(ctx context.Context, name string) (*cli
8684
if !tokenRow.ConsumerKey.Valid { // all auth token fields are set together
8785
return nil, fmt.Errorf("consumer key not valid: %w", storage.ErrNotFound)
8886
}
89-
fmt.Println(tokenRow.AccessTokenExpiry.String)
90-
accessTokenExpiryTime, err := time.Parse(timestampFormat, tokenRow.AccessTokenExpiry.String)
91-
if err != nil {
92-
return nil, err
93-
}
87+
9488
return &client.OAuth1Tokens{
9589
ConsumerKey: tokenRow.ConsumerKey.String,
9690
ConsumerSecret: tokenRow.ConsumerSecret.String,
9791
AccessToken: tokenRow.AccessToken.String,
9892
AccessSecret: tokenRow.AccessSecret.String,
99-
AccessTokenExpiry: accessTokenExpiryTime,
93+
AccessTokenExpiry: tokenRow.AccessTokenExpiry.Time,
10094
}, nil
10195
}
10296

@@ -108,7 +102,7 @@ func (s *PSQLStorage) StoreAuthTokens(ctx context.Context, name string, tokens *
108102
ConsumerSecret: sql.NullString{String: tokens.ConsumerSecret, Valid: true},
109103
AccessToken: sql.NullString{String: tokens.AccessToken, Valid: true},
110104
AccessSecret: sql.NullString{String: tokens.AccessSecret, Valid: true},
111-
AccessTokenExpiry: sql.NullString{String: tokens.AccessTokenExpiry.Format(timestampFormat), Valid: true},
105+
AccessTokenExpiry: sql.NullTime{Time: tokens.AccessTokenExpiry, Valid: true},
112106
})
113107
}
114108

@@ -158,7 +152,7 @@ func (s *PSQLStorage) RetrieveAssignerProfile(ctx context.Context, name string)
158152
profileUUID = assignerRow.AssignerProfileUuid.String
159153
}
160154
if assignerRow.AssignerProfileUuidAt.Valid {
161-
modTime, err = time.Parse(timestampFormat, assignerRow.AssignerProfileUuidAt.String)
155+
modTime = assignerRow.AssignerProfileUuidAt.Time
162156
}
163157
return
164158
}

storage/psql/psql_test.go storage/pgsql/pgsql_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package psql
1+
package pgsql
22

33
import (
44
"context"
File renamed without changes.
File renamed without changes.

storage/psql/sqlc.yaml storage/pgsql/sqlc.yaml

-6
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,3 @@ sql:
2424
go_type:
2525
type: "byte"
2626
slice: true
27-
- column: "dep_names.access_token_expiry"
28-
go_type:
29-
type: "sql.NullString"
30-
- column: "dep_names.assigner_profile_uuid_at"
31-
go_type:
32-
type: "sql.NullString"

storage/psql/sqlc/db.go storage/pgsql/sqlc/db.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

storage/psql/sqlc/models.go storage/pgsql/sqlc/models.go

+3-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

storage/psql/sqlc/query.sql.go storage/pgsql/sqlc/query.sql.go

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)