casbin / gorm-adapter

GORM adapter for Casbin, see extended version of GORM Adapter Ex at: https://github.com/casbin/gorm-adapter-ex
https://github.com/casbin/casbin
Apache License 2.0
679 stars 206 forks source link

[bug] Resolver mysql #186

Closed vine1993 closed 1 year ago

vine1993 commented 1 year ago

When i try to use NewAdapterByDB everything explodes with drivers not related to the one that I'm using.

Im using GORM with MySQL and I've got errors related SQL server and SQlite.

db.go

package main

import (
    "fmt"
    "log"
    "os"
    "time"

    "github.com/Teknier/api/domain/user"
    "gorm.io/driver/mysql"
    "gorm.io/gorm"
    "gorm.io/gorm/logger"
)

func DBConnection() (*gorm.DB, error) {

    newLogger := logger.New(
        log.New(os.Stdout, "\r\n", log.LstdFlags), // io writer
        logger.Config{
            SlowThreshold: time.Second, // Slow SQL threshold
            LogLevel:      logger.Info, // Log level
            Colorful:      true,        // Disable color
        },
    )

    url := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True&loc=Local", "root", "root", "localhost", "3306", "test")
    db, dbErr := gorm.Open(mysql.Open(url), &gorm.Config{Logger: newLogger})

    db.AutoMigrate(&user.Entity{})

    return db, dbErr
}

policy.go

package main

import (
    "fmt"

    gormadapter "github.com/casbin/gorm-adapter/v3"
    "gorm.io/gorm"
)

func Policy(db *gorm.DB) {
    _, errAdapter := gormadapter.NewAdapterByDB(db)
    if errAdapter != nil {
        panic(fmt.Sprintf("failed to initialize casbin adapter: %v", errAdapter))
    }
}
image

I've already checked the DB variable and the connection work just fine, my entities migrations are working just fine.

casbin-bot commented 1 year ago

@tangyang9464 @JalinWang @imp2002

hsluoyz commented 1 year ago

@imp2002

imp2002 commented 1 year ago

Sorry, I cannot reproduce your error. Could you provide more code, it is best to run the reproduction directly.

vine1993 commented 1 year ago

@imp2002 I've got very busy right now, by today or tomorrow I'll try to create a new project and see what happened. Please don't close this issue.

I did change my adapter to use the file system and everything works just fine.

vine1993 commented 1 year ago

@imp2002 im not sure why my main project its getting this issue, I did start a new project, and did work as expected. Thank you for checking this out, I'm closing this issue.