sqldb: add table to track custom SQL migrations

This commit adds the migration_tracker table which we'll use to track if
a custom migration has already been done.
This commit is contained in:
Andras Banki-Horvath
2024-11-12 16:25:29 +01:00
parent 680394518f
commit 9acd06d296
6 changed files with 108 additions and 0 deletions

View File

@ -0,0 +1,60 @@
// Code generated by sqlc. DO NOT EDIT.
// versions:
// sqlc v1.25.0
// source: migration.sql
package sqlc
import (
"context"
"time"
)
const getDatabaseVersion = `-- name: GetDatabaseVersion :one
SELECT
version
FROM
migration_tracker
ORDER BY
version DESC
LIMIT 1
`
func (q *Queries) GetDatabaseVersion(ctx context.Context) (int32, error) {
row := q.db.QueryRowContext(ctx, getDatabaseVersion)
var version int32
err := row.Scan(&version)
return version, err
}
const getMigration = `-- name: GetMigration :one
SELECT
migration_time
FROM
migration_tracker
WHERE
version = $1
`
func (q *Queries) GetMigration(ctx context.Context, version int32) (time.Time, error) {
row := q.db.QueryRowContext(ctx, getMigration, version)
var migration_time time.Time
err := row.Scan(&migration_time)
return migration_time, err
}
const setMigration = `-- name: SetMigration :exec
INSERT INTO
migration_tracker (version, migration_time)
VALUES ($1, $2)
`
type SetMigrationParams struct {
Version int32
MigrationTime time.Time
}
func (q *Queries) SetMigration(ctx context.Context, arg SetMigrationParams) error {
_, err := q.db.ExecContext(ctx, setMigration, arg.Version, arg.MigrationTime)
return err
}

View File

@ -0,0 +1 @@
DROP TABLE IF EXISTS migration_tracker;

View File

@ -0,0 +1,17 @@
-- The migration_tracker table keeps track of migrations that have been applied
-- to the database. This table ensures that migrations are idempotent and are
-- only run once. It tracks a global database version that encompasses both
-- schema migrations handled by golang-migrate and custom in-code migrations
-- for more complex data conversions that cannot be expressed in pure SQL.
CREATE TABLE IF NOT EXISTS migration_tracker (
-- version is the global version of the migration. Note that we
-- intentionally don't set it as PRIMARY KEY as it'd auto increment on
-- SQLite and our sqlc workflow will replace it with an auto
-- incrementing SERIAL on Postgres too. UNIQUE achieves the same effect
-- without the auto increment.
version INTEGER UNIQUE NOT NULL,
-- migration_time is the timestamp at which the migration was run.
migration_time TIMESTAMP NOT NULL
);

View File

@ -91,3 +91,8 @@ type InvoiceSequence struct {
Name string
CurrentValue int64
}
type MigrationTracker struct {
Version int32
MigrationTime time.Time
}

View File

@ -7,6 +7,7 @@ package sqlc
import (
"context"
"database/sql"
"time"
)
type Querier interface {
@ -17,6 +18,7 @@ type Querier interface {
FetchSettledAMPSubInvoices(ctx context.Context, arg FetchSettledAMPSubInvoicesParams) ([]FetchSettledAMPSubInvoicesRow, error)
FilterInvoices(ctx context.Context, arg FilterInvoicesParams) ([]Invoice, error)
GetAMPInvoiceID(ctx context.Context, setID []byte) (int64, error)
GetDatabaseVersion(ctx context.Context) (int32, error)
// This method may return more than one invoice if filter using multiple fields
// from different invoices. It is the caller's responsibility to ensure that
// we bubble up an error in those cases.
@ -25,6 +27,7 @@ type Querier interface {
GetInvoiceFeatures(ctx context.Context, invoiceID int64) ([]InvoiceFeature, error)
GetInvoiceHTLCCustomRecords(ctx context.Context, invoiceID int64) ([]GetInvoiceHTLCCustomRecordsRow, error)
GetInvoiceHTLCs(ctx context.Context, invoiceID int64) ([]InvoiceHtlc, error)
GetMigration(ctx context.Context, version int32) (time.Time, error)
InsertAMPSubInvoiceHTLC(ctx context.Context, arg InsertAMPSubInvoiceHTLCParams) error
InsertInvoice(ctx context.Context, arg InsertInvoiceParams) (int64, error)
InsertInvoiceFeature(ctx context.Context, arg InsertInvoiceFeatureParams) error
@ -37,6 +40,7 @@ type Querier interface {
OnInvoiceCanceled(ctx context.Context, arg OnInvoiceCanceledParams) error
OnInvoiceCreated(ctx context.Context, arg OnInvoiceCreatedParams) error
OnInvoiceSettled(ctx context.Context, arg OnInvoiceSettledParams) error
SetMigration(ctx context.Context, arg SetMigrationParams) error
UpdateAMPSubInvoiceHTLCPreimage(ctx context.Context, arg UpdateAMPSubInvoiceHTLCPreimageParams) (sql.Result, error)
UpdateAMPSubInvoiceState(ctx context.Context, arg UpdateAMPSubInvoiceStateParams) error
UpdateInvoiceAmountPaid(ctx context.Context, arg UpdateInvoiceAmountPaidParams) (sql.Result, error)

View File

@ -0,0 +1,21 @@
-- name: SetMigration :exec
INSERT INTO
migration_tracker (version, migration_time)
VALUES ($1, $2);
-- name: GetMigration :one
SELECT
migration_time
FROM
migration_tracker
WHERE
version = $1;
-- name: GetDatabaseVersion :one
SELECT
version
FROM
migration_tracker
ORDER BY
version DESC
LIMIT 1;