Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renaming a table inside a transaction and using it doesn't work #39664

Closed
dveeden opened this issue Dec 6, 2022 · 5 comments · Fixed by #39945
Closed

Renaming a table inside a transaction and using it doesn't work #39664

dveeden opened this issue Dec 6, 2022 · 5 comments · Fixed by #39945
Labels
affects-4.0 This bug affects 4.0.x versions. affects-5.0 This bug affects 5.0.x versions. affects-5.1 This bug affects 5.1.x versions. affects-5.2 This bug affects 5.2.x versions. affects-5.3 This bug affects 5.3.x versions. affects-5.4 This bug affects 5.4.x versions. affects-6.0 affects-6.1 affects-6.2 affects-6.3 affects-6.4 affects-6.5 report/community The community has encountered this bug. severity/moderate sig/sql-infra SIG: SQL Infra type/bug The issue is confirmed as a bug. type/compatibility

Comments

@dveeden
Copy link
Contributor

dveeden commented Dec 6, 2022

This happens only if that multi-statement query is inside a transaction (which is how diesel runs migrations).

Here is the minimal reproduction:

mariadb --host=127.0.0.1 --port=4000 --user=root --database test --delimiter ';;;' --execute "$(cat << EOF
-- First migration creates the table foo
BEGIN;
;;;
CREATE TABLE foo(pk CHAR(1) PRIMARY KEY);
;;;
COMMIT;
-- Second migration renames foo, creates a new foo, 
-- and selects into the new foo from the old foo
;;;
BEGIN;
;;;
ALTER TABLE foo RENAME TO bar; 
CREATE TABLE foo(pk CHAR(1) PRIMARY KEY);
INSERT INTO foo (pk) SELECT pk FROM bar;
;;;
COMMIT;
;;;
EOF
)"

Tested against a new instance run by tiup:

*************************** 1. row ***************************
tidb_version(): Release Version: v6.4.0
Edition: Community
Git Commit Hash: cf36a9ce2fe1039db3cf3444d51930b887df18a1
Git Branch: heads/refs/tags/v6.4.0
UTC Build Time: 2022-11-13 05:25:30
GoVersion: go1.19.2
Race Enabled: false
TiKV Min Version: 6.2.0-alpha
Check Table Before Drop: false
Store: tikv

Originally posted by @Pythoner6 in #24194 (comment)

@dveeden
Copy link
Contributor Author

dveeden commented Dec 6, 2022

$ mysql --host=127.0.0.1 --port=4000 --user=root --database test --delimiter ';;;' --execute "$(cat << EOF
> -- First migration creates the table foo
BEGIN;
;;;
CREATE TABLE foo(pk CHAR(1) PRIMARY KEY);
;;;
COMMIT;
-- Second migration renames foo, creates a new foo, 
-- and selects into the new foo from the old foo
;;;
BEGIN;
;;;
ALTER TABLE foo RENAME TO bar; 
CREATE TABLE foo(pk CHAR(1) PRIMARY KEY);
INSERT INTO foo (pk) SELECT pk FROM bar;
;;;
COMMIT;
;;;
EOF
)"
ERROR 1146 (42S02) at line 12: Table 'test.bar' doesn't exist

And from the logs:

[2022/12/06 10:35:18.992 +02:00] [INFO] [conn.go:1152] ["command dispatched failed"] [conn=9120160880355443095] [connInfo="id:9120160880355443095, addr:127.0.0.1:36118 status:11, collation:utf8mb4_0900_ai_ci, user:root"] [command=Query] [status="inTxn:1, autocommit:1"] [sql="ALTER TABLE foo RENAME TO bar; \nCREATE TABLE foo(pk CHAR(1) PRIMARY KEY);\nINSERT INTO foo (pk) SELECT pk FROM bar;"] [txn_mode=PESSIMISTIC] [timestamp=437863243827904521] [err="[schema:1146]Table 'test.bar' doesn't exist"]

@tiancaiamao
Copy link
Contributor

Between "begin ... commit", any ddl statement would commit the current transaction automatically.
In TiDB we're following MySQL's behaviour here https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html

@dveeden
Copy link
Contributor Author

dveeden commented Dec 9, 2022

Between "begin ... commit", any ddl statement would commit the current transaction automatically. In TiDB we're following MySQL's behaviour here https://dev.mysql.com/doc/refman/8.0/en/implicit-commit.html

Yes I'm fully aware of that. But with that I still don't expect the "Table 'test.bar' doesn't exist" message.

The way this ORM does this isn't great for MySQL (compatible) databases, but it should still work.

@tiancaiamao
Copy link
Contributor

Oh, I see.
This should be a multiple-statement related bug.
multiple-statement is not battle-tested in tidb, I'll try to reproduce and fix it. @dveeden

@seiya-annie
Copy link

/found community

@ti-chi-bot ti-chi-bot bot added the report/community The community has encountered this bug. label Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
affects-4.0 This bug affects 4.0.x versions. affects-5.0 This bug affects 5.0.x versions. affects-5.1 This bug affects 5.1.x versions. affects-5.2 This bug affects 5.2.x versions. affects-5.3 This bug affects 5.3.x versions. affects-5.4 This bug affects 5.4.x versions. affects-6.0 affects-6.1 affects-6.2 affects-6.3 affects-6.4 affects-6.5 report/community The community has encountered this bug. severity/moderate sig/sql-infra SIG: SQL Infra type/bug The issue is confirmed as a bug. type/compatibility
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants