Skip to content

Commit

Permalink
feat: let database generate creation date of ledgers
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 16, 2024
1 parent 95948cc commit 0264603
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
4 changes: 1 addition & 3 deletions internal/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type Ledger struct {
Configuration
ID int `json:"id" bun:"id,type:int,scanonly"`
Name string `json:"name" bun:"name,type:varchar(255),pk"`
AddedAt time.Time `json:"addedAt" bun:"addedat,type:timestamp"`
AddedAt time.Time `json:"addedAt" bun:"added_at,type:timestamp,nullzero"`
}

func (l Ledger) HasFeature(feature, value string) bool {
Expand Down Expand Up @@ -50,8 +50,6 @@ func New(name string, configuration Configuration) (*Ledger, error) {
return &Ledger{
Configuration: configuration,
Name: name,
// todo: get from database
AddedAt: time.Now(),
}, nil
}

Expand Down
5 changes: 3 additions & 2 deletions internal/storage/driver/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ func (d *Driver) createLedgerStore(ctx context.Context, db bun.IDB, ledger ledge

func (d *Driver) CreateLedger(ctx context.Context, l *ledger.Ledger) (*ledgerstore.Store, error) {

// start a transaction because we will need to create the schema and apply ledger migrations
tx, err := d.db.BeginTx(ctx, &sql.TxOptions{})
if err != nil {
return nil, fmt.Errorf("begin transaction: %w", err)
Expand All @@ -106,7 +107,7 @@ func (d *Driver) CreateLedger(ctx context.Context, l *ledger.Ledger) (*ledgersto
ret, err := d.db.NewInsert().
Model(l).
Ignore().
Returning("id").
Returning("id, added_at").
Exec(ctx)
if err != nil {
return nil, postgres.ResolveError(err)
Expand Down Expand Up @@ -185,7 +186,7 @@ func (d *Driver) ListLedgers(ctx context.Context, q ledgercontroller.ListLedgers
query := d.db.NewSelect().
Model(&ledger.Ledger{}).
Column("*").
Order("addedat asc")
Order("added_at asc")

return bunpaginate.UsingOffset[ledgercontroller.PaginatedQueryOptions[struct{}], ledger.Ledger](
ctx,
Expand Down
1 change: 1 addition & 0 deletions internal/storage/driver/driver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ func TestLedgersCreate(t *testing.T) {
_, err := driver.CreateLedger(ctx, &l)
require.NoError(t, err)
require.Equal(t, 1, l.ID)
require.NotEmpty(t, l.AddedAt)
}

func TestLedgersList(t *testing.T) {
Expand Down
16 changes: 16 additions & 0 deletions internal/storage/driver/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ func GetMigrator() *migrations.Migrator {
return err
},
},
migrations.Migration{
Name: "Generate addedat of table ledgers",
UpWithContext: func(ctx context.Context, tx bun.Tx) error {
_, err := tx.ExecContext(ctx, `
alter table _system.ledgers
alter column addedat type timestamp without time zone;
alter table _system.ledgers
alter column addedat set default (now() at time zone 'utc');
alter table _system.ledgers
rename column addedat to added_at;
`)
return err
},
},
)

return migrator
Expand Down

0 comments on commit 0264603

Please sign in to comment.