I need to perform some database manipulations, such as creating tables, adding indexes etc. I would like to use the same methods used in migrations, such as create_table
, add_index
etc. However, when I try this I get
NoMethodError: undefined method `add_index' for main:Object
I added this at the beginning of my file:
include ActiveRecord::ConnectionAdapters::SchemaStatements
However, now I get the following error:
NameError: undefined local variable or method `allowed_index_name_length' for main:Object
This is defined in ActiveRecord::ConnectionAdapters::DatabaseLimits
. I tried to include ActiveRecord::ConnectionAdapters
, but it didn't include all subclasses/modules, as I expected.
So the question is - what should I do in order to be able to write the same code I am normally able to write within a migration?
The methods in a migration are all class methods of the ActiveRecord::Migration
class. So you can call them like
ActiveRecord::Migration.add_index :foo, :bar