Skip to content

Commit

Permalink
Add migrate db cmd
Browse files Browse the repository at this point in the history
  • Loading branch information
owenthereal committed Jun 12, 2024
1 parent 6bca096 commit 6ea3f27
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 16 deletions.
114 changes: 114 additions & 0 deletions cmd/migrate/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package main

import (
"context"
"encoding/json"
"log"
"os"
"time"

cursoriterator "github.com/Eun/go-pgx-cursor-iterator/v2"
"github.com/jackc/pgx/v5/pgxpool"
_ "github.com/jackc/pgx/v5/stdlib"
"github.com/jmoiron/sqlx"
)

const (
upsertSnippetQuery = `INSERT INTO snippets (slug, json, query, options, created_at) VALUES (:slug, :json, :query, :options, :created_at) ON CONFLICT (slug) DO UPDATE SET json = :json, query = :query, options = :options, created_at = :created_at`
)

var optionsMap = map[string]string{
"slurp": "-s",
"null-input": "-n",
"compact-output": "-c",
"raw-output": "-r",
"raw-input": "-R",
}

type OldSnippet struct {
ID int `db:"id"`
J string `db:"j"`
Q string `db:"q"`
O string `db:"o"`
Slug string `db:"slug"`
CreatedAt time.Time `db:"created_at"`
}

type OldSnippetOption struct {
Name string `json:"name"`
Enabled bool `json:"enabled"`
}

type NewSnippet struct {
JSON string `db:"json"`
Query string `db:"query"`
Options []string `db:"options"`
Slug string `db:"slug"`
CreatedAt time.Time `db:"created_at"`
}

func main() {
sourceURL := os.Getenv("DATABASE_URL")
targetURL := os.Getenv("TARGET_DATABASE_URL")
if sourceURL == "" || targetURL == "" {
panic("DATABASE_URL and TARGET_DATABASE_URL must be set")
}

ctx := context.Background()
pool, err := pgxpool.New(ctx, sourceURL)
if err != nil {
log.Fatal(err)
}

db, err := sqlx.Connect("pgx", targetURL)
if err != nil {
log.Fatal(err)
}

olds := make([]OldSnippet, 1000)
iter, err := cursoriterator.NewCursorIterator(pool, olds, "SELECT * FROM snippets")
if err != nil {
panic(err)
}
defer iter.Close(ctx)

for iter.Next(ctx) {
old := olds[iter.ValueIndex()]

var oldOptions []OldSnippetOption
if err := json.Unmarshal([]byte(old.O), &oldOptions); err != nil {
log.Printf("error unmarshalling options: %v", err)
continue
}

var newOptions []string
for _, oldOption := range oldOptions {
if oldOption.Enabled {
newOpt, ok := optionsMap[oldOption.Name]
if ok {
newOptions = append(newOptions, newOpt)
} else {
log.Printf("unknown option: %s %s", old.Slug, oldOption.Name)
}
}
}

new := NewSnippet{
JSON: old.J,
Query: old.Q,
Options: newOptions,
Slug: old.Slug,
CreatedAt: old.CreatedAt,
}
_, err := db.NamedExec(upsertSnippetQuery,
new,
)
if err != nil {
log.Fatal(err)
}
}

if err := iter.Error(); err != nil {
log.Fatal(err)
}
}
Binary file added cmd/migrate/migrate
Binary file not shown.
9 changes: 5 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module github.com/owenthereal/jqplay
go 1.21

require (
github.com/Eun/go-pgx-cursor-iterator/v2 v2.0.1
github.com/getsentry/sentry-go v0.27.0
github.com/gin-gonic/gin v1.9.1
github.com/google/uuid v1.6.0
Expand All @@ -21,26 +22,26 @@ require (
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/gabriel-vasile/mimetype v1.4.2 // indirect
github.com/georgysavva/scany/v2 v2.1.0 // indirect
github.com/gin-contrib/sse v0.1.0 // indirect
github.com/go-playground/locales v0.14.1 // indirect
github.com/go-playground/universal-translator v0.18.1 // indirect
github.com/go-playground/validator/v10 v10.14.0 // indirect
github.com/goccy/go-json v0.10.2 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/kr/text v0.2.0 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/lib/pq v1.10.2 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-sqlite3 v1.14.12 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/pelletier/go-toml/v2 v2.0.8 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rogpeppe/go-internal v1.6.1 // indirect
github.com/samber/lo v1.38.1 // indirect
github.com/samber/slog-common v0.15.2 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
Expand All @@ -49,7 +50,7 @@ require (
golang.org/x/crypto v0.21.0 // indirect
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 // indirect
golang.org/x/net v0.23.0 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sync v0.6.0 // indirect
golang.org/x/sys v0.18.0 // indirect
golang.org/x/text v0.14.0 // indirect
google.golang.org/protobuf v1.33.0 // indirect
Expand Down
Loading

0 comments on commit 6ea3f27

Please sign in to comment.