2020-12-31 03:37:32 +05:30
|
|
|
package ent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/sha256"
|
|
|
|
"strings"
|
|
|
|
|
2021-03-24 12:07:51 +05:30
|
|
|
"entgo.io/ent/dialect/sql"
|
2022-03-07 14:48:32 +05:30
|
|
|
"entgo.io/ent/dialect/sql/schema"
|
2022-05-25 14:47:34 +05:30
|
|
|
_ "github.com/mattn/go-sqlite3" // Register sqlite driver.
|
2020-12-31 03:37:32 +05:30
|
|
|
|
|
|
|
"github.com/dexidp/dex/pkg/log"
|
|
|
|
"github.com/dexidp/dex/storage"
|
|
|
|
"github.com/dexidp/dex/storage/ent/client"
|
|
|
|
"github.com/dexidp/dex/storage/ent/db"
|
|
|
|
)
|
|
|
|
|
|
|
|
// SQLite3 options for creating an SQL db.
|
|
|
|
type SQLite3 struct {
|
|
|
|
File string `json:"file"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open always returns a new in sqlite3 storage.
|
|
|
|
func (s *SQLite3) Open(logger log.Logger) (storage.Storage, error) {
|
|
|
|
logger.Debug("experimental ent-based storage driver is enabled")
|
|
|
|
|
|
|
|
// Implicitly set foreign_keys pragma to "on" because it is required by ent
|
|
|
|
s.File = addFK(s.File)
|
|
|
|
|
|
|
|
drv, err := sql.Open("sqlite3", s.File)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:55:17 +05:30
|
|
|
// always allow only one connection to sqlite3, any other thread/go-routine
|
|
|
|
// attempting concurrent access will have to wait
|
2020-12-31 03:37:32 +05:30
|
|
|
pool := drv.DB()
|
2021-09-14 20:25:03 +05:30
|
|
|
pool.SetMaxOpenConns(1)
|
2020-12-31 03:37:32 +05:30
|
|
|
|
|
|
|
databaseClient := client.NewDatabase(
|
|
|
|
client.WithClient(db.NewClient(db.Driver(drv))),
|
|
|
|
client.WithHasher(sha256.New),
|
|
|
|
)
|
|
|
|
|
2022-04-06 17:48:32 +05:30
|
|
|
if err := databaseClient.Schema().Create(context.TODO(), schema.WithAtlas(false)); err != nil {
|
2020-12-31 03:37:32 +05:30
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return databaseClient, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func addFK(dsn string) string {
|
|
|
|
if strings.Contains(dsn, "_fk") {
|
|
|
|
return dsn
|
|
|
|
}
|
|
|
|
|
|
|
|
delim := "?"
|
|
|
|
if strings.Contains(dsn, "?") {
|
|
|
|
delim = "&"
|
|
|
|
}
|
|
|
|
return dsn + delim + "_fk=1"
|
|
|
|
}
|