Skip to content

Commit e0250a0

Browse files
authored
Merge pull request #4 from storage-lock/dev
修改为使用SqlBasedStorage
2 parents 530ae30 + fb4fa76 commit e0250a0

File tree

9 files changed

+47
-15
lines changed

9 files changed

+47
-15
lines changed

errors.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package postgresql_storage
2+
3+
import "errors"
4+
5+
var (
6+
ErrQueryPostgresqlServerTime = errors.New("query postgresql server time failed")
7+
)

go.mod

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,20 @@ go 1.19
44

55
require (
66
github.com/lib/pq v1.10.9
7-
github.com/storage-lock/go-sql-based-storage v0.0.0-20230815152553-e6057f9c32e7
7+
github.com/storage-lock/go-sql-based-storage v0.0.0-20230903155403-1c8c724d3383
88
github.com/storage-lock/go-storage v0.0.2
9-
github.com/storage-lock/go-storage-test-helper v0.0.2
9+
github.com/storage-lock/go-storage-test-helper v0.0.3
1010
github.com/stretchr/testify v1.8.4
1111
)
1212

1313
require (
1414
github.com/davecgh/go-spew v1.1.1 // indirect
1515
github.com/golang-infrastructure/go-goroutine-id v0.0.0-20230331174358-98b48a64077b // indirect
16+
github.com/golang-infrastructure/go-if-expression v0.0.3 // indirect
1617
github.com/golang-infrastructure/go-iterator v0.0.0-20230524171120-56988a9b127c // indirect
1718
github.com/golang-infrastructure/go-pointer v0.0.5 // indirect
1819
github.com/golang-infrastructure/go-reflect-utils v0.0.0-20221130143747-965ef2eb09c3 // indirect
19-
github.com/google/uuid v1.3.0 // indirect
20+
github.com/google/uuid v1.3.1 // indirect
2021
github.com/pmezard/go-difflib v1.0.0 // indirect
2122
github.com/storage-lock/go-events v0.0.3 // indirect
2223
github.com/storage-lock/go-storage-events v0.0.5 // indirect

go.sum

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/golang-infrastructure/go-goroutine-id v0.0.0-20230331174358-98b48a64077b h1:kt3zNiijgkfPm47YaL4MaQJkw0T7ukEEttnCQMOcWxo=
44
github.com/golang-infrastructure/go-goroutine-id v0.0.0-20230331174358-98b48a64077b/go.mod h1:1L+eneS0HXUlZP0tmESGhlmpJ3hl063JGz6UQaJCmY0=
5+
github.com/golang-infrastructure/go-if-expression v0.0.3 h1:Ic6L/s250nwtQz8CtTFpkWopCwsWmxQErSRkrBqBVj4=
6+
github.com/golang-infrastructure/go-if-expression v0.0.3/go.mod h1:U4ZtU29/RtZ/cM0tAdBzFaFxENFShSFj+jCRFRE/cv4=
57
github.com/golang-infrastructure/go-iterator v0.0.0-20230524171120-56988a9b127c h1:h4321AFRVv5rTKcColUjE5pa+Y1exG1e6aEXn43L/Eg=
68
github.com/golang-infrastructure/go-iterator v0.0.0-20230524171120-56988a9b127c/go.mod h1:Guf14ZZ7f7qiE0YpZbUkICcm7LWc3nrn3/gDObT//WI=
79
github.com/golang-infrastructure/go-pointer v0.0.5 h1:wzZ/XnXKzD3DWECTnUpUh+xAlGSWqfn/pQyusPNsqrQ=
810
github.com/golang-infrastructure/go-pointer v0.0.5/go.mod h1:SBP0/8QH+mr8LGP/tDo28EQ2b45WqfzjXYbPjmK88g8=
911
github.com/golang-infrastructure/go-reflect-utils v0.0.0-20221130143747-965ef2eb09c3 h1:jJ7AdpNdLQudsx1hiXY9iwmauHARV4/UB52KnBh9Se0=
1012
github.com/golang-infrastructure/go-reflect-utils v0.0.0-20221130143747-965ef2eb09c3/go.mod h1:zqXYxqOBa1mL2ilBK6PuH/Wb/Iego7en6XhiKWdZQHI=
11-
github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I=
12-
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
13+
github.com/google/uuid v1.3.1 h1:KjJaJ9iWZ3jOFZIf1Lqf4laDRCasjl0BCmnEGxkdLb4=
14+
github.com/google/uuid v1.3.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
1315
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
1416
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
1517
github.com/lib/pq v1.10.9 h1:YXG7RB+JIjhP29X+OtkiDnYaXQwpS4JEWq7dtCCRUEw=
@@ -21,14 +23,16 @@ github.com/storage-lock/go-events v0.0.3 h1:QuNbaREe5m1WYC0meKs7SRJ5lY11a18jCJJS
2123
github.com/storage-lock/go-events v0.0.3/go.mod h1:DKwPgfuq8T8CyPLipH+0XHsNt8hEN4g9krUGfosU2oY=
2224
github.com/storage-lock/go-sql-based-storage v0.0.0-20230815152553-e6057f9c32e7 h1:WbtCSU+1NC4eO0QwehH4aQNLGBm9SUyvo39aMEYLFjE=
2325
github.com/storage-lock/go-sql-based-storage v0.0.0-20230815152553-e6057f9c32e7/go.mod h1:kKR7bTZ59cP7jK0tFhQ4yz2hD2XAL8A97Haql9NAygI=
26+
github.com/storage-lock/go-sql-based-storage v0.0.0-20230903155403-1c8c724d3383 h1:fw1cz2FM9h6Y7XmfEP8xgA8AmxpGFZVXcBi0iKa+O8Y=
27+
github.com/storage-lock/go-sql-based-storage v0.0.0-20230903155403-1c8c724d3383/go.mod h1:4ixNpjEQ9Aj4E9GExfopf3vw14oqXRSuFSduq7QRgF4=
2428
github.com/storage-lock/go-storage v0.0.2 h1:N5glUlrr1bbJ8ixYsPmOv+TuhIOObX/65+hrNMPWFDc=
2529
github.com/storage-lock/go-storage v0.0.2/go.mod h1:4vuU2nLdbmzj2UIBKpwB5lj8RI+GnPDZ4YHZ7QAcFHE=
2630
github.com/storage-lock/go-storage-events v0.0.5 h1:qUgB/VZIQNM0PIos67T9D1JIqu3Yiu3+YDpuHEal3FI=
2731
github.com/storage-lock/go-storage-events v0.0.5/go.mod h1:aBMVsOSr61+BLxO3QnU5FwKd5bdMGE4IpV0oDrAg8uQ=
2832
github.com/storage-lock/go-storage-lock v0.0.3 h1:eobL73fhfAjLKNk4msQNTYFcilrPKwySXY/5ympfeRU=
2933
github.com/storage-lock/go-storage-lock v0.0.3/go.mod h1:kzyjFhgPmiYMwLuuVoQfYxXPUOIGNETA+Z/wDnRc/iw=
30-
github.com/storage-lock/go-storage-test-helper v0.0.2 h1:6uipDmyxhncEJJSVwPiydKJrGVLoFARwbnkarDfhWSY=
31-
github.com/storage-lock/go-storage-test-helper v0.0.2/go.mod h1:JrQptW0yJZVqDsObE9vWSoyZxhQHdQDM8/eh4vz7Tls=
34+
github.com/storage-lock/go-storage-test-helper v0.0.3 h1:y8B24zJkQaI7uYOW2MTBIHTZXbRpkEG6rS1XMWAttjs=
35+
github.com/storage-lock/go-storage-test-helper v0.0.3/go.mod h1:Zn6YIq0r0pmK+naGUhRhb7TqTEFDjoU+25yIugt9iAw=
3236
github.com/storage-lock/go-utils v0.0.2 h1:pdacTOlD+AHqwWVmDqZLcjKF+7p8TjsmlvZ9bmmkIfE=
3337
github.com/storage-lock/go-utils v0.0.2/go.mod h1:tx8ATHL7yOC0Nyjpba7hJNk0+QNt2XEnMr8NcGm9Gak=
3438
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=

postgresql_connection_manager.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,10 @@ import (
99
"github.com/storage-lock/go-storage"
1010
)
1111

12+
// DefaultPostgresqlStorageSchema 默认的schema
1213
const DefaultPostgresqlStorageSchema = "public"
1314

15+
// PostgresqlConnectionManager Postgresql的连接管理器
1416
type PostgresqlConnectionManager struct {
1517

1618
// 主机的名字

postgresql_sql_provider.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
// PostgresqlSqlProvider storage sql的postgresql方言
1111
type PostgresqlSqlProvider struct {
12-
*sql_based_storage.Sql97Provider
12+
*sql_based_storage.Sql92Provider
1313
}
1414

1515
var _ sql_based_storage.SqlProvider = &PostgresqlSqlProvider{}
@@ -41,3 +41,23 @@ func (x *PostgresqlSqlProvider) UpdateWithVersionSql(ctx context.Context, tableF
4141
insertSql := fmt.Sprintf("UPDATE %s SET version = $1, lock_information_json_string = $2 WHERE lock_id = $3 AND owner_id = $4 AND version = $5", tableFullName)
4242
return insertSql, []any{newVersion, lockInformation.ToJsonString(), lockId, lockInformation.OwnerId, exceptedVersion}
4343
}
44+
45+
func (x *PostgresqlSqlProvider) CreateTableSql(ctx context.Context, tableFullName string) (string, []any) {
46+
createTableSql := `CREATE TABLE IF NOT EXISTS %s (
47+
lock_id VARCHAR(255) NOT NULL PRIMARY KEY,
48+
owner_id VARCHAR(255) NOT NULL,
49+
version BIGINT NOT NULL,
50+
lock_information_json_string VARCHAR(255) NOT NULL
51+
)`
52+
return fmt.Sprintf(createTableSql, tableFullName), nil
53+
}
54+
55+
func (x *PostgresqlSqlProvider) FindLockInformationJsonStringByIdSql(ctx context.Context, tableFullName string, lockId string) (string, []any) {
56+
getLockSql := fmt.Sprintf("SELECT lock_information_json_string FROM %s WHERE lock_id = $1", tableFullName)
57+
return getLockSql, []any{lockId}
58+
}
59+
60+
func (x *PostgresqlSqlProvider) ListLockInformationJsonStringSql(ctx context.Context, tableFullName string) (string, []any) {
61+
sql := fmt.Sprintf("SELECT lock_information_json_string FROM %s", tableFullName)
62+
return sql, nil
63+
}

postgresql_storage.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package postgresql_storage
22

33
import (
44
"context"
5-
"errors"
65
"fmt"
76
_ "github.com/lib/pq"
87
sql_based_storage "github.com/storage-lock/go-sql-based-storage"
@@ -116,7 +115,7 @@ func (x *PostgresqlStorage) GetTime(ctx context.Context) (time.Time, error) {
116115
_ = rs.Close()
117116
}()
118117
if !rs.Next() {
119-
return zero, errors.New("query postgresql server time failed")
118+
return zero, ErrQueryPostgresqlServerTime
120119
}
121120
var databaseTime time.Time
122121
err = rs.Scan(&databaseTime)

postgresql_storage_options.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type PostgresqlStorageOptions struct {
1919
ConnectionManager storage.ConnectionManager[*sql.DB]
2020
}
2121

22+
// NewPostgresqlStorageOptions 创建一个Storage选项
2223
func NewPostgresqlStorageOptions() *PostgresqlStorageOptions {
2324
return &PostgresqlStorageOptions{
2425
Schema: DefaultPostgresqlStorageSchema,
@@ -61,6 +62,7 @@ func (x *PostgresqlStorageOptions) SetConnectionManager(connectionManager storag
6162

6263
func (x *PostgresqlStorageOptions) Check() error {
6364

65+
// TODO 参数为空的时候是设置值还是返回错误比较合适呢?
6466
if x.Schema == "" {
6567
x.Schema = DefaultPostgresqlStorageSchema
6668
}

postgresql_storage_test.go

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package postgresql_storage
22

33
import (
44
"context"
5-
"github.com/storage-lock/go-storage"
65
storage_test_helper "github.com/storage-lock/go-storage-test-helper"
76
"github.com/stretchr/testify/assert"
87
"os"
@@ -15,10 +14,7 @@ func TestNewPostgresqlStorage(t *testing.T) {
1514
dsn := os.Getenv(envName)
1615
assert.NotEmpty(t, dsn)
1716
connectionGetter := NewPostgresqlConnectionGetterFromDSN(dsn)
18-
s, err := NewPostgresqlStorage(context.Background(), &PostgresqlStorageOptions{
19-
ConnectionManager: connectionGetter,
20-
TableName: storage.DefaultStorageTableName,
21-
})
17+
s, err := NewPostgresqlStorage(context.Background(), NewPostgresqlStorageOptions().SetConnectionManager(connectionGetter))
2218
assert.Nil(t, err)
2319
storage_test_helper.TestStorage(t, s)
2420
}

scripts/postgresql.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ docker rm -f storage-lock-postgres
66
# 启动测试使用的Postgresql数据库
77
docker run -d --name storage-lock-postgres -p 5432:5432 -e POSTGRES_PASSWORD=UeGqAm8CxYGldMDLoNNt postgres:14
88

9+
# 设置环境变量
910
export STORAGE_LOCK_POSTGRESQL_DSN="host=127.0.0.1 user=postgres password=UeGqAm8CxYGldMDLoNNt port=5432 dbname=postgres sslmode=disable"

0 commit comments

Comments
 (0)