Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SQLite Schema #2187

Draft
wants to merge 7 commits into
base: v2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cmd/hooks/hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ func checkPendingMigrations(db database.Database) error {
}
counter := map[string]ID{}

files, err := convoy.MigrationFiles.ReadDir("sql")
files, err := convoy.PostgresMigrationFiles.ReadDir("sql")
if err != nil {
return err
}
Expand Down
87 changes: 70 additions & 17 deletions cmd/migrate/migrate.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package migrate

import (
"fmt"
"github.com/frain-dev/convoy/database/sqlite3"
"os"
"time"

Expand All @@ -13,6 +14,11 @@ import (
"github.com/spf13/cobra"
)

var mapping = map[string]string{
"agent": "postgres",
"server": "sqlite",
}

func AddMigrateCommand(a *cli.App) *cobra.Command {
cmd := &cobra.Command{
Use: "migrate",
Expand All @@ -27,6 +33,7 @@ func AddMigrateCommand(a *cli.App) *cobra.Command {
}

func addUpCommand() *cobra.Command {
var component string
cmd := &cobra.Command{
Use: "up",
Aliases: []string{"migrate-up"},
Expand All @@ -36,32 +43,65 @@ func addUpCommand() *cobra.Command {
"ShouldBootstrap": "false",
},
Run: func(cmd *cobra.Command, args []string) {
cfg, err := config.Get()
t, err := cmd.Flags().GetString("component")
if err != nil {
log.WithError(err).Fatal("Error fetching the config.")
log.Fatal(err)
}

db, err := postgres.NewDB(cfg)
if err != nil {
log.Fatal(err)
if t != "server" && t != "agent" {
log.Fatalf("Invalid component %s. Must be one of: server or agent", t)
}

defer db.Close()
switch t {
case "server":
cfg, err := config.Get()
if err != nil {
log.WithError(err).Fatal("[sqlite3] error fetching the config.")
}

m := migrator.New(db)
err = m.Up()
if err != nil {
log.Fatalf("migration up failed with error: %+v", err)
db, err := sqlite3.NewDB(cfg.Database.SqliteDB, log.NewLogger(os.Stdout))
if err != nil {
log.Fatal(err)
}

defer db.Close()

m := migrator.New(db, "sqlite3")
err = m.Up()
if err != nil {
log.Fatalf("[sqlite3] migration up failed with error: %+v", err)
}
case "agent":
cfg, err := config.Get()
if err != nil {
log.WithError(err).Fatal("[postgres] error fetching the config.")
}

db, err := postgres.NewDB(cfg)
if err != nil {
log.Fatal(err)
}

defer db.Close()

m := migrator.New(db, "postgres")
err = m.Up()
if err != nil {
log.Fatalf("[postgres] migration up failed with error: %+v", err)
}
}

log.Info("migration up succeeded")
},
}

cmd.Flags().StringVarP(&component, "component", "c", "server", "The component to create for: (server|agent)")

return cmd
}

func addDownCommand() *cobra.Command {
var max int
var maxDown int

cmd := &cobra.Command{
Use: "down",
Expand All @@ -84,29 +124,40 @@ func addDownCommand() *cobra.Command {

defer db.Close()

m := migrator.New(db)
err = m.Down(max)
m := migrator.New(db, "postgres")
err = m.Down(maxDown)
if err != nil {
log.Fatalf("migration down failed with error: %+v", err)
}
},
}

cmd.Flags().IntVar(&max, "max", 1, "The maximum number of migrations to rollback")
cmd.Flags().IntVar(&maxDown, "max", 1, "The maximum number of migrations to rollback")

return cmd
}

func addCreateCommand() *cobra.Command {
var component string
cmd := &cobra.Command{
Use: "create",
Short: "creates a new migration file",
Use: "create",
Aliases: []string{"migrate-create"},
Short: "creates a new migration file",
Annotations: map[string]string{
"CheckMigration": "false",
"ShouldBootstrap": "false",
},
Run: func(cmd *cobra.Command, args []string) {
fileName := fmt.Sprintf("sql/%v.sql", time.Now().Unix())
t, err := cmd.Flags().GetString("component")
if err != nil {
log.Fatal(err)
}

if t != "server" && t != "agent" {
log.Fatalf("Invalid component %s. Must be one of: server or agent", t)
}

fileName := fmt.Sprintf("sql/%s/%v.sql", mapping[component], time.Now().Unix())
f, err := os.Create(fileName)
if err != nil {
log.Fatal(err)
Expand All @@ -124,5 +175,7 @@ func addCreateCommand() *cobra.Command {
},
}

cmd.Flags().StringVarP(&component, "component", "c", "server", "The component to create for: (server|agent)")

return cmd
}
2 changes: 2 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,8 @@ var DefaultConfiguration = Configuration{
type DatabaseConfiguration struct {
Type DatabaseProvider `json:"type" envconfig:"CONVOY_DB_TYPE"`

SqliteDB string `json:"sqlite_db" envconfig:"CONVOY_SQLITE_DB"`

Scheme string `json:"scheme" envconfig:"CONVOY_DB_SCHEME"`
Host string `json:"host" envconfig:"CONVOY_DB_HOST"`
Username string `json:"username" envconfig:"CONVOY_DB_USERNAME"`
Expand Down
Loading
Loading