Skip to content

Commit

Permalink
migrate to json encoding for the events
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanfaerman committed Jan 31, 2024
1 parent a365261 commit 586062e
Show file tree
Hide file tree
Showing 7 changed files with 203 additions and 2 deletions.
42 changes: 42 additions & 0 deletions internal/dao/events.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions internal/sql/migrations/0008_add_event_data_json.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- add_event_data_json
-- +goose Up
-- +goose StatementBegin

ALTER TABLE events
ADD COLUMN event_data_json BLOB NOT NULL;
-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin

ALTER TABLE events
DROP COLUMN event_data_json;
-- +goose StatementEnd

79 changes: 79 additions & 0 deletions internal/sql/migrations/0009_migrate_events_data_to_json.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package migrations

import (
"bytes"
"context"
"database/sql"
"encoding/gob"
"encoding/json"
"errors"
"fmt"
)

func init() {
fmt.Println("hello world")
AddMigration(Up0009, Down0009)
}

func Up0009(ctx context.Context, tx *sql.Tx) error {
l := Log.With("migration", "0009", "direction", "up")

l.Debug("starting migration")

rows, err := tx.QueryContext(ctx, `select id, event_data from events`)
if err != nil {
return err
}
defer rows.Close()

var decoder *gob.Decoder

for rows.Next() {
var (
id int64
data []byte
)
if err := rows.Scan(&id, &data); err != nil {
return err
}

l := l.With("event.id", id)

l.Debug("decoding event data")
decoder = gob.NewDecoder(bytes.NewReader(data))
var p any
if err := decoder.Decode(&p); err != nil {
l.Error("unable to decode event", "error", err)
return err
}

l.Debug("marshaling event data to json")
jsonData, err := json.Marshal(p)
if err != nil {
return err
}

l.Debug("updating event data json")
if _, err := tx.ExecContext(
ctx,
`update events set event_data_json = ? where id = ?`,
jsonData, id,
); err != nil {
return err
}
l.Debug("updated event data json")

}
if err := rows.Close(); err != nil {
return err
}
if err := rows.Err(); err != nil {
return err
}

return nil
}

func Down0009(ctx context.Context, tx *sql.Tx) error {
return errors.New("cannot downgrade")
}
17 changes: 17 additions & 0 deletions internal/sql/migrations/0010_replace_event_data_with_json.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- replace_event_data_with_json
-- +goose Up
-- +goose StatementBegin

ALTER TABLE events
DROP COLUMN event_data;

ALTER TABLE events
RENAME COLUMN event_data_json TO event_data;

-- +goose StatementEnd

-- +goose Down
-- +goose StatementBegin

-- +goose StatementEnd

32 changes: 32 additions & 0 deletions internal/sql/migrations/migrations.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package migrations

import (
"runtime"

"github.com/pressly/goose/v3"

"github.com/charmbracelet/log"
)

// A migrations file is a Go file that perform a migration. It must start with
// a number, followed by an underscore and a description of the migration.
type Migration struct {
Up goose.GoMigrationContext
Down goose.GoMigrationContext
Name string
}

var (
Migrations []Migration
Log *log.Logger
)

// Add a migration to the list of migrations to run
func AddMigration(up, down goose.GoMigrationContext) {
_, filename, _, _ := runtime.Caller(1)
Migrations = append(Migrations, Migration{
Name: filename,
Up: up,
Down: down,
})
}
6 changes: 6 additions & 0 deletions internal/sql/queries/events.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,9 @@ INSERT INTO events_recovery (

-- name: DeleteEventRecovery :exec
DELETE FROM events_recovery WHERE id = ?1;

-- name: GetEventsForCallsign :many
SELECT *
FROM events
WHERE event_type = ?1
AND json_extract(event_data, '$.Callsign') = @callsign;
14 changes: 12 additions & 2 deletions internal/sql/sql.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
package sql

import (
"context"
"database/sql"
"embed"

"github.com/charmbracelet/log"
"github.com/pressly/goose/v3"

_ "github.com/glebarez/go-sqlite"
goMigrations "github.com/ryanfaerman/netctl/internal/sql/migrations"
)

//go:generate sqlc generate
Expand All @@ -14,7 +18,8 @@ import (
var migrations embed.FS

func RunMigrations(log *log.Logger, db *sql.DB) error {
l := log.With("pgk", "sql")
l := log.With("pkg", "sql")
goMigrations.Log = log.With("pkg", "go-migrations")

goose.SetLogger(logAdapter{*l})
goose.SetBaseFS(migrations)
Expand All @@ -23,5 +28,10 @@ func RunMigrations(log *log.Logger, db *sql.DB) error {
return err
}

return goose.Up(db, "migrations")
// Add these migrations manually
for _, migration := range goMigrations.Migrations {
goose.AddNamedMigrationContext(migration.Name, migration.Up, migration.Down)
}

return goose.UpContext(context.Background(), db, "migrations")
}

0 comments on commit 586062e

Please sign in to comment.