casbin/gorm-adapter

table names cannot be specified after automatic migration is disabled

OldSmokeGun opened this issue · 13 comments

#160

This commit can indeed turn off the automatic migration, but it cannot specify a custom data table name

I try to make a commit, but I don't have the environment for unit testing, so I can't pass the test

Hope the NewAdapterWithoutAutoMigrate function can specify the data table name

@OldSmokeGun Because the adapter gives the user the right to create the table, customize table name and even table structure will be based on the functions provided by gorm itself. You can customize the table name like this

db, _ := gorm.Open(mysql.Open("root:root@tcp(127.0.0.1:3306)/casbin"), &gorm.Config{})
db.Table("custom_name").Migrator().AutoMigrate(&gormadapter.CasbinRule{})
a, _ := gormadapter.NewAdapterWithoutAutoMigrate(db)

@tangyang9464 Thanks for your answer, but don't you think this limits user only can use the gormadapter.CasbinRule type?

Why can't the user's own structure and table name be passed in like the NewAdapterByDBWithCustomTable function? This is extremely scalable for users

@tangyang9464 Thanks for your answer, but don't you think this limits user only can use the gormadapter.CasbinRule type?

Why can't the user's own structure and table name be passed in like the NewAdapterByDBWithCustomTable function? This is extremely scalable for users

@OldSmokeGun db.Table("custom_name").Migrator().AutoMigrate(custom_table{}) can define both table name and table structure. Actually NewAdapterByDBWithCustomTable also does it like this. CustomTable just change the gorm struct tags, but the table structure must stay the same. see https://github.com/casbin/gorm-adapter#customize-table-columns-example

@tangyang9464

image

As you can see, the NewAdapterWithoutAutoMigrate function uses defaultTableName, and its value is casbin_rule, which means that even if I use a custom structure when migrating, its table name must be casbin_rule, otherwise, how does the adapter know to use which table

If there are multiple casbins in a system, this will create conflicts

@OldSmokeGun Got it. Fix later.

@tangyang9464 Thanks for your contribution!

use TurnOffAutoMigrate is Fail, As follows

db, err := gorm.Open(mysql.Open("root:ddd@tcp(127.0.0.1:3306)/test"), &gorm.Config{})      
gormadapter.TurnOffAutoMigrate(db)       
a, err := gormadapter.NewAdapterByDBWithCustomTable(db, nil, "casbin_rule")

someone like me?

CREATE TABLE `dsc_casbin_rule` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'key',
  `ptype` varchar(100) DEFAULT NULL,
  `v0` varchar(100) DEFAULT NULL,
  `v1` varchar(100) DEFAULT NULL,
  `v2` varchar(100) DEFAULT NULL,
  `v3` varchar(100) DEFAULT NULL,
  `v4` varchar(100) DEFAULT NULL,
  `v5` varchar(100) DEFAULT NULL,
  `v6` varchar(25) DEFAULT NULL,
  `v7` varchar(25) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_casbin_rule` (`ptype`,`v0`,`v1`,`v2`,`v3`),
  UNIQUE KEY `idx_dsc_casbin_rule` (`ptype`,`v0`,`v1`,`v2`,`v3`,`v4`,`v5`,`v6`,`v7`)   # it is automatically generated
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COMMENT='casbin rule';

use TurnOffAutoMigrate is Fail, As follows

db, err := gorm.Open(mysql.Open("root:ddd@tcp(127.0.0.1:3306)/test"), &gorm.Config{})      
gormadapter.TurnOffAutoMigrate(db)       
a, err := gormadapter.NewAdapterByDBWithCustomTable(db, nil, "casbin_rule")

someone like me?

@Lee-0x00 db = gormadapter.TurnOffAutoMigrate(db)

@tangyang9464 this usage is not natural and can easily cause confusion. A call on gormadapter.TurnOffAutoMigrate(db) should have effect directly and it doesn't need to return a value. Can we change this behavior?

@tangyang9464 this usage is not natural and can easily cause confusion. A call on gormadapter.TurnOffAutoMigrate(db) should have effect directly and it doesn't need to return a value. Can we change this behavior?

Check it out later

@tangyang9464 this usage is not natural and can easily cause confusion. A call on gormadapter.TurnOffAutoMigrate(db) should have effect directly and it doesn't need to return a value. Can we change this behavior?