forked from JamesStewy/go-mysqldump
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from BrandonRoehl/lock-tables-first
Add the ability to lock all tables
- Loading branch information
Showing
4 changed files
with
90 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -120,8 +120,7 @@ func TestCreateTableSQLOk(t *testing.T) { | |
Connection: db, | ||
} | ||
|
||
table, err := data.createTable("Test_Table") | ||
assert.NoError(t, err) | ||
table := data.createTable("Test_Table") | ||
|
||
result, err := table.CreateSQL() | ||
assert.NoError(t, err) | ||
|
@@ -151,8 +150,7 @@ func TestCreateTableRowValues(t *testing.T) { | |
Connection: db, | ||
} | ||
|
||
table, err := data.createTable("test") | ||
assert.NoError(t, err) | ||
table := data.createTable("test") | ||
|
||
assert.True(t, table.Next()) | ||
|
||
|
@@ -181,8 +179,7 @@ func TestCreateTableValuesSteam(t *testing.T) { | |
MaxAllowedPacket: 4096, | ||
} | ||
|
||
table, err := data.createTable("test") | ||
assert.NoError(t, err) | ||
table := data.createTable("test") | ||
|
||
s := table.Stream() | ||
assert.EqualValues(t, "INSERT INTO `test` VALUES ('1','[email protected]','Test Name 1'),('2','[email protected]','Test Name 2');", <-s) | ||
|
@@ -207,8 +204,7 @@ func TestCreateTableValuesSteamSmallPackets(t *testing.T) { | |
MaxAllowedPacket: 64, | ||
} | ||
|
||
table, err := data.createTable("test") | ||
assert.NoError(t, err) | ||
table := data.createTable("test") | ||
|
||
s := table.Stream() | ||
assert.EqualValues(t, "INSERT INTO `test` VALUES ('1','[email protected]','Test Name 1');", <-s) | ||
|
@@ -234,8 +230,7 @@ func TestCreateTableAllValuesWithNil(t *testing.T) { | |
Connection: db, | ||
} | ||
|
||
table, err := data.createTable("test") | ||
assert.NoError(t, err) | ||
table := data.createTable("test") | ||
|
||
results := make([]string, 0) | ||
for table.Next() { | ||
|
@@ -277,8 +272,7 @@ func TestCreateTableOk(t *testing.T) { | |
|
||
assert.NoError(t, data.getTemplates()) | ||
|
||
table, err := data.createTable("Test_Table") | ||
assert.NoError(t, err) | ||
table := data.createTable("Test_Table") | ||
|
||
data.writeTable(table) | ||
|
||
|
@@ -335,8 +329,7 @@ func TestCreateTableOkSmallPackets(t *testing.T) { | |
|
||
assert.NoError(t, data.getTemplates()) | ||
|
||
table, err := data.createTable("Test_Table") | ||
assert.NoError(t, err) | ||
table := data.createTable("Test_Table") | ||
|
||
data.writeTable(table) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ package mysqldump | |
|
||
import ( | ||
"bytes" | ||
"io/ioutil" | ||
"strings" | ||
"testing" | ||
|
||
|
@@ -56,11 +57,12 @@ UNLOCK TABLES; | |
` | ||
|
||
func RunDump(t testing.TB) string { | ||
func RunDump(t testing.TB, data *Data) { | ||
db, mock, err := sqlmock.New() | ||
assert.NoError(t, err, "an error was not expected when opening a stub database connection") | ||
defer db.Close() | ||
|
||
data.Connection = db | ||
showTablesRows := sqlmock.NewRows([]string{"Tables_in_Testdb"}). | ||
AddRow("Test_Table") | ||
|
||
|
@@ -74,27 +76,72 @@ func RunDump(t testing.TB) string { | |
AddRow(1, nil, "Test Name 1"). | ||
AddRow(2, "[email protected]", "Test Name 2") | ||
|
||
mock.ExpectQuery("^SELECT version()").WillReturnRows(serverVersionRows) | ||
mock.ExpectQuery(`^SELECT version\(\)$`).WillReturnRows(serverVersionRows) | ||
mock.ExpectQuery("^SHOW TABLES$").WillReturnRows(showTablesRows) | ||
mock.ExpectExec("^LOCK TABLES `Test_Table` READ /\\*!32311 LOCAL \\*/$").WillReturnResult(sqlmock.NewResult(1, 1)) | ||
mock.ExpectQuery("^SHOW CREATE TABLE `Test_Table`$").WillReturnRows(createTableRows) | ||
mock.ExpectQuery("^SELECT (.+) FROM `Test_Table`$").WillReturnRows(createTableValueRows) | ||
|
||
assert.NoError(t, data.Dump(), "an error was not expected when dumping a stub database connection") | ||
} | ||
|
||
func TestDumpOk(t *testing.T) { | ||
var buf bytes.Buffer | ||
assert.NoError(t, Dump(db, &buf), "an error was not expected when dumping a stub database connection") | ||
|
||
return buf.String() | ||
RunDump(t, &Data{ | ||
Out: &buf, | ||
LockTables: true, | ||
}) | ||
|
||
result := strings.Replace(strings.Split(buf.String(), "-- Dump completed")[0], "`", "~", -1) | ||
|
||
assert.Equal(t, expected, result) | ||
} | ||
|
||
func TestDumpOk(t *testing.T) { | ||
out := RunDump(t) | ||
func TestNoLockOk(t *testing.T) { | ||
var buf bytes.Buffer | ||
|
||
result := strings.Replace(strings.Split(out, "-- Dump completed")[0], "`", "~", -1) | ||
data := &Data{ | ||
Out: &buf, | ||
LockTables: false, | ||
} | ||
|
||
db, mock, err := sqlmock.New() | ||
assert.NoError(t, err, "an error was not expected when opening a stub database connection") | ||
defer db.Close() | ||
|
||
data.Connection = db | ||
showTablesRows := sqlmock.NewRows([]string{"Tables_in_Testdb"}). | ||
AddRow("Test_Table") | ||
|
||
serverVersionRows := sqlmock.NewRows([]string{"Version()"}). | ||
AddRow("test_version") | ||
|
||
createTableRows := sqlmock.NewRows([]string{"Table", "Create Table"}). | ||
AddRow("Test_Table", "CREATE TABLE 'Test_Table' (`id` int(11) NOT NULL AUTO_INCREMENT,`email` char(60) DEFAULT NULL, `name` char(60), PRIMARY KEY (`id`))ENGINE=InnoDB DEFAULT CHARSET=latin1") | ||
|
||
createTableValueRows := sqlmock.NewRows([]string{"id", "email", "name"}). | ||
AddRow(1, nil, "Test Name 1"). | ||
AddRow(2, "[email protected]", "Test Name 2") | ||
|
||
mock.ExpectQuery(`^SELECT version\(\)$`).WillReturnRows(serverVersionRows) | ||
mock.ExpectQuery("^SHOW TABLES$").WillReturnRows(showTablesRows) | ||
mock.ExpectQuery("^SHOW CREATE TABLE `Test_Table`$").WillReturnRows(createTableRows) | ||
mock.ExpectQuery("^SELECT (.+) FROM `Test_Table`$").WillReturnRows(createTableValueRows) | ||
|
||
assert.NoError(t, data.Dump(), "an error was not expected when dumping a stub database connection") | ||
|
||
result := strings.Replace(strings.Split(buf.String(), "-- Dump completed")[0], "`", "~", -1) | ||
|
||
assert.Equal(t, expected, result) | ||
} | ||
|
||
func BenchmarkDump(b *testing.B) { | ||
data := &Data{ | ||
Out: ioutil.Discard, | ||
LockTables: true, | ||
} | ||
for i := 0; i < b.N; i++ { | ||
_ = RunDump(b) | ||
RunDump(b, data) | ||
} | ||
} |