CountTableRows counts exact number of rows on the original table
(ctx context.Context)
| 659 | |
| 660 | // CountTableRows counts exact number of rows on the original table |
| 661 | func (isp *Inspector) CountTableRows(ctx context.Context) error { |
| 662 | atomic.StoreInt64(&isp.migrationContext.CountingRowsFlag, 1) |
| 663 | defer atomic.StoreInt64(&isp.migrationContext.CountingRowsFlag, 0) |
| 664 | |
| 665 | isp.migrationContext.Log.Infof("As instructed, I'm issuing a SELECT COUNT(*) on the table. This may take a while") |
| 666 | |
| 667 | conn, err := isp.db.Conn(ctx) |
| 668 | if err != nil { |
| 669 | return err |
| 670 | } |
| 671 | defer conn.Close() |
| 672 | |
| 673 | var connectionID string |
| 674 | if err := conn.QueryRowContext(ctx, `SELECT /* gh-ost */ CONNECTION_ID()`).Scan(&connectionID); err != nil { |
| 675 | return err |
| 676 | } |
| 677 | |
| 678 | query := fmt.Sprintf(`select /* gh-ost */ count(*) as count_rows from %s.%s`, sql.EscapeName(isp.migrationContext.DatabaseName), sql.EscapeName(isp.migrationContext.OriginalTableName)) |
| 679 | var rowsEstimate int64 |
| 680 | if err := conn.QueryRowContext(ctx, query).Scan(&rowsEstimate); err != nil { |
| 681 | if errors.Is(err, context.Canceled) || errors.Is(err, context.DeadlineExceeded) { |
| 682 | isp.migrationContext.Log.Infof("exact row count cancelled (%s), likely because I'm about to cut over. I'm going to kill that query.", ctx.Err()) |
| 683 | return mysql.Kill(isp.db, connectionID) |
| 684 | } |
| 685 | return err |
| 686 | } |
| 687 | |
| 688 | // row count query finished. nil out the cancel func, so the main migration thread |
| 689 | // doesn't bother calling it after row copy is done. |
| 690 | isp.migrationContext.SetCountTableRowsCancelFunc(nil) |
| 691 | |
| 692 | atomic.StoreInt64(&isp.migrationContext.RowsEstimate, rowsEstimate) |
| 693 | isp.migrationContext.UsedRowsEstimateMethod = base.CountRowsEstimate |
| 694 | |
| 695 | isp.migrationContext.Log.Infof("Exact number of rows via COUNT: %d", rowsEstimate) |
| 696 | |
| 697 | return nil |
| 698 | } |
| 699 | |
| 700 | // applyColumnTypes |
| 701 | func (isp *Inspector) applyColumnTypes(databaseName, tableName string, columnsLists ...*sql.ColumnList) error { |
no test coverage detected