Skip to content

Commit

Permalink
Merge pull request #15 from orange-llajeanne/skipTableCreatePR
Browse files Browse the repository at this point in the history
Add option to skip table creation at adapter startup
  • Loading branch information
hsluoyz committed Jan 13, 2021
2 parents 7984420 + 0fb125e commit b31339b
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ type Filter struct {

// Adapter represents the github.com/go-pg/pg adapter for policy storage.
type Adapter struct {
db *pg.DB
tableName string
filtered bool
db *pg.DB
tableName string
skipTableCreate bool
filtered bool
}

type Option func(a *Adapter)
Expand Down Expand Up @@ -70,8 +71,10 @@ func NewAdapterByDB(db *pg.DB, opts ...Option) (*Adapter, error) {
a.db.Model((*CasbinRule)(nil)).TableModel().Table().FullNameForSelects = (types.Safe)(a.tableName)
}

if err := a.createTableifNotExists(); err != nil {
return nil, fmt.Errorf("pgadapter.NewAdapter: %v", err)
if !a.skipTableCreate {
if err := a.createTableifNotExists(); err != nil {
return nil, fmt.Errorf("pgadapter.NewAdapter: %v", err)
}
}
return a, nil
}
Expand All @@ -83,6 +86,14 @@ func WithTableName(tableName string) Option {
}
}

// SkipTableCreate skips the table creation step when the adapter starts
// If the Casbin rules table does not exist, it will lead to issues when using the adapter
func SkipTableCreate() Option {
return func(a *Adapter) {
a.skipTableCreate = true
}
}

func createCasbinDatabase(arg interface{}) (*pg.DB, error) {
var opts *pg.Options
var err error
Expand Down

0 comments on commit b31339b

Please sign in to comment.