@@ -1303,51 +1303,48 @@ func (e *Extractor) mysqlDump() (retErr error) {
13031303
13041304 e .gotCoordinateCh <- struct {}{}
13051305
1306- // Transform the current schema so that it reflects the *current* state of the MySQL server's contents.
1307- // First, get the DROP TABLE and CREATE TABLE statement (with keys and constraint definitions) for our tables ...
1308- if ! e .mysqlContext .SkipCreateDbTable {
1309- e .logger .Info ("generating DROP and CREATE statements to reflect current database schemas" ,
1310- "replicateDoDb" , e .replicateDoDb )
1311-
1312- for _ , db := range e .replicateDoDb {
1313- var dbSQL string
1314- if strings .ToLower (db .TableSchema ) != "mysql" {
1315- if db .TableSchemaRename != "" {
1316- dbSQL , err = base .RenameCreateSchemaAddINE (db .CreateSchemaString , db .TableSchemaRename )
1317- if err != nil {
1318- return errors .Wrap (err , "RenameCreateSchemaAddINE" )
1319- }
1320- } else {
1321- dbSQL = db .CreateSchemaString
1306+ // Go through all tables to get DDL and row numbers.
1307+ for _ , db := range e .replicateDoDb {
1308+ if strings .ToLower (db .TableSchema ) == "mysql" {
1309+ continue
1310+ }
1311+
1312+ // Create the schema.
1313+ entry := & common.DumpEntry {}
1314+ if ! e .mysqlContext .SkipCreateDbTable {
1315+ if db .TableSchemaRename != "" {
1316+ entry .DbSQL , err = base .RenameCreateSchemaAddINE (db .CreateSchemaString , db .TableSchemaRename )
1317+ if err != nil {
1318+ return errors .Wrap (err , "RenameCreateSchemaAddINE" )
13221319 }
1320+ } else {
1321+ entry .DbSQL = db .CreateSchemaString
1322+ }
1323+ }
1324+ if err := e .encodeAndSendDumpEntry (entry ); err != nil {
1325+ return errors .Wrap (err , "encodeAndSendDumpEntry. create schema entry" )
1326+ }
13231327
1328+ // Create the tables.
1329+ for _ , tbCtx := range db .TableMap {
1330+ tb := tbCtx .Table
1331+ tb .Counter , err = e .CountTableRows (tb )
1332+ if err != nil {
1333+ return errors .Wrapf (err , "CountTableRows %v.%v" , tb .TableSchema , tb .TableName )
13241334 }
1335+ e .logger .Info ("count table" , "schema" , db .TableSchema , "table" , tb .TableName , "rows" , tb .Counter )
1336+
13251337 entry := & common.DumpEntry {
1326- DbSQL : dbSQL ,
1327- }
1328- atomic .AddInt64 (& e .mysqlContext .RowsEstimate , 1 )
1329- atomic .AddInt64 (& e .TotalRowsCopied , 1 )
1330- if err := e .encodeAndSendDumpEntry (entry ); err != nil {
1331- return errors .Wrap (err , "encodeAndSendDumpEntry. create schema entry" )
1338+ TbSQL : []string {},
1339+ TotalCount : tb .Counter ,
13321340 }
1333-
1334- for _ , tbCtx := range db .TableMap {
1335- tb := tbCtx .Table
1336- if tb .TableSchema != db .TableSchema {
1337- continue
1338- }
1339- total , err := e .CountTableRows (tb )
1340- if err != nil {
1341- return errors .Wrapf (err , "CountTableRows %v.%v" , tb .TableSchema , tb .TableName )
1342- }
1343- tb .Counter = total
1344- var tbSQL []string
1341+ if ! e .mysqlContext .SkipCreateDbTable {
13451342 if strings .ToLower (tb .TableType ) == "view" {
13461343 /*tbSQL, err = base.ShowCreateView(e.singletonDB, tb.TableSchema, tb.TableName, e.mysqlContext.DropTableIfExists)
13471344 if err != nil {
13481345 return err
13491346 }*/
1350- } else if strings . ToLower ( tb . TableSchema ) != "mysql" {
1347+ } else {
13511348 ctStmt , err := base .ShowCreateTable (e .singletonDB , tb .TableSchema , tb .TableName )
13521349 if err != nil {
13531350 return err
@@ -1362,23 +1359,17 @@ func (e *Extractor) mysqlDump() (retErr error) {
13621359 }
13631360
13641361 if e .mysqlContext .DropTableIfExists {
1365- tbSQL = append (tbSQL , fmt .Sprintf ("DROP TABLE IF EXISTS %s.%s" ,
1362+ entry . TbSQL = append (entry . TbSQL , fmt .Sprintf ("DROP TABLE IF EXISTS %s.%s" ,
13661363 mysqlconfig .EscapeName (targetSchema ), mysqlconfig .EscapeName (targetTable )))
13671364 }
1368- tbSQL = append (tbSQL , ctStmt )
1369- }
1370- entry := & common.DumpEntry {
1371- TbSQL : tbSQL ,
1372- TotalCount : tb .Counter ,
1373- }
1374- atomic .AddInt64 (& e .mysqlContext .RowsEstimate , 1 )
1375- atomic .AddInt64 (& e .TotalRowsCopied , 1 )
1376- if err := e .encodeAndSendDumpEntry (entry ); err != nil {
1377- return errors .Wrap (err , "encodeAndSendDumpEntry. create table" )
1365+ entry .TbSQL = append (entry .TbSQL , ctStmt )
13781366 }
13791367 }
1380- e .tableCount += len (db .TableMap )
1368+ if err := e .encodeAndSendDumpEntry (entry ); err != nil {
1369+ return errors .Wrap (err , "encodeAndSendDumpEntry. create table" )
1370+ }
13811371 }
1372+ e .tableCount += len (db .TableMap )
13821373 }
13831374 step ++
13841375
0 commit comments