Skip to content

The AUTO_INCREMENT has been changed #381

@ghost

Description

This is the place to report a bug, ask a question, or suggest an enhancement.

When we delete some data, and the max value of AUTO_INCREMENT columns is not match the AUTO_INCREMENT of the table. Then we use ghost to do some alter .The AUTO_INCREMENT of the table have be changed.

This is also the place to make a discussion before creating a PR.

If this is a bug report, please provide a test case (e.g., your table definition and gh-ost command) and the error output.
Please use markdown to format code or SQL: https://guides.github.com/features/mastering-markdown/

CREATE TABLE ghost_test(
    -> `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
    -> val int(11) NOT NULL DEFAULT '0',
    -> PRIMARY KEY (`id`)
    -> )ENGINE=InnoDB;

we insert 3 rows:

>select * from ghost_test;
+----+-----+
| id | val |
+----+-----+
|  1 |   1 |
|  2 |   2 |
|  3 |   3 |
+----+-----+
3 rows in set (0.00 sec)

delete 1 row

>delete from ghost_test where id =3;
Query OK, 1 row affected (0.00 sec)

>select * from ghost_test;
+----+-----+
| id | val |
+----+-----+
|  1 |   1 |
|  2 |   2 |
+----+-----+
>show create table ghost_test\G
*************************** 1. row ***************************
       Table: ghost_test
Create Table: CREATE TABLE `ghost_test` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `val` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
1 row in set (0.00 sec)

Then we used gh-ost to do "Alter table ghost_test engine=innodb"

show create table ghost_test\G
*************************** 1. row ***************************
       Table: ghost_test
Create Table: CREATE TABLE `ghost_test` (
  `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
  `val` int(11) NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
1 row in set (0.00 sec)

Please label the issue on the right (bug, enhancement, question, etc.).
My business friend try to fix the bug.
He add the AUTO_INCREMENT to the SQL which create the new table .

There is his code

func (this *Applier) ModifyGhostTable() (string, error) {
	SourceStr := fmt.Sprintf(`show create table %s.%s`,
		sql.EscapeName(this.migrationContext.DatabaseName),
		sql.EscapeName(this.migrationContext.OriginalTableName),
	)
	ResponseSource, err := sqlutils.QueryResultData(this.db, SourceStr)
	if err != nil {
		return "", err
	}
	SourceStrip := strings.Replace(ResponseSource[0][1].String, "`", "", -1)
	NewGhostTableName := this.migrationContext.DatabaseName + "." + this.migrationContext.GetGhostTableName()
	NewGhostTableSQL := strings.Replace(SourceStrip, this.migrationContext.OriginalTableName, NewGhostTableName, -1)
	return NewGhostTableSQL, nil
}

// CreateGhostTable creates the ghost table on the applier host
func (this *Applier) CreateGhostTable() error {
	NewGhostTableSQL, err := this.ModifyGhostTable()
	if err != nil {
		return err
	}
	log.Infof(NewGhostTableSQL)
	if _, err := sqlutils.ExecNoPrepare(this.db, NewGhostTableSQL); err != nil {
		return err
	}
	log.Infof("Ghost table created")
	return nil
}

And please understand if this issue is not addressed immediately or in a timeframe you were expecting.

Thank you!

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions