Skip to content

Commit 837e4d6

Browse files
committed
v4
1 parent 5335977 commit 837e4d6

4 files changed

Lines changed: 59 additions & 110 deletions

File tree

adapter.go

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,13 @@ import (
2323
"sync"
2424
"time"
2525

26-
"github.com/casbin/casbin/v3"
27-
"github.com/casbin/casbin/v3/model"
28-
"github.com/casbin/casbin/v3/persist"
29-
"github.com/glebarez/sqlite"
26+
"github.com/casbin/casbin/v2"
27+
"github.com/casbin/casbin/v2/model"
28+
"github.com/casbin/casbin/v2/persist"
3029
"github.com/pkg/errors"
3130
"gorm.io/driver/mysql"
3231
"gorm.io/driver/postgres"
32+
"gorm.io/driver/sqlite"
3333
"gorm.io/driver/sqlserver"
3434
"gorm.io/gorm"
3535
"gorm.io/gorm/clause"
@@ -326,16 +326,17 @@ func openDBConnection(driverName, dataSourceName string) (*gorm.DB, error) {
326326
config := &gorm.Config{
327327
Logger: logger.Default.LogMode(logger.Silent),
328328
}
329-
if driverName == "postgres" {
329+
switch driverName {
330+
case "postgres":
330331
db, err = gorm.Open(postgres.Open(dataSourceName), config)
331-
} else if driverName == "mysql" {
332+
case "mysql":
332333
db, err = gorm.Open(mysql.Open(dataSourceName), config)
333-
} else if driverName == "sqlserver" {
334+
case "sqlserver":
334335
db, err = gorm.Open(sqlserver.Open(dataSourceName), config)
335-
} else if driverName == "sqlite3" {
336+
case "sqlite":
336337
db, err = gorm.Open(sqlite.Open(dataSourceName), config)
337-
} else {
338-
return nil, errors.New("Database dialect '" + driverName + "' is not supported. Supported databases are postgres, mysql, sqlserver and sqlite3")
338+
default:
339+
return nil, errors.New("Database dialect '" + driverName + "' is not supported. Supported databases are postgres, mysql, sqlserver and sqlite")
339340
}
340341
if err != nil {
341342
return nil, err
@@ -356,7 +357,7 @@ func (a *Adapter) createDatabase() error {
356357
return nil
357358
}
358359
}
359-
} else if a.driverName != "sqlite3" && a.driverName != "sqlserver" {
360+
} else if a.driverName != "sqlite" && a.driverName != "sqlserver" {
360361
err = db.Exec("CREATE DATABASE IF NOT EXISTS " + a.databaseName).Error
361362
}
362363
if err != nil {
@@ -378,13 +379,14 @@ func (a *Adapter) Open() error {
378379
if err = a.createDatabase(); err != nil {
379380
return err
380381
}
381-
if a.driverName == "postgres" {
382+
switch a.driverName {
383+
case "postgres":
382384
db, err = openDBConnection(a.driverName, a.dataSourceName+" dbname="+a.databaseName)
383-
} else if a.driverName == "sqlite3" {
385+
case "sqlite":
384386
db, err = openDBConnection(a.driverName, a.dataSourceName)
385-
} else if a.driverName == "sqlserver" {
387+
case "sqlserver":
386388
db, err = openDBConnection(a.driverName, a.dataSourceName+"?database="+a.databaseName)
387-
} else {
389+
default:
388390
db, err = openDBConnection(a.driverName, a.dataSourceName+a.databaseName)
389391
}
390392
if err != nil {
@@ -468,9 +470,7 @@ func (a *Adapter) dropTable() error {
468470
func (a *Adapter) truncateTable() error {
469471
var sql string
470472
switch a.db.Config.Name() {
471-
case sqlite.DriverName:
472-
sql = fmt.Sprintf("delete from %s", a.getFullTableName())
473-
case "sqlite3":
473+
case "sqlite":
474474
sql = fmt.Sprintf("delete from %s", a.getFullTableName())
475475
case "postgres":
476476
sql = fmt.Sprintf("truncate table %s RESTART IDENTITY", a.getFullTableName())

adapter_test.go

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,16 +24,17 @@ import (
2424
"testing"
2525
"time"
2626

27-
"github.com/casbin/casbin/v3"
28-
"github.com/casbin/casbin/v3/util"
29-
"github.com/glebarez/sqlite"
27+
"github.com/casbin/casbin/v2"
28+
"github.com/casbin/casbin/v2/util"
29+
3030
_ "github.com/go-sql-driver/mysql"
3131
_ "github.com/lib/pq"
3232
"github.com/stretchr/testify/assert"
3333
"github.com/stretchr/testify/require"
3434
"golang.org/x/sync/errgroup"
3535
"gorm.io/driver/mysql"
3636
"gorm.io/driver/postgres"
37+
"gorm.io/driver/sqlite"
3738
"gorm.io/driver/sqlserver"
3839
"gorm.io/gorm"
3940
"gorm.io/gorm/logger"
@@ -268,7 +269,7 @@ func initAdapterWithGormInstanceByPrefixAndName(t *testing.T, db *gorm.DB, prefi
268269
}
269270

270271
func TestNilField(t *testing.T) {
271-
a, err := NewAdapter("sqlite3", "test.db")
272+
a, err := NewAdapter("sqlite", "test.db")
272273
assert.Nil(t, err)
273274
defer os.Remove("test.db")
274275

@@ -542,7 +543,7 @@ func TestAdapters(t *testing.T) {
542543
testAutoSave(t, a)
543544
testSaveLoad(t, a)
544545

545-
a = initAdapter(t, "sqlite3", "casbin.db")
546+
a = initAdapter(t, "sqlite", "casbin.db")
546547
testAutoSave(t, a)
547548
testSaveLoad(t, a)
548549

@@ -667,7 +668,7 @@ func TestAdapters(t *testing.T) {
667668
testUpdatePolicies(t, a)
668669
testUpdateFilteredPolicies(t, a)
669670

670-
a = initAdapter(t, "sqlite3", "casbin.db")
671+
a = initAdapter(t, "sqlite", "casbin.db")
671672
testUpdatePolicy(t, a)
672673
testUpdatePolicies(t, a)
673674

go.mod

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
1-
module github.com/casbin/gorm-adapter/v3
1+
module github.com/maestrohub-labs/casbin-gorm-adapter/v4
22

3-
go 1.24.0
3+
go 1.25.0
44

55
require (
6-
github.com/casbin/casbin/v3 v3.8.1
7-
github.com/glebarez/sqlite v1.11.0
6+
github.com/casbin/casbin/v2 v2.123.0
87
github.com/go-sql-driver/mysql v1.9.3
9-
github.com/lib/pq v1.10.2
8+
github.com/lib/pq v1.11.2
109
github.com/pkg/errors v0.9.1
1110
github.com/stretchr/testify v1.11.1
1211
golang.org/x/sync v0.19.0
1312
gorm.io/driver/mysql v1.6.0
1413
gorm.io/driver/postgres v1.6.0
14+
gorm.io/driver/sqlite v1.6.0
1515
gorm.io/driver/sqlserver v1.6.3
1616
gorm.io/gorm v1.31.1
1717
gorm.io/plugin/dbresolver v1.6.2
1818
)
1919

2020
require (
2121
filippo.io/edwards25519 v1.1.0 // indirect
22-
github.com/bmatcuk/doublestar/v4 v4.9.1 // indirect
22+
github.com/bmatcuk/doublestar/v4 v4.10.0 // indirect
2323
github.com/casbin/govaluate v1.10.0 // indirect
2424
github.com/davecgh/go-spew v1.1.1 // indirect
25-
github.com/dustin/go-humanize v1.0.1 // indirect
26-
github.com/glebarez/go-sqlite v1.22.0 // indirect
2725
github.com/golang-sql/civil v0.0.0-20220223132316-b832511892a9 // indirect
2826
github.com/golang-sql/sqlexp v0.1.0 // indirect
2927
github.com/google/uuid v1.6.0 // indirect
@@ -33,19 +31,11 @@ require (
3331
github.com/jackc/puddle/v2 v2.2.2 // indirect
3432
github.com/jinzhu/inflection v1.0.0 // indirect
3533
github.com/jinzhu/now v1.1.5 // indirect
36-
github.com/mattn/go-isatty v0.0.20 // indirect
37-
github.com/microsoft/go-mssqldb v1.9.5 // indirect
38-
github.com/ncruces/go-strftime v1.0.0 // indirect
34+
github.com/mattn/go-sqlite3 v1.14.34 // indirect
35+
github.com/microsoft/go-mssqldb v1.9.6 // indirect
3936
github.com/pmezard/go-difflib v1.0.0 // indirect
40-
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
4137
github.com/shopspring/decimal v1.4.0 // indirect
42-
golang.org/x/crypto v0.46.0 // indirect
43-
golang.org/x/exp v0.0.0-20251219203646-944ab1f22d93 // indirect
44-
golang.org/x/sys v0.39.0 // indirect
45-
golang.org/x/text v0.32.0 // indirect
38+
golang.org/x/crypto v0.48.0 // indirect
39+
golang.org/x/text v0.34.0 // indirect
4640
gopkg.in/yaml.v3 v3.0.1 // indirect
47-
modernc.org/libc v1.67.4 // indirect
48-
modernc.org/mathutil v1.7.1 // indirect
49-
modernc.org/memory v1.11.0 // indirect
50-
modernc.org/sqlite v1.42.2 // indirect
5141
)

0 commit comments

Comments
 (0)