From 2c9485da7fe54fdfb66544c20e7eb579ca704f20 Mon Sep 17 00:00:00 2001 From: Vladimir Mihailenco Date: Sat, 27 Aug 2022 10:07:07 +0300 Subject: [PATCH] chmigrate: add MissingMigrations --- ch/db_test.go | 19 +++++++++++++++++++ chmigrate/migrator.go | 29 ++++++++++++++++++++++++++--- 2 files changed, 45 insertions(+), 3 deletions(-) diff --git a/ch/db_test.go b/ch/db_test.go index 6aaed58..c815255 100644 --- a/ch/db_test.go +++ b/ch/db_test.go @@ -294,6 +294,25 @@ func TestDateTime64(t *testing.T) { require.Equal(t, in.Time.UnixNano(), out.Time.UnixNano()) } +func TestInvalidType(t *testing.T) { + t.Skip() + + ctx := context.Background() + + db := chDB() + defer db.Close() + + var dest struct { + Numbers []float32 + } + err := db.NewSelect(). + ColumnExpr("groupArray(number) AS numbers"). + TableExpr("numbers(10)"). + Scan(ctx, &dest) + require.NoError(t, err) + require.Equal(t, []float64{}, dest.Numbers) +} + type Event struct { ch.CHModel `ch:"goch_events,partition:toYYYYMM(created_at)"` diff --git a/chmigrate/migrator.go b/chmigrate/migrator.go index 0777721..45d886c 100644 --- a/chmigrate/migrator.go +++ b/chmigrate/migrator.go @@ -82,7 +82,7 @@ func (m *Migrator) MigrationsWithStatus(ctx context.Context) (MigrationSlice, er func (m *Migrator) migrationsWithStatus(ctx context.Context) (MigrationSlice, int64, error) { sorted := m.migrations.Sorted() - applied, err := m.selectAppliedMigrations(ctx) + applied, err := m.AppliedMigrations(ctx) if err != nil { return nil, 0, err } @@ -377,8 +377,31 @@ func (m *Migrator) MarkUnapplied(ctx context.Context, migration *Migration) erro return err } -// selectAppliedMigrations selects applied (applied) migrations in descending order. -func (m *Migrator) selectAppliedMigrations(ctx context.Context) (MigrationSlice, error) { +func (m *Migrator) TruncateTable(ctx context.Context) error { + _, err := m.db.Exec("TRUNCATE TABLE ?", ch.Ident(m.table)) + return err +} + +// MissingMigrations returns applied migrations that can no longer be found. +func (m *Migrator) MissingMigrations(ctx context.Context) (MigrationSlice, error) { + applied, err := m.AppliedMigrations(ctx) + if err != nil { + return nil, err + } + + existing := migrationMap(m.migrations.ms) + for i := len(applied) - 1; i >= 0; i-- { + m := &applied[i] + if _, ok := existing[m.Name]; ok { + applied = append(applied[:i], applied[i+1:]...) + } + } + + return applied, nil +} + +// AppliedMigrations selects applied migrations in descending order. +func (m *Migrator) AppliedMigrations(ctx context.Context) (MigrationSlice, error) { var ms MigrationSlice if err := m.db.NewSelect(). ColumnExpr("*").