Splitting of sql is now more robust and better tested

This commit is contained in:
Till Klocke 2016-06-16 14:34:50 +02:00
parent 6612806e06
commit cd5f75d47c
2 changed files with 42 additions and 9 deletions

View File

@ -72,16 +72,12 @@ func (driver *Driver) Migrate(f file.File, pipe chan interface{}) {
return
}
lines := strings.Split(string(f.Content), ";")
lines := splitContent(string(f.Content))
for _, line := range lines {
query := strings.TrimSpace(line)
query = strings.Replace(query, ";", "", -1)
if query != "" {
_, err := driver.db.Exec(query)
if err != nil {
pipe <- err
return
}
_, err := driver.db.Exec(line)
if err != nil {
pipe <- err
return
}
}
@ -98,6 +94,19 @@ func (driver *Driver) Migrate(f file.File, pipe chan interface{}) {
}
}
func splitContent(content string) []string {
lines := strings.Split(content, ";")
resultLines := make([]string, 0, len(lines))
for i, line := range lines {
line = strings.Replace(lines[i], ";", "", -1)
line = strings.TrimSpace(line)
if line != "" {
resultLines = append(resultLines, line)
}
}
return resultLines
}
func (driver *Driver) ensureVersionTableExists() error {
if _, err := driver.db.Exec(fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (version INTEGER PRIMARY KEY)", tableName)); err != nil {
return err

View File

@ -10,6 +10,30 @@ import (
pipep "github.com/mattes/migrate/pipe"
)
func TestContentSplit(t *testing.T) {
content := `CREATE TABLE users (user_id STRING primary key, first_name STRING, last_name STRING, email STRING, password_hash STRING) CLUSTERED INTO 3 shards WITH (number_of_replicas = 0);
CREATE TABLE units (unit_id STRING primary key, name STRING, members array(string)) CLUSTERED INTO 3 shards WITH (number_of_replicas = 0);
CREATE TABLE available_connectors (technology_id STRING primary key, description STRING, icon STRING, link STRING, configuration_parameters array(object as (name STRING, type STRING))) CLUSTERED INTO 3 shards WITH (number_of_replicas = 0);
`
lines := splitContent(content)
if len(lines) != 3 {
t.Errorf("Expected 3 lines, but got %d", len(lines))
}
if lines[0] != "CREATE TABLE users (user_id STRING primary key, first_name STRING, last_name STRING, email STRING, password_hash STRING) CLUSTERED INTO 3 shards WITH (number_of_replicas = 0)" {
t.Error("Line does not match expected output")
}
if lines[1] != "CREATE TABLE units (unit_id STRING primary key, name STRING, members array(string)) CLUSTERED INTO 3 shards WITH (number_of_replicas = 0)" {
t.Error("Line does not match expected output")
}
if lines[2] != "CREATE TABLE available_connectors (technology_id STRING primary key, description STRING, icon STRING, link STRING, configuration_parameters array(object as (name STRING, type STRING))) CLUSTERED INTO 3 shards WITH (number_of_replicas = 0)" {
t.Error("Line does not match expected output")
}
}
func TestMigrate(t *testing.T) {
host := os.Getenv("CRATE_PORT_4200_TCP_ADDR")
port := os.Getenv("CRATE_PORT_4200_TCP_PORT")