Skip to content

Commit

Permalink
feat: support NewAdapterWithoutAutoMigrate (#160)
Browse files Browse the repository at this point in the history
* feat: support NewAdapterWithoutAutoMigrate

Signed-off-by: tangyang9464 <tangyang9464@163.com>

* Update adapter_test.go

Co-authored-by: Yang Luo <hsluoyz@qq.com>
  • Loading branch information
tangyang9464 and hsluoyz committed May 12, 2022
1 parent b86e139 commit 956618a
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 3 deletions.
23 changes: 20 additions & 3 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ const (

type customTableKey struct{}

const disableMigrateKey = "disableMigrateKey"

type CasbinRule struct {
ID uint `gorm:"primaryKey;autoIncrement"`
Ptype string `gorm:"size:100"`
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
62 changes: 62 additions & 0 deletions adapter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -354,6 +378,44 @@ 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"), &gorm.Config{})
if err != nil {
panic(err)
}

if err = db.Exec("CREATE DATABASE casbin_custom_table").Error; err != nil {
// 42P04 is duplicate_database
if !strings.Contains(fmt.Sprintf("%s", err), "42P04") {
panic(err)
}
}

db, err = gorm.Open(postgres.Open("user=postgres password=postgres host=127.0.0.1 port=5432 sslmode=disable dbname=casbin_custom_table"), &gorm.Config{})
if err != nil {
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")
Expand Down

0 comments on commit 956618a

Please sign in to comment.