From 27b1478f389b89a360bef80c407855fa630e7a5b Mon Sep 17 00:00:00 2001 From: tangyang9464 Date: Thu, 12 May 2022 19:36:49 +0800 Subject: [PATCH] feat: support NewAdapterWithOutAutoMigrate Signed-off-by: tangyang9464 --- adapter.go | 23 +++++++++++++++++--- adapter_test.go | 57 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+), 3 deletions(-) diff --git a/adapter.go b/adapter.go index ebb4316..15cc018 100755 --- a/adapter.go +++ b/adapter.go @@ -39,6 +39,8 @@ const ( type customTableKey struct{} +const disableMigrateKey = "disableMigrateKey" + type CasbinRule struct { ID uint `gorm:"primaryKey;autoIncrement"` Ptype string `gorm:"size:100"` @@ -195,9 +197,13 @@ func NewAdapterByDBUseTableName(db *gorm.DB, prefix string, tableName string) (* } a.db = db.Scopes(a.casbinRuleTable()).Session(&gorm.Session{Context: db.Statement.Context}) - err := a.createTable() - if err != nil { - return nil, err + + disableMigrate := a.db.Statement.Context.Value(disableMigrateKey) + if disableMigrate == nil { + err := a.createTable() + if err != nil { + return nil, err + } } return a, nil @@ -249,6 +255,17 @@ func NewAdapterByDB(db *gorm.DB) (*Adapter, error) { return NewAdapterByDBUseTableName(db, "", defaultTableName) } +func NewAdapterWithOutAutoMigrate(db *gorm.DB) (*Adapter, error) { + ctx := db.Statement.Context + if ctx == nil { + ctx = context.Background() + } + + ctx = context.WithValue(ctx, disableMigrateKey, false) + + return NewAdapterByDBUseTableName(db.WithContext(ctx), "", defaultTableName) +} + func NewAdapterByDBWithCustomTable(db *gorm.DB, t interface{}, tableName ...string) (*Adapter, error) { ctx := db.Statement.Context if ctx == nil { diff --git a/adapter_test.go b/adapter_test.go index 98273aa..975ac6f 100755 --- a/adapter_test.go +++ b/adapter_test.go @@ -192,6 +192,30 @@ func initAdapterWithGormInstanceByName(t *testing.T, db *gorm.DB, name string) * return a } +func initAdapterWithOutAutoMigrate(t *testing.T, db *gorm.DB) *Adapter { + var err error + hasTable := db.Migrator().HasTable(defaultTableName) + if hasTable { + err = db.Migrator().DropTable(defaultTableName) + if err != nil { + panic(err) + } + } + + a, err := NewAdapterWithOutAutoMigrate(db) + + hasTable = a.db.Migrator().HasTable(a.getFullTableName()) + if hasTable { + t.Fatal("AutoMigration has been disabled but tables are still created in NewAdapterWithOutAutoMigrate method") + } + err = a.db.Migrator().CreateTable(&CasbinRule{}) + if err != nil { + panic(err) + } + initPolicy(t, a) + return a +} + func initAdapterWithGormInstanceByMulDb(t *testing.T, dbPool DbPool, dbName string, prefix string, tableName string) *Adapter { //Create an Adapter a, _ := NewAdapterByMulDb(dbPool, dbName, prefix, tableName) @@ -354,6 +378,39 @@ func TestAdapterWithCustomTable(t *testing.T) { testFilteredPolicy(t, a) } +func TestAdapterWithOutAutoMigrate(t *testing.T) { + db, err := gorm.Open(mysql.Open("root:@tcp(127.0.0.1:3306)/casbin"), &gorm.Config{}) + if err != nil { + panic(err) + } + + a := initAdapterWithOutAutoMigrate(t, db) + testAutoSave(t, a) + testSaveLoad(t, a) + + a = initAdapterWithOutAutoMigrate(t, db) + testFilteredPolicy(t, a) + + db, err = gorm.Open(postgres.Open("user=postgres password=postgres host=127.0.0.1 port=5432 sslmode=disable dbname=casbin"), &gorm.Config{}) + if err != nil { + panic(err) + } + + if err = db.Exec("CREATE DATABASE casbin").Error; err != nil { + // 42P04 is duplicate_database + if !strings.Contains(fmt.Sprintf("%s", err), "42P04") { + panic(err) + } + } + + a = initAdapterWithOutAutoMigrate(t, db) + testAutoSave(t, a) + testSaveLoad(t, a) + + a = initAdapterWithOutAutoMigrate(t, db) + testFilteredPolicy(t, a) +} + func TestAdapterWithMulDb(t *testing.T) { //create new database NewAdapter("mysql", "root:@tcp(127.0.0.1:3306)/", "casbin")