dimanche 26 juin 2016

Rails edit migration file to include index?

I've just been trying to figure out some ways to optimize my MySQL database, but I'm having a bit of an issue with indexing here.

As opposed to stacking up a bunch of migration files (since I'm not really managing much data yet), I'm simply "going back" and editing migration files periodically to include columns, etc. Not sure if this is best practice, but I figured it'd be easier to avoid ending up with 100 migration files adding small things.

Anyways, I'm trying to add an index to one of the columns in a migration file, but I can't seem to get it to work (it's not showing up in the schema file after rolling back and migrating again).

class CreateVuln < ActiveRecord::Migration
  def change
    create_table :vuln do |t|
      t.integer :node_id
      t.string :node_identifier
      t.string :vuln_finding_identifier

      t.timestamps null: false
    end

    add_index :vuln, :node_identifier
  end
end

But when I go to schema.rb, here's what it looks like:

  create_table "vuln", force: :cascade do |t|
    t.integer  "node_id",                 limit: 4
    t.string   "node_identifier",         limit: 255
    t.string   "vuln_finding_identifier", limit: 255
    t.datetime "created_at",                               null: false
    t.datetime "updated_at",                               null: false
  end

Can someone tell me what I'm missing here? If I add a new migration by running rails g migration AddIndexToVuln node_identifier:string:index then I can see that the schema.rb file gets updated properly. schema.rb then adds this line: add_index "vuln", ["node_identifier"], name: "index_vuln_on_node_identifier", using: :btree

So as opposed to creating a new migration file, I'm just wondering if I can just roll back my database and make the changes in the existing migration file for that table.

Aucun commentaire:

Enregistrer un commentaire