Melora Hugues
d5aa640df0
Because polyculeconnect is a OIDC proxy, we need to know which auth backend to use. This is provided using a query param or a form, so we need to get it from our own middleware. This commit adds the following elements: - basic DB storage for the backends - support for DB migrations and a first test migration (not definitive) - middleware to get the backend from the request and put it in the context - test that the backend exists in the auth flow
60 lines
1.8 KiB
Go
60 lines
1.8 KiB
Go
package db
|
|
|
|
import (
|
|
"fmt"
|
|
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/cmd/utils"
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/config"
|
|
"git.faercol.me/faercol/polyculeconnect/polyculeconnect/internal/db"
|
|
"github.com/golang-migrate/migrate/v4"
|
|
"github.com/golang-migrate/migrate/v4/database/sqlite3"
|
|
_ "github.com/golang-migrate/migrate/v4/source/file"
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// migrateCmd represents the db migrate command
|
|
var migrateCmd = &cobra.Command{
|
|
Use: "migrate",
|
|
Short: "Run the database migrations",
|
|
Long: `Run the database migrations.`,
|
|
Run: func(cmd *cobra.Command, args []string) {
|
|
conf := utils.InitConfig("")
|
|
if err := runMigrations(conf); err != nil {
|
|
utils.Failf("Failed to run migrations: %s", err.Error())
|
|
}
|
|
},
|
|
}
|
|
|
|
func runMigrations(conf *config.AppConfig) error {
|
|
storage, err := db.New(*conf)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to connect to db: %w", err)
|
|
}
|
|
driver, err := sqlite3.WithInstance(storage.DB(), &sqlite3.Config{})
|
|
if err != nil {
|
|
return fmt.Errorf("failed to open sqlite3 driver: %w", err)
|
|
}
|
|
|
|
m, err := migrate.NewWithDatabaseInstance("file://migrations", "", driver)
|
|
if err != nil {
|
|
return fmt.Errorf("failed to init migrator: %w", err)
|
|
}
|
|
if err := m.Up(); err != nil {
|
|
return fmt.Errorf("failed to run migrations: %w", err)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func init() {
|
|
dbCmd.AddCommand(migrateCmd)
|
|
|
|
// Here you will define your flags and configuration settings.
|
|
|
|
// Cobra supports Persistent Flags which will work for this command
|
|
// and all subcommands, e.g.:
|
|
// dbCmd.PersistentFlags().String("foo", "", "A help for foo")
|
|
|
|
// Cobra supports local flags which will only run when this command
|
|
// is called directly, e.g.:
|
|
// dbCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
|
|
}
|