wader / gormstore

GORM backend for gorilla sessions

Home Page:https://pkg.go.dev/github.com/wader/gormstore?tab=doc

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support for GORM 2.0

jagardaniel opened this issue · comments

Hi!

GORM recently released version 2 which seems to have introduced a few changes. One of them is that the project has moved and the import path has changed from "github.com/jinzhu/gorm" to "gorm.io/gorm". I'm new to programming and Go in general but I think this is the main reason why it doesn't work with GORM 2.0 right now.

package main

import (
  	"fmt"
	"log"

	"github.com/wader/gormstore"
	"gorm.io/driver/postgres"
	"gorm.io/gorm"
)

func main() {
	dsn := "user=gorm password=gorm dbname=gorm port=5432 sslmode=disable TimeZone=Europe/Stockholm"
	db, err := gorm.Open(postgres.Open(dsn), &gorm.Config{})
	if err != nil {
		log.Fatal(err)
	}

	store := gormstore.New(db, []byte("secret"))
	fmt.Println(store)
}
C:\Users\daniel\Desktop\go\store>go build main.go && main.exe
# command-line-arguments
.\main.go:19:24: cannot use db (type *"gorm.io/gorm".DB) as type *"github.com/jinzhu/gorm".DB in argument to gormstore.New

I also made a desperate attempt to update gormstore to use the new import path to see what happens. The compiler complains about that gorm.NowFunc() does not exist anymore in V2.

So I'm wondering if it would be possible to get gormstore to work with GORM version 2 in the future. I don't think a lot of changes has to be made but maybe the import path change could be an issue since it would break with people still using V1 of GORM.

Thanks!
/Daniel

Hi! sorry for the delay

That would be nice. Yeah it seems quite a lot has changed about Open and also how to manipulate tables. I wonder if it should be just a new version of gormstore, a gormstore v2 or a new repo gormstore2?

Did some quick testing here https://github.com/wader/gormstore/tree/gorm2 seems to be some issue with TableName and also not sure about NowFunc. Feel free to continue one it, it can really promise to work on it that much atm.

Hi. First of all, thanks for the reply. Appreciated!

I tried out the new branch and as you mentioned TableName returns an empty string which causes AutoMigrate to fail. I don't know if this is the reason, but I checked the release notes for GORM 2.0 and TableName does not allow dynamic names anymore. It works if I just return a "static" string (return "sessions") in the TableName method.

It looks like the rest of the code works (it saves to the database, clean up expired etc) but as I mentioned in the post I'm pretty new to all of this (sessions as well) so don't trust me too much. I'm also on Windows right now so not able to run the tests. Also not sure about versioning. I know that some packages like echo and validator allows you to add a version in the end of the import path, maybe that could be an alternative as well.

Thanks,
Daniel

Mm read that also about dynamic tables name but i wonder if it has to with caching mostly? they still support overriding the name using a method?

Aha i see, could running the tests using docker for windows be an option somehow?

Yeap maybe v2 in same repo is the best option

+1 on this item.
Your "v2" release/tag is a good option.

Log

cannot use DB (type *"gorm.io/gorm".DB) as type *"github.com/jinzhu/gorm".DB in argument to gormstore.NewOptions
commented

I used Scope to let gorm create the table with Options.TableName.
It runs great in my code, but I'm not sure that is there any problem with it.
Maybe there is a problem with the Store.db? Because it's passing by pointer.
below is what I do.
https://github.com/fnsne/gormstore/blob/gorm2/gormstore.go#L105-L107

Mm the table name thing was what i got stuck at also, haven't had much time to look into this yet but wish i can soon. In the meantime if someone else could come up with a solution for gorm v2 that could keep the gormstore API as it is it would be great.

@fnsne Did you get Scope to work? I saw that you changed your code a little bit after the post. I also got it to work by setting and then returning a global variable from the TableName() method instead (like your did) but wasn't sure if that is seen as "ugly" in Go. That would probably be more clean than using Table() everywhere like I tried.

commented

@jagardaniel Because I'm afraid that the Scope will affect other Gorm models, I choose to let the gorilla sessions can only use one storm with Gorm.
I don't have any general solution. But using a global variable for Gormstore is a solution for my project.

Thanks for the reply!

I ran the tests (./test as well) and they all passed with the change to use a global variable instead for the table name. If you guys think this is good enough maybe one of us could create a pull request with the change and get it merged into the gorm2 branch. It should be enough to get gormstore to work with GORM 2.0 :)

Hi again, i had a go at trying to use scope instead of TableName() and it seems to work and I don't think it should affect other models? Haven't used gorm 2 myself much so maybe i misunderstand something?

I've updated the gorm2 branch https://github.com/wader/gormstore/tree/gorm2

I can't comment about using scope or if that is the best way to solve this, but I tried your updated gorm2 branch and it seems to work fine for me. I'm able to create sessions, clean them up etc. I can also run all tests without any issue :)

Tjena, thanks for testing, wait for some more people to test and if looks good move ahead and creating a v2 version somehow?

I upgraded an old web service (the one i created gormstore for 😄) to use gorm2 and gormstore with gorm2 branch and it seems to work fine.

Now you should be able to use import "github.com/wader/gormstore/v2"

Awesome! Thanks to everyone involved :)

Sorry for the delay, turn out not being that tricky after all (i hope). Also setup some actions to keep dependency versions update to date and tested