Skip to content

Commit edb446c

Browse files
authored
fix: use TurnOffAutoMigrate instead of NewAdapterWithoutAutoMigrate (#163)
Signed-off-by: tangyang9464 <tangyang9464@163.com>
1 parent 6248ffe commit edb446c

File tree

3 files changed

+59
-19
lines changed

3 files changed

+59
-19
lines changed

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,15 @@ func main() {
7070
}
7171
```
7272
## Turn off AutoMigrate
73-
New an adapter will use ``AutoMigrate`` by default for create table, if you want to turn it off, please use API ``NewAdapterWithoutAutoMigrate(db *gorm.DB) (*Adapter, error)``. Find out more at [gorm-adapter#160](https://github.com/casbin/gorm-adapter/pull/160)
73+
New an adapter will use ``AutoMigrate`` by default for create table, if you want to turn it off, please use API ``TurnOffAutoMigrate(db *gorm.DB) *gorm.DB``. See example:
74+
```go
75+
db, err := gorm.Open(mysql.Open("root:@tcp(127.0.0.1:3306)/casbin"), &gorm.Config{})
76+
db = TurnOffAutoMigrate(db)
77+
// a,_ := NewAdapterByDB(...)
78+
// a,_ := NewAdapterByDBUseTableName(...)
79+
a,_ := NewAdapterByDBWithCustomTable(...)
80+
```
81+
Find out more details at [gorm-adapter#162](https://github.com/casbin/gorm-adapter/issues/162)
7482
## Customize table columns example
7583
You can change the gorm struct tags, but the table structure must stay the same.
7684
```go

adapter.go

+18-13
Original file line numberDiff line numberDiff line change
@@ -198,12 +198,9 @@ func NewAdapterByDBUseTableName(db *gorm.DB, prefix string, tableName string) (*
198198

199199
a.db = db.Scopes(a.casbinRuleTable()).Session(&gorm.Session{Context: db.Statement.Context})
200200

201-
disableMigrate := a.db.Statement.Context.Value(disableMigrateKey)
202-
if disableMigrate == nil {
203-
err := a.createTable()
204-
if err != nil {
205-
return nil, err
206-
}
201+
err := a.createTable()
202+
if err != nil {
203+
return nil, err
207204
}
208205

209206
return a, nil
@@ -255,15 +252,15 @@ func NewAdapterByDB(db *gorm.DB) (*Adapter, error) {
255252
return NewAdapterByDBUseTableName(db, "", defaultTableName)
256253
}
257254

258-
func NewAdapterWithoutAutoMigrate(db *gorm.DB) (*Adapter, error) {
255+
func TurnOffAutoMigrate(db *gorm.DB) *gorm.DB {
259256
ctx := db.Statement.Context
260257
if ctx == nil {
261258
ctx = context.Background()
262259
}
263260

264261
ctx = context.WithValue(ctx, disableMigrateKey, false)
265262

266-
return NewAdapterByDBUseTableName(db.WithContext(ctx), "", defaultTableName)
263+
return db.WithContext(ctx)
267264
}
268265

269266
func NewAdapterByDBWithCustomTable(db *gorm.DB, t interface{}, tableName ...string) (*Adapter, error) {
@@ -383,6 +380,11 @@ func (a *Adapter) casbinRuleTable() func(db *gorm.DB) *gorm.DB {
383380
}
384381

385382
func (a *Adapter) createTable() error {
383+
disableMigrate := a.db.Statement.Context.Value(disableMigrateKey)
384+
if disableMigrate != nil {
385+
return nil
386+
}
387+
386388
t := a.db.Statement.Context.Value(customTableKey{})
387389

388390
if t != nil {
@@ -414,6 +416,13 @@ func (a *Adapter) dropTable() error {
414416
return a.db.Migrator().DropTable(t)
415417
}
416418

419+
func (a *Adapter) truncateTable() error {
420+
if a.db.Config.Name() == sqlite.DriverName {
421+
return a.db.Exec(fmt.Sprintf("delete from %s", a.getFullTableName())).Error
422+
}
423+
return a.db.Exec(fmt.Sprintf("truncate table %s", a.getFullTableName())).Error
424+
}
425+
417426
func loadPolicyLine(line CasbinRule, model model.Model) {
418427
var p = []string{line.Ptype,
419428
line.V0, line.V1, line.V2,
@@ -538,11 +547,7 @@ func (a *Adapter) savePolicyLine(ptype string, rule []string) CasbinRule {
538547

539548
// SavePolicy saves policy to database.
540549
func (a *Adapter) SavePolicy(model model.Model) error {
541-
err := a.dropTable()
542-
if err != nil {
543-
return err
544-
}
545-
err = a.createTable()
550+
err := a.truncateTable()
546551
if err != nil {
547552
return err
548553
}

adapter_test.go

+32-5
Original file line numberDiff line numberDiff line change
@@ -194,21 +194,36 @@ func initAdapterWithGormInstanceByName(t *testing.T, db *gorm.DB, name string) *
194194

195195
func initAdapterWithoutAutoMigrate(t *testing.T, db *gorm.DB) *Adapter {
196196
var err error
197-
hasTable := db.Migrator().HasTable(defaultTableName)
197+
var customTableName = "without_auto_migrate_custom_table"
198+
hasTable := db.Migrator().HasTable(customTableName)
198199
if hasTable {
199-
err = db.Migrator().DropTable(defaultTableName)
200+
err = db.Migrator().DropTable(customTableName)
200201
if err != nil {
201202
panic(err)
202203
}
203204
}
204205

205-
a, err := NewAdapterWithoutAutoMigrate(db)
206+
db = TurnOffAutoMigrate(db)
207+
208+
type CustomCasbinRule struct {
209+
ID uint `gorm:"primaryKey;autoIncrement"`
210+
Ptype string `gorm:"size:50"`
211+
V0 string `gorm:"size:50"`
212+
V1 string `gorm:"size:50"`
213+
V2 string `gorm:"size:50"`
214+
V3 string `gorm:"size:50"`
215+
V4 string `gorm:"size:50"`
216+
V5 string `gorm:"size:50"`
217+
V6 string `gorm:"size:50"`
218+
V7 string `gorm:"size:50"`
219+
}
220+
a, err := NewAdapterByDBWithCustomTable(db, &CustomCasbinRule{}, customTableName)
206221

207222
hasTable = a.db.Migrator().HasTable(a.getFullTableName())
208223
if hasTable {
209224
t.Fatal("AutoMigration has been disabled but tables are still created in NewAdapterWithoutAutoMigrate method")
210225
}
211-
err = a.db.Migrator().CreateTable(&CasbinRule{})
226+
err = a.db.Migrator().CreateTable(&CustomCasbinRule{})
212227
if err != nil {
213228
panic(err)
214229
}
@@ -391,7 +406,7 @@ func TestAdapterWithoutAutoMigrate(t *testing.T) {
391406
a = initAdapterWithoutAutoMigrate(t, db)
392407
testFilteredPolicy(t, a)
393408

394-
db, err = gorm.Open(postgres.Open("user=postgres password=postgres host=127.0.0.1 port=5432 sslmode=disable"), &gorm.Config{})
409+
db, err = gorm.Open(postgres.Open("user=postgres password=postgres host=localhost port=5432 sslmode=disable TimeZone=Asia/Shanghai"), &gorm.Config{})
395410
if err != nil {
396411
panic(err)
397412
}
@@ -414,6 +429,18 @@ func TestAdapterWithoutAutoMigrate(t *testing.T) {
414429

415430
a = initAdapterWithoutAutoMigrate(t, db)
416431
testFilteredPolicy(t, a)
432+
433+
db, err = gorm.Open(sqlite.Open("casbin.db"), &gorm.Config{})
434+
if err != nil {
435+
panic(err)
436+
}
437+
438+
a = initAdapterWithoutAutoMigrate(t, db)
439+
testAutoSave(t, a)
440+
testSaveLoad(t, a)
441+
442+
a = initAdapterWithoutAutoMigrate(t, db)
443+
testFilteredPolicy(t, a)
417444
}
418445

419446
func TestAdapterWithMulDb(t *testing.T) {

0 commit comments

Comments
 (0)