-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
migrate to json encoding for the events
- Loading branch information
1 parent
a365261
commit 586062e
Showing
7 changed files
with
203 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
79
internal/sql/migrations/0009_migrate_events_data_to_json.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
internal/sql/migrations/0010_replace_event_data_with_json.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters