Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support multiple queries for clickhouse using up/down-sections #589

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 31 additions & 12 deletions pkg/dbmate/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,12 +385,17 @@ func (db *DB) Migrate() error {
}

execMigration := func(tx dbutil.Transaction) error {
// run actual migration
result, err := tx.Exec(parsed.Up)
if err != nil {
return drv.QueryError(parsed.Up, err)
} else if db.Verbose {
db.printVerbose(result)
if len(parsed.UpSections) != 0 {
for _, section := range parsed.UpSections {
if err = db.execQuery(drv, tx, section); err != nil {
return err
}
}
} else {
// run actual migration
if err = db.execQuery(drv, tx, parsed.Up); err != nil {
return err
}
}

// record migration
Expand Down Expand Up @@ -554,12 +559,17 @@ func (db *DB) Rollback() error {
}

execMigration := func(tx dbutil.Transaction) error {
// rollback migration
result, err := tx.Exec(parsed.Down)
if err != nil {
return drv.QueryError(parsed.Down, err)
} else if db.Verbose {
db.printVerbose(result)
if len(parsed.DownSections) != 0 {
for _, section := range parsed.DownSections {
if err = db.execQuery(drv, tx, section); err != nil {
return err
}
}
} else {
// rollback migration
if err = db.execQuery(drv, tx, parsed.Down); err != nil {
return err
}
}

// remove migration record
Expand Down Expand Up @@ -589,6 +599,15 @@ func (db *DB) Rollback() error {
return nil
}

func (db *DB) execQuery(drv Driver, tx dbutil.Transaction, query string) error {
if result, err := tx.Exec(query); err != nil {
return drv.QueryError(query, err)
} else if db.Verbose {
db.printVerbose(result)
}
return nil
}

// Status shows the status of all migrations
func (db *DB) Status(quiet bool) (int, error) {
results, err := db.FindMigrations()
Expand Down
53 changes: 44 additions & 9 deletions pkg/dbmate/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,17 @@ func (m *Migration) Parse() (*ParsedMigration, error) {
}

// ParsedMigration contains the migration contents and options
// It should be only one query pro up- or down-section provided,
// but it might have multiple sections.
// If the `UpSections` or `DownSections` are not empty,
// they are applied instead of `Up` or `Down`.
type ParsedMigration struct {
Up string
UpOptions ParsedMigrationOptions
Down string
DownOptions ParsedMigrationOptions
Up string
UpSections []string
UpOptions ParsedMigrationOptions
Down string
DownSections []string
DownOptions ParsedMigrationOptions
}

// ParsedMigrationOptions is an interface for accessing migration options
Expand All @@ -60,7 +66,9 @@ func (m migrationOptions) Transaction() bool {

var (
upRegExp = regexp.MustCompile(`(?m)^--\s*migrate:up(\s*$|\s+\S+)`)
upSectionRegExp = regexp.MustCompile(`(?m)^--\s*migrate:up-section(\s*$|\s+\S+)`)
downRegExp = regexp.MustCompile(`(?m)^--\s*migrate:down(\s*$|\s+\S+)`)
downSectionRegExp = regexp.MustCompile(`(?m)^--\s*migrate:down-section(\s*$|\s+\S+)`)
emptyLineRegExp = regexp.MustCompile(`^\s*$`)
commentLineRegExp = regexp.MustCompile(`^\s*--`)
whitespaceRegExp = regexp.MustCompile(`\s+`)
Expand All @@ -80,7 +88,9 @@ var (
// It will return two Migration objects, the first representing the "up"
// block and the second representing the "down" block. This function
// requires that at least an up block was defined and will otherwise
// return an error.
// return an error. It returns also UpSections or DownSections (default: empty arrays).
// If UpSections or DownSections are not empty, they will be executed instead of
// "up" or "down" blocks.
func parseMigrationContents(contents string) (*ParsedMigration, error) {
upDirectiveStart, hasDefinedUpBlock := getMatchPosition(contents, upRegExp)
downDirectiveStart, hasDefinedDownBlock := getMatchPosition(contents, downRegExp)
Expand All @@ -101,15 +111,40 @@ func parseMigrationContents(contents string) (*ParsedMigration, error) {
upBlock := substring(contents, upDirectiveStart, downDirectiveStart)
downBlock := substring(contents, downDirectiveStart, len(contents))

upSections := parseMigrationSections(upBlock, downDirectiveStart, upSectionRegExp)
downSections := parseMigrationSections(downBlock, len(downBlock), downSectionRegExp)

parsed := ParsedMigration{
Up: upBlock,
UpOptions: parseMigrationOptions(upBlock),
Down: downBlock,
DownOptions: parseMigrationOptions(downBlock),
Up: upBlock,
UpSections: upSections,
UpOptions: parseMigrationOptions(upBlock),
Down: downBlock,
DownSections: downSections,
DownOptions: parseMigrationOptions(downBlock),
}
return &parsed, nil
}

func parseMigrationSections(block string, blockDirectiveEnds int, sectionRegExp *regexp.Regexp) []string {
sectionDirectiveStart, hasDefinedSectionBlocks := getMatchPosition(block, sectionRegExp)
if !hasDefinedSectionBlocks {
return []string{}
}

sectionBlocks := substring(block, sectionDirectiveStart, blockDirectiveEnds)

sectionIdx := sectionRegExp.FindStringIndex(sectionBlocks)[1]
sections := sectionRegExp.Split(sectionBlocks[sectionIdx:], -1)
cleanSections := make([]string, 0)
for _, section := range sections {
section = strings.ReplaceAll(strings.TrimSpace(section), "\n", "")
if section != "" {
cleanSections = append(cleanSections, section)
}
}
return cleanSections
}

// parseMigrationOptions parses the migration options out of a block
// directive into an object that implements the MigrationOptions interface.
//
Expand Down
36 changes: 36 additions & 0 deletions pkg/dbmate/migration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,40 @@ DROP COLUMN status;
require.Equal(t, true, parsed.DownOptions.Transaction())
})
})

t.Run("support up and down sections", func(t *testing.T) {
migration := `-- migrate:up
-- migrate:up-section
create table users (id serial, name text);
-- migrate:up-section
select * from users;
-- migrate:down
-- migrate:down-section
drop table users;`

parsed, err := parseMigrationContents(migration)
require.Nil(t, err)

require.Equal(t, len(parsed.UpSections), 2)
require.Equal(t, "create table users (id serial, name text);", parsed.UpSections[0])
require.Equal(t, "select * from users;", parsed.UpSections[1])

require.Equal(t, len(parsed.DownSections), 1)
require.Equal(t, "drop table users;", parsed.DownSections[0])
})

t.Run("ignore empty up and down sections", func(t *testing.T) {
migration := `-- migrate:up
-- migrate:up-section
-- migrate:up-section
-- migrate:down
-- migrate:down-section
`

parsed, err := parseMigrationContents(migration)
require.Nil(t, err)

require.Equal(t, len(parsed.UpSections), 0)
require.Equal(t, len(parsed.DownSections), 0)
})
}
2 changes: 1 addition & 1 deletion pkg/dbmate/version.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package dbmate

// Version of dbmate
const Version = "2.21.0"
const Version = "2.22.0"