1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283 |
- package sqlite
- import (
- "context"
- "strings"
- )
- type MigrationHistory struct {
- Version string
- CreatedTs int64
- }
- type MigrationHistoryUpsert struct {
- Version string
- }
- type MigrationHistoryFind struct {
- Version *string
- }
- func (d *DB) FindMigrationHistoryList(ctx context.Context, find *MigrationHistoryFind) ([]*MigrationHistory, error) {
- where, args := []string{"1 = 1"}, []any{}
- if v := find.Version; v != nil {
- where, args = append(where, "version = ?"), append(args, *v)
- }
- query := `
- SELECT
- version,
- created_ts
- FROM
- migration_history
- WHERE ` + strings.Join(where, " AND ") + `
- ORDER BY created_ts DESC
- `
- rows, err := d.db.QueryContext(ctx, query, args...)
- if err != nil {
- return nil, err
- }
- defer rows.Close()
- list := make([]*MigrationHistory, 0)
- for rows.Next() {
- var migrationHistory MigrationHistory
- if err := rows.Scan(
- &migrationHistory.Version,
- &migrationHistory.CreatedTs,
- ); err != nil {
- return nil, err
- }
- list = append(list, &migrationHistory)
- }
- if err := rows.Err(); err != nil {
- return nil, err
- }
- return list, nil
- }
- func (d *DB) UpsertMigrationHistory(ctx context.Context, upsert *MigrationHistoryUpsert) (*MigrationHistory, error) {
- stmt := `
- INSERT INTO migration_history (
- version
- )
- VALUES (?)
- ON CONFLICT(version) DO UPDATE
- SET
- version=EXCLUDED.version
- RETURNING version, created_ts
- `
- var migrationHistory MigrationHistory
- if err := d.db.QueryRowContext(ctx, stmt, upsert.Version).Scan(
- &migrationHistory.Version,
- &migrationHistory.CreatedTs,
- ); err != nil {
- return nil, err
- }
- return &migrationHistory, nil
- }
|