database

package
v0.0.0-...-0895702 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 6, 2026 License: AGPL-3.0 Imports: 18 Imported by: 0

README

Postgres Database management

Introduction

The document provides guidance on how to effectively use Postgres whithin the 40swap daemon. Specifically, we'll cover database migrations and code generation.

Schema-First approach

To create or modify and later mapping from the Database to our models, the starting point is the migrations. Models are generated out of the schema so the flow is as follows:

create migration > generate models

Create a migration

Migrations are defined in migrations.go and they are executed in order. Migrations define the desired state of the database at certain point in time, every modification to the db schema happen exclusively in this file.

Refer to gorm migrations to learn how to write migrations.

Generate models

Model generation is streamlined via just generate (which relies on go generate) command. It's important to notice that in order to run this command you'll need to have a running instance of the database with the latest migrations applied. To disable model generation while running go generate you can just generate -tags=no_sql_gen ./....

The models can be customized fully before generating them, generation rules are defined in generate.go.

Documentation

Overview

The idea behing this is that you might not always want to generate db models after each go generate invocation, mainly because model generation requires a running and up-to-date database instance. By default go generate will try to create/update DB models.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddColumnRefundRequested

func AddColumnRefundRequested() *gormigrate.Migration

This migration adds the `RefundRequestedAt` column to the `SwapIn` table

func AddContractFieldsToSwapOut

func AddContractFieldsToSwapOut() *gormigrate.Migration

func AddIsAutoSwapToSwapOut

func AddIsAutoSwapToSwapOut() *gormigrate.Migration

func AddLockTxIdToSwapIn

func AddLockTxIdToSwapIn() *gormigrate.Migration

func AddPreimageTxIdTimeoutBlockHeightToSwapOut

func AddPreimageTxIdTimeoutBlockHeightToSwapOut() *gormigrate.Migration

func ChangeNameClaimPubkey

func ChangeNameClaimPubkey() *gormigrate.Migration

func CreateSwapsTables

func CreateSwapsTables() *gormigrate.Migration

func DropClaimTxForSwapIns

func DropClaimTxForSwapIns() *gormigrate.Migration

func RemoveNotNullInOutcome

func RemoveNotNullInOutcome() *gormigrate.Migration

This migration removes the `not null` from the `Outcome` field

func RemoveNotNullSwapOut

func RemoveNotNullSwapOut() *gormigrate.Migration

func RenameOnchainFeeSatsAndAddCreatedAndUpdatedAt

func RenameOnchainFeeSatsAndAddCreatedAndUpdatedAt() *gormigrate.Migration

Types

type Database

type Database struct {
	// contains filtered or unexported fields
}

func New

func New(username, password, database string, port uint32, dataPath, host string, keepAlive bool) (*Database, func() error, error)

func (*Database) Generate

func (d *Database) Generate(path string) error

func (*Database) GetConnectionURL

func (d *Database) GetConnectionURL() string

func (*Database) GetPendingAutoSwapOuts

func (d *Database) GetPendingAutoSwapOuts(ctx context.Context) ([]*models.SwapOut, error)

func (*Database) GetPendingSwapIns

func (d *Database) GetPendingSwapIns(ctx context.Context) ([]*models.SwapIn, error)

func (*Database) GetPendingSwapOuts

func (d *Database) GetPendingSwapOuts(ctx context.Context) ([]*models.SwapOut, error)

func (*Database) GetSwapIn

func (d *Database) GetSwapIn(ctx context.Context, swapID string) (*models.SwapIn, error)

func (*Database) GetSwapInByClaimAddress

func (d *Database) GetSwapInByClaimAddress(ctx context.Context, address string) (*models.SwapIn, error)

func (*Database) GetSwapOut

func (d *Database) GetSwapOut(ctx context.Context, swapID string) (*models.SwapOut, error)

func (*Database) MigrateDatabase

func (d *Database) MigrateDatabase() error

func (*Database) MigrateTo

func (d *Database) MigrateTo(to string) error

func (*Database) ORM

func (d *Database) ORM() *gorm.DB

func (*Database) Reset

func (d *Database) Reset() error

Reset will WIPE all tables on the database. Use it carefully.

func (*Database) Rollback

func (d *Database) Rollback() error

func (*Database) SaveSwapIn

func (d *Database) SaveSwapIn(ctx context.Context, swapIn *models.SwapIn) error

func (*Database) SaveSwapOut

func (d *Database) SaveSwapOut(ctx context.Context, swapOut *models.SwapOut) error

func (*Database) UpdateAutoSwap

func (d *Database) UpdateAutoSwap(ctx context.Context, swapID string, isAutoSwap bool) error

type Migrator

type Migrator struct {
	// contains filtered or unexported fields
}

func NewMigrator

func NewMigrator(db *gorm.DB) *Migrator

func (*Migrator) Migrate

func (m *Migrator) Migrate() error

func (*Migrator) MigrateTo

func (m *Migrator) MigrateTo(id string) error

func (*Migrator) Reset

func (m *Migrator) Reset() error

Reset will only rollback the DB to its initial state, this is no tables.

func (*Migrator) Rollback

func (m *Migrator) Rollback() error

type SwapInRepository

type SwapInRepository interface {
	SaveSwapIn(ctx context.Context, swapIn *models.SwapIn) error
	GetPendingSwapIns(ctx context.Context) ([]*models.SwapIn, error)
	GetSwapIn(ctx context.Context, swapID string) (*models.SwapIn, error)
	GetSwapInByClaimAddress(ctx context.Context, address string) (*models.SwapIn, error)
}

type SwapOutRepository

type SwapOutRepository interface {
	SaveSwapOut(ctx context.Context, swapOut *models.SwapOut) error
	GetPendingSwapOuts(ctx context.Context) ([]*models.SwapOut, error)
	GetSwapOut(ctx context.Context, swapID string) (*models.SwapOut, error)
	GetPendingAutoSwapOuts(ctx context.Context) ([]*models.SwapOut, error)
	UpdateAutoSwap(ctx context.Context, swapID string, isAutoSwap bool) error
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL