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

Index order :desc isn't saved into schema.rb after migration #30902

Closed
krepflap opened this issue Oct 16, 2017 · 3 comments
Closed

Index order :desc isn't saved into schema.rb after migration #30902

krepflap opened this issue Oct 16, 2017 · 3 comments

Comments

@krepflap
Copy link

krepflap commented Oct 16, 2017

Steps to reproduce

I tried to create a bugreport testcase but couldn't figure out all the API calls needed to reproduce entirely, so here are manual steps:

$ rails new example
$ cd example
# I don't use Node.js, so:
$ sed -i '/rubyracer/s/^# //' Gemfile
$ bundle install

$ ./bin/rails g model topic subject:string body:text bumped_at:datetime
$ ./bin/rails db:migrate
$ ./bin/rails g migration AddIndexOnTopics

# Edit the migration file so it becomes:
class AddIndexOnTopics < ActiveRecord::Migration[5.1]
  def change
    add_index :topics, :bumped_at, order: {bumped_at: :desc}
  end
end

$ ./bin/rails db:migrate
$ grep INDEX log/development.log
   (3.1ms)  CREATE  INDEX "index_topics_on_bumped_at" ON "topics" ("bumped_at" DESC)
$ sqlite3 db/development.sqlite3
SQLite version 3.8.7.1 2014-10-29 13:59:56
Enter ".help" for usage hints.
sqlite> .fullschema
CREATE TABLE "schema_migrations" ("version" varchar NOT NULL PRIMARY KEY);
CREATE TABLE "ar_internal_metadata" ("key" varchar NOT NULL PRIMARY KEY, "value" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
CREATE TABLE "topics" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "subject" varchar, "body" text, "bumped_at" datetime, "created_at" datetime NOT NULL, "
updated_at" datetime NOT NULL);
CREATE INDEX "index_topics_on_bumped_at" ON "topics" ("bumped_at" DESC);
/* No STAT tables available */
sqlite>

Ok so far so good. However the schema.rb has no mention of the sort order:

$ grep -A1 -B1 index db/schema.rb
    t.datetime "updated_at", null: false
    t.index ["bumped_at"], name: "index_topics_on_bumped_at"
  end

So when I drop and recreate the database, the index is added without the DESC:

$ ./bin/rails db:drop
$ ./bin/rails db:schema:load
$ grep INDEX log/development.log|tail -1
   (10.5ms)  CREATE  INDEX "index_topics_on_bumped_at" ON "topics" ("bumped_at")
$ sqlite3 db/development.sqlite3
SQLite version 3.8.7.1 2014-10-29 13:59:56
Enter ".help" for usage hints.
sqlite> .fullschema
CREATE TABLE "topics" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "subject" varchar, "body" text, "bumped_at" datetime, "created_at" datetime NOT NULL, "
updated_at" datetime NOT NULL);
CREATE INDEX "index_topics_on_bumped_at" ON "topics" ("bumped_at");
CREATE TABLE "schema_migrations" ("version" varchar NOT NULL PRIMARY KEY);
CREATE TABLE "ar_internal_metadata" ("key" varchar NOT NULL PRIMARY KEY, "value" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL);
/* No STAT tables available */
sqlite>

Expected behavior

The information relating to index sorting order should be persistent after applying a migration.

Actual behavior

The index sorting order is lost when dropping the database and loading via db:schema:load.

System configuration

$ ruby -v
ruby 2.4.2p198 (2017-09-14 revision 59899) [x86_64-linux]
$ rails -v
Rails 5.1.4
@escoberik
Copy link

escoberik commented Oct 16, 2017

I successfully reproduced this bug using Rails 5.0.6. Also, I tried it with pg adapter and the order was saved into the schema successfully. I think this bug only happens with sqlite3 (I tested it with sqlite3 (1.3.13).

I'll try to work in a fix

@krepflap
Copy link
Author

Thanks. I've prepped the start of a test if that would help but I don't know where to go from there, e.g. see/dump the current state of the schema or reload the schema.

@escoberik
Copy link

I know of a library to parse schema.rb but I'm not sure that's the right way. I'll take a look into the current tests.

rohitpaulk added a commit to rohitpaulk/rails that referenced this issue Oct 24, 2017
Although the sqlite adapter supports index sort orders, they
weren't being written to db/schema.rb.
rafaelfranca added a commit that referenced this issue Oct 25, 2017
Save index order :desc to schema.rb (sqlite). Fixes #30902
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