Destroy/Remove database in Rails

By issuing rake -T you have the following database tasks: rake db:create # Create the database from DATABASE_URL or config/database.yml for the current Rails.env (use db:create:all to create all dbs in the config) rake db:drop # Drops the database using DATABASE_URL or the current Rails.env (use db:drop:all to drop all databases) rake db:fixtures:load # Load … Read more

Rails migration does not change schema.rb

From the documentation: The rake db:reset task will drop the database, recreate it and load the current schema into it. This is not the same as running all the migrations. It will only use the contents of the current schema.rb file. If a migration can’t be rolled back, ‘rake db:reset’ may not help you. To … Read more

Rails rake db:migrate has no effect

There’s a few reasons why your migrations won’t run, but the most common is that the system is already under the impression that all the migrations you’ve defined have already run. Each migration creates an entry in the schema_migrations table with the version column corresponding to the identifier number. If you want to force a … Read more

Rails Migrations: tried to change the type of column from string to integer

I quote the manual about ALTER TABLE: A USING clause must be provided if there is no implicit or assignment cast from old to new type. What you need is: ALTER TABLE listings ALTER longitude TYPE integer USING longitude::int; ALTER TABLE listings ALTER latitude TYPE integer USING latitude::int; Or shorter and faster (for big tables) … Read more

Running migrations with Rails in a Docker container with multiple container instances

Especially with Rails I don’t have any experience, but let’s look from a docker and software engineering point of view. The Docker team advocates, sometimes quite aggressively, that containers are about shipping applications. In this really great statement, Jerome Petazzoni says that it is all about separation of concerns. I feel that this is exactly … Read more

How do I move a column (with contents) to another table in a Rails migration?

I ended up using this migration (tested, it works, and rolls back successfully): class AddPropertyToUser < ActiveRecord::Migration def self.up add_column :users, :someprop, :string execute “UPDATE users u, profiles p SET u.someprop = p.someprop WHERE u.id = p.user_id” remove_column :profiles, :someprop end def self.down add_column :profiles, :someprop, :string execute “UPDATE profiles p, users u SET p.someprop … Read more

How do I add migration with multiple references to the same model in one table? Ruby/Rails

You can do this simply with the add_column method in your migrations and set up the proper associations in your classes: class AddFields < ActiveRecord::Migration def change add_column :tickets, :image_1_id, :integer add_column :tickets, :image_2_id, :integer end end class Ticket < ActiveRecord::Base belongs_to :image_1, :class_name => “Image” belongs_to :image_2, :class_name => “Image” end class Image < … Read more

rails 3.2 migration cannot add index to create_table in change method

You can still add an index as a part of a “change” migration. You just have to do it outside of the call to create_table: class CreateStatistics < ActiveRecord::Migration def change create_table :statistics do |t| t.string :name t.integer :item_id t.integer :value t.text :desc t.timestamps end add_index :statistics, [:name, :item_id] end end This correctly creates the … Read more

tech