Skip to content

Commit

Permalink
✨ Reimplement lock (#352)
Browse files Browse the repository at this point in the history
* ✨ Add redis singleton

* ⬆️ Upgrade golang version

* ✨ Add locker table

* ✨ Reimplement lock
  • Loading branch information
tosone authored Apr 5, 2024
1 parent fd1110e commit 528e5d8
Show file tree
Hide file tree
Showing 33 changed files with 827 additions and 255 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -199,3 +199,4 @@ pkg/**/*.html
*.zstd
web/.yarn/
docs/.yarn/
coverage.txt
2 changes: 1 addition & 1 deletion build/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG GOLANG_VERSION=1.22.1-alpine3.19
ARG GOLANG_VERSION=1.22.2-alpine3.19
ARG NODE_VERSION=20-alpine3.19
ARG ALPINE_VERSION=3.19

Expand Down
2 changes: 1 addition & 1 deletion build/Dockerfile.builder
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG GOLANG_VERSION=1.22.1-alpine3.19
ARG GOLANG_VERSION=1.22.2-alpine3.19
ARG BUILDKIT_VERSION=v0.12.4-rootless
ARG ALPINE_VERSION=3.19

Expand Down
2 changes: 1 addition & 1 deletion build/Dockerfile.debian
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARG GOLANG_VERSION=1.22.1-bookworm
ARG GOLANG_VERSION=1.22.2-bookworm
ARG NODE_VERSION=20-alpine3.19
ARG ALPINE_VERSION=3.19
ARG DEBIAN_VERSION=bookworm-slim
Expand Down
7 changes: 3 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/caarlos0/env/v9 v9.0.0
github.com/casbin/casbin/v2 v2.84.1
github.com/casbin/gorm-adapter/v3 v3.21.0
github.com/containers/podman/v5 v5.0.0
github.com/containers/podman/v5 v5.0.1
github.com/deckarep/golang-set/v2 v2.6.0
github.com/distribution/distribution/v3 v3.0.0-alpha.1
github.com/distribution/reference v0.5.0
Expand All @@ -26,7 +26,6 @@ require (
github.com/glebarez/sqlite v1.11.0
github.com/go-git/go-git/v5 v5.11.0
github.com/go-playground/validator v9.31.0+incompatible
github.com/go-redsync/redsync/v4 v4.12.1
github.com/go-resty/resty/v2 v2.11.0
github.com/go-sql-driver/mysql v1.8.0
github.com/golang-jwt/jwt/v5 v5.2.1
Expand Down Expand Up @@ -119,8 +118,8 @@ require (
github.com/containerd/errdefs v0.1.0 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.15.1 // indirect
github.com/containers/buildah v1.35.1 // indirect
github.com/containers/common v0.58.0 // indirect
github.com/containers/buildah v1.35.3 // indirect
github.com/containers/common v0.58.1 // indirect
github.com/containers/image/v5 v5.30.0 // indirect
github.com/containers/libtrust v0.0.0-20230121012942-c1716e8a8d01 // indirect
github.com/containers/ocicrypt v1.1.9 // indirect
Expand Down
26 changes: 6 additions & 20 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -282,18 +282,18 @@ github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo=
github.com/containerd/stargz-snapshotter/estargz v0.15.1 h1:eXJjw9RbkLFgioVaTG+G/ZW/0kEe2oEKCdS/ZxIyoCU=
github.com/containerd/stargz-snapshotter/estargz v0.15.1/go.mod h1:gr2RNwukQ/S9Nv33Lt6UC7xEx58C+LHRdoqbEKjz1Kk=
github.com/containers/buildah v1.35.1 h1:m4TF6V8b06cS4jH9/t39PUsUIjzDQg/P14FLpwjr40Y=
github.com/containers/buildah v1.35.1/go.mod h1:vVSVUlTu8+99H5j43gBJscpkb/quZvdJg78+6X1HeTM=
github.com/containers/common v0.58.0 h1:iQuwMxDD4ubZ9s1tmgdsiaHxMU4TdVBpV6kctJc6Bk8=
github.com/containers/common v0.58.0/go.mod h1:l3vMqanJGj7tZ3W/i76gEJ128VXgFUO1tLaohJXPvdk=
github.com/containers/buildah v1.35.3 h1:Dn8Krwm2PemBNNOMwp7uiMK2e5cW2ZjTdLRzKM789pc=
github.com/containers/buildah v1.35.3/go.mod h1:kYi6vTHdbr1gnRo3B/RhTHsY2if/w398+/RvCxAXqkQ=
github.com/containers/common v0.58.1 h1:E1DN9Lr7kgMVQy7AXLv1CYQCiqnweklMiYWbf0KOnqY=
github.com/containers/common v0.58.1/go.mod h1:l3vMqanJGj7tZ3W/i76gEJ128VXgFUO1tLaohJXPvdk=
github.com/containers/image/v5 v5.30.0 h1:CmHeSwI6W2kTRWnUsxATDFY5TEX4b58gPkaQcEyrLIA=
github.com/containers/image/v5 v5.30.0/go.mod h1:gSD8MVOyqBspc0ynLsuiMR9qmt8UQ4jpVImjmK0uXfk=
github.com/containers/libtrust v0.0.0-20230121012942-c1716e8a8d01 h1:Qzk5C6cYglewc+UyGf6lc8Mj2UaPTHy/iF2De0/77CA=
github.com/containers/libtrust v0.0.0-20230121012942-c1716e8a8d01/go.mod h1:9rfv8iPl1ZP7aqh9YA68wnZv2NUDbXdcdPHVz0pFbPY=
github.com/containers/ocicrypt v1.1.9 h1:2Csfba4jse85Raxk5HIyEk8OwZNjRvfkhEGijOjIdEM=
github.com/containers/ocicrypt v1.1.9/go.mod h1:dTKx1918d8TDkxXvarscpNVY+lyPakPNFN4jwA9GBys=
github.com/containers/podman/v5 v5.0.0 h1:DVjeY4oTbI9qsxQfBTswerS8xvlAy0KywLN9yNc/MAA=
github.com/containers/podman/v5 v5.0.0/go.mod h1:lJXhiseM72otkIcp0sVDUn9aFyScHqmHmlarbboRX4I=
github.com/containers/podman/v5 v5.0.1 h1:40OJuoOJWxt3hk1j9J0jcwWIbWpjARxpzazYYtCDiNY=
github.com/containers/podman/v5 v5.0.1/go.mod h1:v09KUXu9AEFvzgiKSwkB49jcrVglXx2yi2qaPyaKNBE=
github.com/containers/psgo v1.9.0 h1:eJ74jzSaCHnWt26OlKZROSyUyRcGDf+gYBdXnxrMW4g=
github.com/containers/psgo v1.9.0/go.mod h1:0YoluUm43Mz2UnBIh1P+6V6NWcbpTL5uRtXyOcH0B5A=
github.com/containers/storage v1.53.0 h1:VSES3C/u1pxjTJIXvLrSmyP7OBtDky04oGu07UvdTEA=
Expand Down Expand Up @@ -476,14 +476,6 @@ github.com/go-playground/universal-translator v0.18.1 h1:Bcnm0ZwsGyWbCzImXv+pAJn
github.com/go-playground/universal-translator v0.18.1/go.mod h1:xekY+UJKNuX9WP91TpwSH2VMlDf28Uj24BCp08ZFTUY=
github.com/go-playground/validator v9.31.0+incompatible h1:UA72EPEogEnq76ehGdEDp4Mit+3FDh548oRqwVgNsHA=
github.com/go-playground/validator v9.31.0+incompatible/go.mod h1:yrEkQXlcI+PugkyDjY2bRrL/UBU4f3rvrgkN3V8JEig=
github.com/go-redis/redis v6.15.9+incompatible h1:K0pv1D7EQUjfyoMql+r/jZqCLizCGKFlFgcHWWmHQjg=
github.com/go-redis/redis v6.15.9+incompatible/go.mod h1:NAIEuMOZ/fxfXJIrKDQDz8wamY7mA7PouImQ2Jvg6kA=
github.com/go-redis/redis/v7 v7.4.0 h1:7obg6wUoj05T0EpY0o8B59S9w5yeMWql7sw2kwNW1x4=
github.com/go-redis/redis/v7 v7.4.0/go.mod h1:JDNMw23GTyLNC4GZu9njt15ctBQVn7xjRfnwdHj/Dcg=
github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC0oI=
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
github.com/go-redsync/redsync/v4 v4.12.1 h1:hCtdZ45DJxMxNdPiby5GlQwOKQmcka2587Y466qPqlA=
github.com/go-redsync/redsync/v4 v4.12.1/go.mod h1:sn72ojgeEhxUuRjrliK0NRrB0Zl6kOZ3BDvNN3P2jAY=
github.com/go-resty/resty/v2 v2.11.0 h1:i7jMfNOJYMp69lq7qozJP+bjgzfAzeOhuGlyDrqxT/8=
github.com/go-resty/resty/v2 v2.11.0/go.mod h1:iiP/OpA0CkcL3IGt1O0+/SIItFUbkkyw5BGXiVdTu+A=
github.com/go-sigma/soft_delete v0.0.0-20231124084503-fb6a66078e2b h1:dP1itc+/9pJ/0ku3ntc8Rti0w+zuslJfOAnMNyv3N7I=
Expand Down Expand Up @@ -559,8 +551,6 @@ github.com/golang/snappy v0.0.2/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEW
github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/gomodule/redigo v1.8.9 h1:Sl3u+2BI/kk+VEatbj0scLdrFhjPmbxOc1myhDP41ws=
github.com/gomodule/redigo v1.8.9/go.mod h1:7ArFNvsTjH8GMMzB4uy1snslv2BwmginuMs06a1uzZE=
github.com/google/btree v0.0.0-20180813153112-4030bb1f1f0c/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/btree v1.0.0/go.mod h1:lNA+9X1NB3Zf8V7Ke586lFgjr2dZNuvo3lPJSGZ5JPQ=
github.com/google/certificate-transparency-go v1.1.7 h1:IASD+NtgSTJLPdzkthwvAG1ZVbF2WtFg4IvoA68XGSw=
Expand Down Expand Up @@ -1078,8 +1068,6 @@ github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475/go.mod h1:bCqn
github.com/redis/go-redis/v9 v9.0.3/go.mod h1:WqMKv5vnQbRuZstUwxQI195wHy+t4PuXDOjzMvcuQHk=
github.com/redis/go-redis/v9 v9.5.1 h1:H1X4D3yHPaYrkL5X06Wh6xNVM/pX0Ft4RV0vMGvLBh8=
github.com/redis/go-redis/v9 v9.5.1/go.mod h1:hdY0cQFCN4fnSYT6TkisLufl/4W5UIXyv0b/CLO2V2M=
github.com/redis/rueidis v1.0.19 h1:s65oWtotzlIFN8eMPhyYwxlwLR1lUdhza2KtWprKYSo=
github.com/redis/rueidis v1.0.19/go.mod h1:8B+r5wdnjwK3lTFml5VtxjzGOQAC+5UmujoD12pDrEo=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec h1:W09IVJc94icq4NjY3clb7Lk8O1qJ8BdBEF8z0ibU0rE=
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec/go.mod h1:qqbHyh8v60DhA7CoWK5oRCqLrMHRGoxYCSS9EjAz6Eo=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
Expand Down Expand Up @@ -1206,8 +1194,6 @@ github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/stvp/tempredis v0.0.0-20181119212430-b82af8480203 h1:QVqDTf3h2WHt08YuiTGPZLls0Wq99X9bWd0Q5ZSBesM=
github.com/stvp/tempredis v0.0.0-20181119212430-b82af8480203/go.mod h1:oqN97ltKNihBbwlX8dLpwxCl3+HnXKV/R0e+sRLd9C8=
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
Expand Down
10 changes: 3 additions & 7 deletions pkg/cronjob/builder/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,13 @@ func (r builderRunner) runner(ctx context.Context, tw timewheel.TimeWheel) {
log.Error().Err(err).Msg("New locker failed")
return
}
lock, err := locker.Lock(context.Background(), consts.LockerCronjobBuilder, time.Second*30)
ctx, ctxCancel := context.WithCancel(context.Background())
defer ctxCancel()
err = locker.AcquireWithRenew(ctx, consts.LockerCronjobBuilder, time.Second*3, time.Second*5)
if err != nil {
log.Error().Err(err).Msg("Cronjob builder get locker failed")
return
}
defer func() {
err := lock.Unlock()
if err != nil {
log.Error().Err(err).Msg("Migrate locker release failed")
}
}()

ctx = log.Logger.WithContext(ctx)
builderService := r.builderServiceFactory.New()
Expand Down
12 changes: 4 additions & 8 deletions pkg/dal/dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,12 @@ func Initialize(config configs.Configuration) error {
if err != nil {
return err
}
lock, err := locker.Lock(context.Background(), consts.LockerMigration, time.Second*30)
ctx, ctxCancel := context.WithCancel(context.Background())
defer ctxCancel()
err = locker.AcquireWithRenew(ctx, consts.LockerMigration, time.Second*3, time.Second*5)
if err != nil {
return err
}
defer func() {
err := lock.Unlock()
if err != nil {
log.Error().Err(err).Msg("Migrate locker release failed")
}
}()

switch config.Database.Type {
case enums.DatabaseMysql:
Expand Down Expand Up @@ -165,7 +161,7 @@ func connectPostgres(config configs.Configuration) (string, error) {
func connectSqlite3(config configs.Configuration) (string, error) {
dbname := config.Database.Sqlite3.Path

db, err := gorm.Open(sqlite.Open(dbname), &gorm.Config{
db, err := gorm.Open(sqlite.Open(dbname+"?_busy_timeout=10000"), &gorm.Config{
NowFunc: func() time.Time {
return time.Now().UTC()
},
Expand Down
67 changes: 47 additions & 20 deletions pkg/dal/dao/locker.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/go-sigma/sigma/pkg/dal/models"
"github.com/go-sigma/sigma/pkg/dal/query"
"github.com/go-sigma/sigma/pkg/modules/locker/definition"
)

//go:generate mockgen -destination=mocks/locker.go -package=mocks github.com/go-sigma/sigma/pkg/dal/dao LockerService
Expand All @@ -32,9 +33,11 @@ import (
// LockerService is the interface that provides methods to operate on locker model
type LockerService interface {
// Create creates a new work queue record in the database
Create(ctx context.Context, name string) error
Create(ctx context.Context, key, val string, expire int64) error
// Delete get a locker record
Delete(ctx context.Context, name string) error
Delete(ctx context.Context, key, val string) error
// Renew renew a locker record
Renew(ctx context.Context, key, val string, expire int64) error
}

type lockerService struct {
Expand Down Expand Up @@ -64,28 +67,52 @@ func (s *lockerServiceFactory) New(txs ...*query.Query) LockerService {
}

// Create creates a new work queue record in the database
func (s lockerService) Create(ctx context.Context, name string) error {
for i := 0; i < 6; i++ {
err := s.tx.Locker.WithContext(ctx).Create(&models.Locker{Name: name})
if err == nil {
return nil
func (s lockerService) Create(ctx context.Context, key, value string, expire int64) error {
lock, err := s.tx.Locker.WithContext(ctx).Where(s.tx.Locker.Key.Eq(key)).First()
if err == nil {
if lock.Expire < time.Now().UnixMilli() {
_, err = s.tx.Locker.WithContext(ctx).Where(s.tx.Locker.Key.Eq(key)).Delete()
if err != nil {
return err
}
} else {
return fmt.Errorf("Locker %s already exists", key)
}
if !errors.Is(err, gorm.ErrDuplicatedKey) {
return err
}
<-time.After(time.Second)
}
return fmt.Errorf("cannot acquire locker for %s", name)
if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return err
}
return s.tx.Locker.WithContext(ctx).Create(&models.Locker{Key: key, Value: value, Expire: expire})
}

// Delete get a locker record
func (s lockerService) Delete(ctx context.Context, name string) error {
matched, err := s.tx.Locker.WithContext(ctx).Unscoped().Where(s.tx.Locker.Name.Eq(name)).Delete()
if err != nil {
return err
}
if matched.RowsAffected == 0 {
return gorm.ErrRecordNotFound
func (s lockerService) Delete(ctx context.Context, key, value string) error {
_, err := s.tx.Locker.WithContext(ctx).Unscoped().Where(
s.tx.Locker.Key.Eq(key), s.tx.Locker.Value.Eq(value)).Delete()
return err
}

// Renew renew a locker record
func (s lockerService) Renew(ctx context.Context, key, value string, expire int64) error {
lock, err := s.tx.Locker.WithContext(ctx).Where(s.tx.Locker.Key.Eq(key)).First()
if err == nil {
if lock.Value != value {
return definition.ErrLockNotHeld
}
if lock.Expire < time.Now().UnixMilli() {
_, err = s.tx.Locker.WithContext(ctx).Where(s.tx.Locker.Key.Eq(key)).Delete()
if err != nil {
return err
}
return definition.ErrLockAlreadyExpired
} else {
_, err := s.tx.Locker.WithContext(ctx).Where(s.tx.Locker.Key.Eq(key)).UpdateColumns(map[string]any{
query.Locker.Expire.ColumnName().String(): expire,
})
if err != nil {
return err
}
}
}
return nil
return err
}
22 changes: 18 additions & 4 deletions pkg/dal/dao/mocks/locker.go

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

4 changes: 2 additions & 2 deletions pkg/dal/dao/workq.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ type WorkQueueService interface {
// Create creates a new work queue record in the database
Create(ctx context.Context, workqObj *models.WorkQueue) error
// Get get a work queue record
Get(ctx context.Context, topic string) (*models.WorkQueue, error)
Get(ctx context.Context, topic enums.Daemon) (*models.WorkQueue, error)
// UpdateStatus update a work queue record status
UpdateStatus(ctx context.Context, id int64, version, newVersion string, times int, status enums.TaskCommonStatus) error
}
Expand Down Expand Up @@ -69,7 +69,7 @@ func (s workQueueService) Create(ctx context.Context, workqObj *models.WorkQueue
}

// Get get a work queue record
func (s workQueueService) Get(ctx context.Context, topic string) (*models.WorkQueue, error) {
func (s workQueueService) Get(ctx context.Context, topic enums.Daemon) (*models.WorkQueue, error) {
return s.tx.WorkQueue.WithContext(ctx).Where(
s.tx.WorkQueue.Status.Eq(enums.TaskCommonStatusPending),
s.tx.WorkQueue.Topic.Eq(topic),
Expand Down
2 changes: 1 addition & 1 deletion pkg/dal/dao/workq_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func TestWorkQueueService(t *testing.T) {
err = workqService.UpdateStatus(ctx, workqObj.ID, "version", "newVersion", 1, enums.TaskCommonStatusPending)
assert.NoError(t, err)

workqNewObj, err := workqService.Get(ctx, enums.DaemonGc.String())
workqNewObj, err := workqService.Get(ctx, enums.DaemonGc)
assert.NoError(t, err)
assert.Equal(t, workqObj.ID, workqNewObj.ID)
assert.Equal(t, workqObj.Topic, workqNewObj.Topic)
Expand Down
2 changes: 2 additions & 0 deletions pkg/dal/migrations/mysql/0003_upgrade.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE IF EXISTS `lockers`;

11 changes: 11 additions & 0 deletions pkg/dal/migrations/mysql/0003_upgrade.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS `lockers` (
`id` bigint AUTO_INCREMENT PRIMARY KEY,
`key` varchar(256) NOT NULL,
`value` varchar(256) NOT NULL,
`expire` bigint NOT NULL DEFAULT 0,
`created_at` bigint NOT NULL DEFAULT (UNIX_TIMESTAMP (CURRENT_TIMESTAMP()) * 1000),
`updated_at` bigint NOT NULL DEFAULT (UNIX_TIMESTAMP (CURRENT_TIMESTAMP()) * 1000),
`deleted_at` bigint NOT NULL DEFAULT 0,
CONSTRAINT `idx_lockers_key` UNIQUE (`key`, `deleted_at`)
);

2 changes: 2 additions & 0 deletions pkg/dal/migrations/postgresql/0003_upgrade.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE IF EXISTS "lockers";

11 changes: 11 additions & 0 deletions pkg/dal/migrations/postgresql/0003_upgrade.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS "lockers" (
"id" bigserial PRIMARY KEY,
"key" varchar(256) NOT NULL,
"value" varchar(256) NOT NULL,
"expire" bigint NOT NULL DEFAULT 0,
"created_at" bigint NOT NULL DEFAULT ((EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 1000)::bigint),
"updated_at" bigint NOT NULL DEFAULT ((EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 1000)::bigint),
"deleted_at" bigint NOT NULL DEFAULT 0,
CONSTRAINT "idx_lockers_key" UNIQUE ("key", "deleted_at")
);

2 changes: 2 additions & 0 deletions pkg/dal/migrations/sqlite3/0003_upgrade.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
DROP TABLE IF EXISTS `lockers`;

11 changes: 11 additions & 0 deletions pkg/dal/migrations/sqlite3/0003_upgrade.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
CREATE TABLE IF NOT EXISTS `lockers` (
`id` integer PRIMARY KEY AUTOINCREMENT,
`key` varchar(256) NOT NULL,
`value` varchar(256) NOT NULL,
`expire` integer NOT NULL DEFAULT 0,
`created_at` integer NOT NULL DEFAULT (unixepoch () * 1000),
`updated_at` integer NOT NULL DEFAULT (unixepoch () * 1000),
`deleted_at` integer NOT NULL DEFAULT 0,
CONSTRAINT `idx_lockers_key` UNIQUE (`key`, `deleted_at`)
);

4 changes: 3 additions & 1 deletion pkg/dal/models/locker.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ type Locker struct {
DeletedAt soft_delete.DeletedAt `gorm:"softDelete:milli"`
ID int64 `gorm:"primaryKey"`

Name string `gorm:"uniqueIndex,size:256"`
Key string
Expire int64
Value string
}
Loading

0 comments on commit 528e5d8

Please sign in to comment.