diff --git a/ch/testdata/snapshots/TestQuery-11 b/ch/testdata/snapshots/TestQuery-11 index af828e9..a551671 100644 --- a/ch/testdata/snapshots/TestQuery-11 +++ b/ch/testdata/snapshots/TestQuery-11 @@ -1 +1 @@ -DROP VIEW "hello_world" +DROP VIEW "view_name" diff --git a/ch/testdata/snapshots/TestQuery-12 b/ch/testdata/snapshots/TestQuery-12 index 94d0790..49597f0 100644 --- a/ch/testdata/snapshots/TestQuery-12 +++ b/ch/testdata/snapshots/TestQuery-12 @@ -1 +1 @@ -DROP VIEW IF EXISTS hello_world +DROP VIEW IF EXISTS view_name diff --git a/ch/testdata/snapshots/TestQuery-13 b/ch/testdata/snapshots/TestQuery-13 index 5c032c1..2d8c1d8 100644 --- a/ch/testdata/snapshots/TestQuery-13 +++ b/ch/testdata/snapshots/TestQuery-13 @@ -1 +1 @@ -CREATE VIEW "view_name"TO "dest_table" AS SELECT "col1", col1 AS alias FROM src_table AS alias +CREATE MATERIALIZED VIEW IF NOT EXISTS "view_name" TO "dest_table" AS SELECT "col1", col1 AS alias FROM src_table AS alias WHERE (foo = bar) GROUP BY "group1", group2, group3 ORDER BY order2, order3 diff --git a/chmigrate/migrator.go b/chmigrate/migrator.go index 45d886c..5dcbeae 100644 --- a/chmigrate/migrator.go +++ b/chmigrate/migrator.go @@ -155,11 +155,6 @@ func (m *Migrator) Migrate(ctx context.Context, opts ...MigrationOption) (*Migra return nil, err } - if err := m.Lock(ctx); err != nil { - return nil, err - } - defer m.Unlock(ctx) //nolint:errcheck - migrations, lastGroupID, err := m.migrationsWithStatus(ctx) if err != nil { return nil, err @@ -214,11 +209,6 @@ func (m *Migrator) Rollback(ctx context.Context, opts ...MigrationOption) (*Migr return nil, err } - if err := m.Lock(ctx); err != nil { - return nil, err - } - defer m.Unlock(ctx) //nolint:errcheck - migrations, err := m.MigrationsWithStatus(ctx) if err != nil { return nil, err diff --git a/example/migrations/main.go b/example/migrations/main.go index 333234c..03e5e90 100644 --- a/example/migrations/main.go +++ b/example/migrations/main.go @@ -49,15 +49,23 @@ func newDBCommand(db *ch.DB, migrator *chmigrate.Migrator) *cli.Command { Name: "migrate", Usage: "migrate database", Action: func(c *cli.Context) error { + if err := migrator.Lock(c.Context); err != nil { + return err + } + defer migrator.Unlock(c.Context) //nolint:errcheck + group, err := migrator.Migrate(c.Context) if err != nil { return err } + if group.IsZero() { fmt.Printf("there are no new migrations to run (database is up to date)\n") return nil } + fmt.Printf("migrated to %s\n", group) + return nil }, }, @@ -65,15 +73,23 @@ func newDBCommand(db *ch.DB, migrator *chmigrate.Migrator) *cli.Command { Name: "rollback", Usage: "rollback the last migration group", Action: func(c *cli.Context) error { + if err := migrator.Lock(c.Context); err != nil { + return err + } + defer migrator.Unlock(c.Context) //nolint:errcheck + group, err := migrator.Rollback(c.Context) if err != nil { return err } + if group.IsZero() { fmt.Printf("there are no groups to roll back\n") return nil } + fmt.Printf("rolled back %s\n", group) + return nil }, },