1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
| package migrate
import ( "context" "database/sql"
"github.com/jmoiron/sqlx" "github.com/pkg/errors"
"{{{ .Package }}}/app/lib/database" "{{{ .Package }}}/app/lib/filter" "{{{ .Package }}}/app/util" )
const ( migrationTable = "migration" migrationTableSQL = {{{ if .SQLServer }}}`if not exists (select * from sysobjects where name='migration' and xtype='U') create table migration ( "idx" int not null, "title" varchar(max) not null, "src" varchar(max) not null, "created" datetime not null, primary key (idx) );`{{{ else }}}`create table if not exists "migration" ( "idx" int not null primary key, "title" text not null, "src" text not null, "created" timestamp not null );`{{{ end }}} )
func ListMigrations(ctx context.Context, s *database.Service, params *filter.Params, tx *sqlx.Tx, logger util.Logger) Migrations { params = filter.ParamsWithDefaultOrdering(migrationTable, params, &filter.Ordering{Column: "created", Asc: false}) var rows []*migrationRow q := database.SQLSelect("*", migrationTable, "", params.OrderByString(), params.Limit, params.Offset, s.Placeholder()) err := s.Select(ctx, &rows, q, tx, logger) if err != nil { logger.Errorf("error retrieving migrations: %+v", err) return nil } return toMigrations(rows) }
func createMigrationTableIfNeeded(ctx context.Context, s *database.Service, tx *sqlx.Tx, logger util.Logger) error { q := database.SQLSelectSimple("count(*) as x", migrationTable, s.Placeholder()) _, err := s.SingleInt(ctx, q, tx, logger) if err != nil { logger.Info("first run, creating migration table") _, err = s.Exec(ctx, migrationTableSQL, nil, -1, logger) if err != nil { return errors.Wrapf(err, "error creating migration table: %+v", err) } } return nil }
func getMigrationByIdx(ctx context.Context, s *database.Service, idx int, tx *sqlx.Tx, logger util.Logger) *Migration { row := &migrationRow{} q := database.SQLSelectSimple("*", "migration", s.Placeholder(), "idx = {{{ .Placeholder 1 }}}") err := s.Get(ctx, row, q, tx, logger, idx) if err != nil { if errors.Is(err, sql.ErrNoRows) { return nil } logger.Errorf("error getting migration by idx [%v]: %+v", idx, err) return nil } return row.toMigration() }
func removeMigrationByIdx(ctx context.Context, s *database.Service, idx int, tx *sqlx.Tx, logger util.Logger) error { q := database.SQLDelete("migration", "idx = {{{ .Placeholder 1 }}}", s.Placeholder()) _, err := s.Delete(ctx, q, tx, 1, logger, idx) if err != nil { return errors.Wrap(err, "error removing migration") } return nil }
func newMigration(ctx context.Context, s *database.Service, e *Migration, tx *sqlx.Tx, logger util.Logger) error { q := database.SQLInsert("migration", []string{"idx", "title", "src", "created"}, 1, s.Placeholder()) return s.Insert(ctx, q, tx, logger, e.Idx, e.Title, e.Src, util.TimeCurrent()) }
func maxMigrationIdx(ctx context.Context, s *database.Service, tx *sqlx.Tx, logger util.Logger) int { q := database.SQLSelectSimple("max(idx) as x", "migration", s.Placeholder()) max, err := s.SingleInt(ctx, q, tx, logger) if err != nil { logger.Errorf("error getting migrations: %+v", err) return -1 } return int(max) }
|