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

Partial indexes not recreated correctly after migrations on SQLite #31603

Closed
celsworth opened this issue Dec 31, 2017 · 2 comments
Closed

Partial indexes not recreated correctly after migrations on SQLite #31603

celsworth opened this issue Dec 31, 2017 · 2 comments

Comments

@celsworth
Copy link

Steps to reproduce

I created an executable test case for this bug: https://gist.github.com/celsworth/d3a2fe16d23f57a514655662b1d12cb7

Expected behavior

The test case schema creates a table with a partial unique index, on the test1 column for rows where test1=t. This works as expected. Multiple rows with test1=f can be inserted at this point as the index ignores them.

The migration then performs any operation that requires the table to be dropped and recreated - which is pretty much anything other than adding a column or renaming the table in SQLite. So removing a column is sufficient. When the table is recreated, the partial aspect of the index should be retained as was originally specified.

Actual behavior

When the table is recreated as part of a migration that has to recreate the table, the partial aspect of the index is lost:

D, [2017-12-31T00:25:58.775204 #69138] DEBUG -- :    (0.1ms)  DROP TABLE "tests"
D, [2017-12-31T00:25:58.775675 #69138] DEBUG -- :    (0.1ms)  CREATE TABLE "tests" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "test1" boolean DEFAULT NULL)
...
D, [2017-12-31T00:25:58.776285 #69138] DEBUG -- :    (0.1ms)  CREATE UNIQUE INDEX "index_tests_on_test1" ON "tests" ("test1")

Accordingly, the tests I provided fail because now the unique index covers rows where test1=f as well, so only one can be inserted and the tests fail with a unique index violation.

System configuration

Rails version: 5.1.0

Ruby version: 2.4.2p198

@celsworth
Copy link
Author

Providing test case as an upload as well so it survives longer than the gist will

active_record_migrations_gem.rb.txt

@fatkodima
Copy link
Member

fatkodima commented Dec 31, 2017

I can confirm also on master. I'm working on fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants