Skip to content

Commit

Permalink
create table when to init adapter (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
JunNishimura committed Mar 28, 2024
1 parent 3c2801a commit b0dac19
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ func NewAdapter(driverName, dataSourceName string, opts ...adapterOption) (*bunA
opt(b)
}

if err := b.createTalbe(); err != nil {
return nil, err
}

return b, nil
}

Expand Down Expand Up @@ -97,6 +101,29 @@ func connectDB(driverName, dataSourceName string) (*bun.DB, error) {
}
}

func (a *bunAdapter) createTalbe() error {
if _, err := a.db.NewCreateTable().
Model((*CasbinPolicy)(nil)).
Table(a.tableName).
IfNotExists().
Exec(context.Background()); err != nil {
return err
}
return nil
}

var _ bun.AfterCreateTableHook = (*CasbinPolicy)(nil)

func (*CasbinPolicy) AfterCreateTable(ctx context.Context, query *bun.CreateTableQuery) error {
_, err := query.DB().NewCreateIndex().
Model((*CasbinPolicy)(nil)).
Unique().
Index("idx_ptype_v0_v1_v2_v3_v4_v5").
Column("ptype", "v0", "v1", "v2", "v3", "v4", "v5").
Exec(ctx)
return err
}

// LoadPolicy loads all policy rules from the storage.
func (a *bunAdapter) LoadPolicy(model model.Model) error {
var policies []CasbinPolicy
Expand Down

0 comments on commit b0dac19

Please sign in to comment.