diff --git a/sqldb/sqlc/migration.sql.go b/sqldb/sqlc/migration.sql.go new file mode 100644 index 000000000..d65ff74f5 --- /dev/null +++ b/sqldb/sqlc/migration.sql.go @@ -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 +} diff --git a/sqldb/sqlc/migrations/000005_migration_tracker.down.sql b/sqldb/sqlc/migrations/000005_migration_tracker.down.sql new file mode 100644 index 000000000..5f86e385c --- /dev/null +++ b/sqldb/sqlc/migrations/000005_migration_tracker.down.sql @@ -0,0 +1 @@ +DROP TABLE IF EXISTS migration_tracker; diff --git a/sqldb/sqlc/migrations/000005_migration_tracker.up.sql b/sqldb/sqlc/migrations/000005_migration_tracker.up.sql new file mode 100644 index 000000000..fa55812b5 --- /dev/null +++ b/sqldb/sqlc/migrations/000005_migration_tracker.up.sql @@ -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 +); + diff --git a/sqldb/sqlc/models.go b/sqldb/sqlc/models.go index 83be5a708..1476d0470 100644 --- a/sqldb/sqlc/models.go +++ b/sqldb/sqlc/models.go @@ -91,3 +91,8 @@ type InvoiceSequence struct { Name string CurrentValue int64 } + +type MigrationTracker struct { + Version int32 + MigrationTime time.Time +} diff --git a/sqldb/sqlc/querier.go b/sqldb/sqlc/querier.go index 04b61c700..268ca027c 100644 --- a/sqldb/sqlc/querier.go +++ b/sqldb/sqlc/querier.go @@ -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) diff --git a/sqldb/sqlc/queries/migration.sql b/sqldb/sqlc/queries/migration.sql new file mode 100644 index 000000000..aed90d193 --- /dev/null +++ b/sqldb/sqlc/queries/migration.sql @@ -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;