Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/github/gh-ost into meiji1…
Browse files Browse the repository at this point in the history
…63/parallel-repl
  • Loading branch information
arthurschreiber committed Oct 21, 2024
2 parents 113e674 + 00f450d commit 8e38b86
Show file tree
Hide file tree
Showing 16 changed files with 477 additions and 558 deletions.
24 changes: 12 additions & 12 deletions go/base/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"time"

"github.com/openark/golib/log"
test "github.com/openark/golib/tests"
"github.com/stretchr/testify/require"
)

func init() {
Expand All @@ -22,22 +22,22 @@ func TestGetTableNames(t *testing.T) {
{
context := NewMigrationContext()
context.OriginalTableName = "some_table"
test.S(t).ExpectEquals(context.GetOldTableName(), "_some_table_del")
test.S(t).ExpectEquals(context.GetGhostTableName(), "_some_table_gho")
test.S(t).ExpectEquals(context.GetChangelogTableName(), "_some_table_ghc")
require.Equal(t, "_some_table_del", context.GetOldTableName())
require.Equal(t, "_some_table_gho", context.GetGhostTableName())
require.Equal(t, "_some_table_ghc", context.GetChangelogTableName(), "_some_table_ghc")
}
{
context := NewMigrationContext()
context.OriginalTableName = "a123456789012345678901234567890123456789012345678901234567890"
test.S(t).ExpectEquals(context.GetOldTableName(), "_a1234567890123456789012345678901234567890123456789012345678_del")
test.S(t).ExpectEquals(context.GetGhostTableName(), "_a1234567890123456789012345678901234567890123456789012345678_gho")
test.S(t).ExpectEquals(context.GetChangelogTableName(), "_a1234567890123456789012345678901234567890123456789012345678_ghc")
require.Equal(t, "_a1234567890123456789012345678901234567890123456789012345678_del", context.GetOldTableName())
require.Equal(t, "_a1234567890123456789012345678901234567890123456789012345678_gho", context.GetGhostTableName())
require.Equal(t, "_a1234567890123456789012345678901234567890123456789012345678_ghc", context.GetChangelogTableName())
}
{
context := NewMigrationContext()
context.OriginalTableName = "a123456789012345678901234567890123456789012345678901234567890123"
oldTableName := context.GetOldTableName()
test.S(t).ExpectEquals(oldTableName, "_a1234567890123456789012345678901234567890123456789012345678_del")
require.Equal(t, "_a1234567890123456789012345678901234567890123456789012345678_del", oldTableName)
}
{
context := NewMigrationContext()
Expand All @@ -46,15 +46,15 @@ func TestGetTableNames(t *testing.T) {
longForm := "Jan 2, 2006 at 3:04pm (MST)"
context.StartTime, _ = time.Parse(longForm, "Feb 3, 2013 at 7:54pm (PST)")
oldTableName := context.GetOldTableName()
test.S(t).ExpectEquals(oldTableName, "_a1234567890123456789012345678901234567890123_20130203195400_del")
require.Equal(t, "_a1234567890123456789012345678901234567890123_20130203195400_del", oldTableName)
}
{
context := NewMigrationContext()
context.OriginalTableName = "foo_bar_baz"
context.ForceTmpTableName = "tmp"
test.S(t).ExpectEquals(context.GetOldTableName(), "_tmp_del")
test.S(t).ExpectEquals(context.GetGhostTableName(), "_tmp_gho")
test.S(t).ExpectEquals(context.GetChangelogTableName(), "_tmp_ghc")
require.Equal(t, "_tmp_del", context.GetOldTableName())
require.Equal(t, "_tmp_gho", context.GetGhostTableName())
require.Equal(t, "_tmp_ghc", context.GetChangelogTableName())
}
}

Expand Down
22 changes: 11 additions & 11 deletions go/base/load_map_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"

"github.com/openark/golib/log"
test "github.com/openark/golib/tests"
"github.com/stretchr/testify/require"
)

func init() {
Expand All @@ -20,39 +20,39 @@ func TestParseLoadMap(t *testing.T) {
{
loadList := ""
m, err := ParseLoadMap(loadList)
test.S(t).ExpectNil(err)
test.S(t).ExpectEquals(len(m), 0)
require.NoError(t, err)
require.Len(t, m, 0)
}
{
loadList := "threads_running=20,threads_connected=10"
m, err := ParseLoadMap(loadList)
test.S(t).ExpectNil(err)
test.S(t).ExpectEquals(len(m), 2)
test.S(t).ExpectEquals(m["threads_running"], int64(20))
test.S(t).ExpectEquals(m["threads_connected"], int64(10))
require.NoError(t, err)
require.Len(t, m, 2)
require.Equal(t, int64(20), m["threads_running"])
require.Equal(t, int64(10), m["threads_connected"])
}
{
loadList := "threads_running=20=30,threads_connected=10"
_, err := ParseLoadMap(loadList)
test.S(t).ExpectNotNil(err)
require.Error(t, err)
}
{
loadList := "threads_running=20,threads_connected"
_, err := ParseLoadMap(loadList)
test.S(t).ExpectNotNil(err)
require.Error(t, err)
}
}

func TestString(t *testing.T) {
{
m, _ := ParseLoadMap("")
s := m.String()
test.S(t).ExpectEquals(s, "")
require.Equal(t, "", s)
}
{
loadList := "threads_running=20,threads_connected=10"
m, _ := ParseLoadMap(loadList)
s := m.String()
test.S(t).ExpectEquals(s, "threads_connected=10,threads_running=20")
require.Equal(t, "threads_connected=10,threads_running=20", s)
}
}
16 changes: 8 additions & 8 deletions go/base/utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"testing"

"github.com/openark/golib/log"
test "github.com/openark/golib/tests"
"github.com/stretchr/testify/require"
)

func init() {
Expand All @@ -19,11 +19,11 @@ func init() {
func TestStringContainsAll(t *testing.T) {
s := `insert,delete,update`

test.S(t).ExpectFalse(StringContainsAll(s))
test.S(t).ExpectFalse(StringContainsAll(s, ""))
test.S(t).ExpectFalse(StringContainsAll(s, "drop"))
test.S(t).ExpectTrue(StringContainsAll(s, "insert"))
test.S(t).ExpectFalse(StringContainsAll(s, "insert", "drop"))
test.S(t).ExpectTrue(StringContainsAll(s, "insert", ""))
test.S(t).ExpectTrue(StringContainsAll(s, "insert", "update", "delete"))
require.False(t, StringContainsAll(s))
require.False(t, StringContainsAll(s, ""))
require.False(t, StringContainsAll(s, "drop"))
require.True(t, StringContainsAll(s, "insert"))
require.False(t, StringContainsAll(s, "insert", "drop"))
require.True(t, StringContainsAll(s, "insert", ""))
require.True(t, StringContainsAll(s, "insert", "update", "delete"))
}
76 changes: 39 additions & 37 deletions go/logic/applier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"strings"
"testing"

test "github.com/openark/golib/tests"
"github.com/stretchr/testify/require"

"github.com/github/gh-ost/go/base"
"github.com/github/gh-ost/go/binlog"
Expand All @@ -21,33 +21,33 @@ func TestApplierGenerateSqlModeQuery(t *testing.T) {
applier := NewApplier(migrationContext)

{
test.S(t).ExpectEquals(
applier.generateSqlModeQuery(),
require.Equal(t,
`sql_mode = CONCAT(@@session.sql_mode, ',NO_AUTO_VALUE_ON_ZERO,STRICT_ALL_TABLES')`,
applier.generateSqlModeQuery(),
)
}
{
migrationContext.SkipStrictMode = true
migrationContext.AllowZeroInDate = false
test.S(t).ExpectEquals(
applier.generateSqlModeQuery(),
require.Equal(t,
`sql_mode = CONCAT(@@session.sql_mode, ',NO_AUTO_VALUE_ON_ZERO')`,
applier.generateSqlModeQuery(),
)
}
{
migrationContext.SkipStrictMode = false
migrationContext.AllowZeroInDate = true
test.S(t).ExpectEquals(
applier.generateSqlModeQuery(),
require.Equal(t,
`sql_mode = REPLACE(REPLACE(CONCAT(@@session.sql_mode, ',NO_AUTO_VALUE_ON_ZERO,STRICT_ALL_TABLES'), 'NO_ZERO_IN_DATE', ''), 'NO_ZERO_DATE', '')`,
applier.generateSqlModeQuery(),
)
}
{
migrationContext.SkipStrictMode = true
migrationContext.AllowZeroInDate = true
test.S(t).ExpectEquals(
applier.generateSqlModeQuery(),
require.Equal(t,
`sql_mode = REPLACE(REPLACE(CONCAT(@@session.sql_mode, ',NO_AUTO_VALUE_ON_ZERO'), 'NO_ZERO_IN_DATE', ''), 'NO_ZERO_DATE', '')`,
applier.generateSqlModeQuery(),
)
}
}
Expand All @@ -72,8 +72,8 @@ func TestApplierUpdateModifiesUniqueKeyColumns(t *testing.T) {
NewColumnValues: columnValues,
WhereColumnValues: columnValues,
})
test.S(t).ExpectEquals(modifiedColumn, "")
test.S(t).ExpectFalse(isModified)
require.Equal(t, "", modifiedColumn)
require.False(t, isModified)
})

t.Run("modified", func(t *testing.T) {
Expand All @@ -83,8 +83,8 @@ func TestApplierUpdateModifiesUniqueKeyColumns(t *testing.T) {
NewColumnValues: sql.ToColumnValues([]interface{}{123456, 24}),
WhereColumnValues: columnValues,
})
test.S(t).ExpectEquals(modifiedColumn, "item_id")
test.S(t).ExpectTrue(isModified)
require.Equal(t, "item_id", modifiedColumn)
require.True(t, isModified)
})
}

Expand Down Expand Up @@ -112,17 +112,17 @@ func TestApplierBuildDMLEventQuery(t *testing.T) {
}

res := applier.buildDMLEventQuery(binlogEvent)
test.S(t).ExpectEquals(len(res), 1)
test.S(t).ExpectNil(res[0].err)
test.S(t).ExpectEquals(strings.TrimSpace(res[0].query), `delete /* gh-ost `+"`test`.`_test_gho`"+` */
require.Len(t, res, 1)
require.NoError(t, res[0].err)
require.Equal(t, `delete /* gh-ost `+"`test`.`_test_gho`"+` */
from
`+"`test`.`_test_gho`"+`
where
((`+"`id`"+` = ?) and (`+"`item_id`"+` = ?))`,
)
test.S(t).ExpectEquals(len(res[0].args), 2)
test.S(t).ExpectEquals(res[0].args[0], 123456)
test.S(t).ExpectEquals(res[0].args[1], 42)
strings.TrimSpace(res[0].query))
require.Len(t, res[0].args, 2)
require.Equal(t, 123456, res[0].args[0])
require.Equal(t, 42, res[0].args[1])
})

t.Run("insert", func(t *testing.T) {
Expand All @@ -132,18 +132,19 @@ func TestApplierBuildDMLEventQuery(t *testing.T) {
NewColumnValues: columnValues,
}
res := applier.buildDMLEventQuery(binlogEvent)
test.S(t).ExpectEquals(len(res), 1)
test.S(t).ExpectNil(res[0].err)
test.S(t).ExpectEquals(strings.TrimSpace(res[0].query),
require.Len(t, res, 1)
require.NoError(t, res[0].err)
require.Equal(t,
`replace /* gh-ost `+"`test`.`_test_gho`"+` */
into
`+"`test`.`_test_gho`"+`
`+"(`id`, `item_id`)"+`
values
(?, ?)`)
test.S(t).ExpectEquals(len(res[0].args), 2)
test.S(t).ExpectEquals(res[0].args[0], 123456)
test.S(t).ExpectEquals(res[0].args[1], 42)
(?, ?)`,
strings.TrimSpace(res[0].query))
require.Len(t, res[0].args, 2)
require.Equal(t, 123456, res[0].args[0])
require.Equal(t, 42, res[0].args[1])
})

t.Run("update", func(t *testing.T) {
Expand All @@ -154,20 +155,21 @@ func TestApplierBuildDMLEventQuery(t *testing.T) {
WhereColumnValues: columnValues,
}
res := applier.buildDMLEventQuery(binlogEvent)
test.S(t).ExpectEquals(len(res), 1)
test.S(t).ExpectNil(res[0].err)
test.S(t).ExpectEquals(strings.TrimSpace(res[0].query),
require.Len(t, res, 1)
require.NoError(t, res[0].err)
require.Equal(t,
`update /* gh-ost `+"`test`.`_test_gho`"+` */
`+"`test`.`_test_gho`"+`
set
`+"`id`"+`=?, `+"`item_id`"+`=?
where
((`+"`id`"+` = ?) and (`+"`item_id`"+` = ?))`)
test.S(t).ExpectEquals(len(res[0].args), 4)
test.S(t).ExpectEquals(res[0].args[0], 123456)
test.S(t).ExpectEquals(res[0].args[1], 42)
test.S(t).ExpectEquals(res[0].args[2], 123456)
test.S(t).ExpectEquals(res[0].args[3], 42)
((`+"`id`"+` = ?) and (`+"`item_id`"+` = ?))`,
strings.TrimSpace(res[0].query))
require.Len(t, res[0].args, 4)
require.Equal(t, 123456, res[0].args[0])
require.Equal(t, 42, res[0].args[1])
require.Equal(t, 123456, res[0].args[2])
require.Equal(t, 42, res[0].args[3])
})
}

Expand All @@ -180,6 +182,6 @@ func TestApplierInstantDDL(t *testing.T) {

t.Run("instantDDLstmt", func(t *testing.T) {
stmt := applier.generateInstantDDLQuery()
test.S(t).ExpectEquals(stmt, "ALTER /* gh-ost */ TABLE `test`.`mytable` ADD INDEX (foo), ALGORITHM=INSTANT")
require.Equal(t, "ALTER /* gh-ost */ TABLE `test`.`mytable` ADD INDEX (foo), ALGORITHM=INSTANT", stmt)
})
}
32 changes: 16 additions & 16 deletions go/logic/hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"testing"
"time"

"github.com/openark/golib/tests"
"github.com/stretchr/testify/require"

"github.com/github/gh-ost/go/base"
)
Expand Down Expand Up @@ -44,7 +44,7 @@ func TestHooksExecutorExecuteHooks(t *testing.T) {

t.Run("does-not-exist", func(t *testing.T) {
migrationContext.HooksPath = "/does/not/exist"
tests.S(t).ExpectNil(hooksExecutor.executeHooks("test-hook"))
require.Nil(t, hooksExecutor.executeHooks("test-hook"))
})

t.Run("failed", func(t *testing.T) {
Expand All @@ -57,7 +57,7 @@ func TestHooksExecutorExecuteHooks(t *testing.T) {
panic(err)
}
defer os.RemoveAll(migrationContext.HooksPath)
tests.S(t).ExpectNotNil(hooksExecutor.executeHooks("failed-hook"))
require.NotNil(t, hooksExecutor.executeHooks("failed-hook"))
})

t.Run("success", func(t *testing.T) {
Expand All @@ -73,40 +73,40 @@ func TestHooksExecutorExecuteHooks(t *testing.T) {

var buf bytes.Buffer
hooksExecutor.writer = &buf
tests.S(t).ExpectNil(hooksExecutor.executeHooks("success-hook", "TEST="+t.Name()))
require.Nil(t, hooksExecutor.executeHooks("success-hook", "TEST="+t.Name()))

scanner := bufio.NewScanner(&buf)
for scanner.Scan() {
split := strings.SplitN(scanner.Text(), "=", 2)
switch split[0] {
case "GH_OST_COPIED_ROWS":
copiedRows, _ := strconv.ParseInt(split[1], 10, 64)
tests.S(t).ExpectEquals(copiedRows, migrationContext.TotalRowsCopied)
require.Equal(t, migrationContext.TotalRowsCopied, copiedRows)
case "GH_OST_DATABASE_NAME":
tests.S(t).ExpectEquals(split[1], migrationContext.DatabaseName)
require.Equal(t, migrationContext.DatabaseName, split[1])
case "GH_OST_DDL":
tests.S(t).ExpectEquals(split[1], migrationContext.AlterStatement)
require.Equal(t, migrationContext.AlterStatement, split[1])
case "GH_OST_DRY_RUN":
tests.S(t).ExpectEquals(split[1], "false")
require.Equal(t, "false", split[1])
case "GH_OST_ESTIMATED_ROWS":
estimatedRows, _ := strconv.ParseInt(split[1], 10, 64)
tests.S(t).ExpectEquals(estimatedRows, int64(123))
require.Equal(t, int64(123), estimatedRows)
case "GH_OST_ETA_SECONDS":
etaSeconds, _ := strconv.ParseInt(split[1], 10, 64)
tests.S(t).ExpectEquals(etaSeconds, int64(60))
require.Equal(t, int64(60), etaSeconds)
case "GH_OST_EXECUTING_HOST":
tests.S(t).ExpectEquals(split[1], migrationContext.Hostname)
require.Equal(t, migrationContext.Hostname, split[1])
case "GH_OST_GHOST_TABLE_NAME":
tests.S(t).ExpectEquals(split[1], fmt.Sprintf("_%s_gho", migrationContext.OriginalTableName))
require.Equal(t, fmt.Sprintf("_%s_gho", migrationContext.OriginalTableName), split[1])
case "GH_OST_OLD_TABLE_NAME":
tests.S(t).ExpectEquals(split[1], fmt.Sprintf("_%s_del", migrationContext.OriginalTableName))
require.Equal(t, fmt.Sprintf("_%s_del", migrationContext.OriginalTableName), split[1])
case "GH_OST_PROGRESS":
progress, _ := strconv.ParseFloat(split[1], 64)
tests.S(t).ExpectEquals(progress, 50.0)
require.Equal(t, 50.0, progress)
case "GH_OST_TABLE_NAME":
tests.S(t).ExpectEquals(split[1], migrationContext.OriginalTableName)
require.Equal(t, migrationContext.OriginalTableName, split[1])
case "TEST":
tests.S(t).ExpectEquals(split[1], t.Name())
require.Equal(t, t.Name(), split[1])
}
}
})
Expand Down
Loading

0 comments on commit 8e38b86

Please sign in to comment.