How do you migrate db structure changes to heroku postgres? - ruby-on-rails

For example, I have a migration file for posts:
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.text :text
t.integer :ip
t.timestamps
end
end
end
And want to change it to:
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.text :text
t.integer :ip, :limit => 8
t.timestamps
end
end
end
Would I add a line:
change_column :posts, :ip, :limit => 8
Below so that the file is:
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.text :text
t.integer :ip, :limit => 8
t.timestamps
change_column :posts, :ip, :limit => 8
end
end
end
And then run heroku run rake --trace db:migrate
I'm having trouble understanding how migrations work, and especially to production, so any help would be greatly appreciated.
There is http://guides.rubyonrails.org/active_record_migrations.html#changing-columns a section 3.5 on column modifiers but it doesn't specify how to pass them.
Thanks!

You should create a separate migration for adding the limit on the ip column.
Generate your migration:
rails generate migration ChangeIpLimitOnPosts
Inside the generated migration file, update the contents:
class ChangeIpLimitOnPosts < ActiveRecord::Migration
def up
change_column :posts, :ip, :integer, limit: 8
end
def down
change_column :posts, :ip, :integer
end
end
Take note here: you need to specify the column type when changing the column, even though in your case, you're not changing the type.
Also, in this case, Active Record will not know how to reverse the transaction if you need to rollback, so you need to explicitly tell Active Record how to do that — this can be done using the up and down methods, rather than change.
Run your migration:
rake db:migrate
On Heroku:
heroku run rake db:migrate
Hope that helps.

Related

change column name Rails

I have this table:
class CreateShoes < ActiveRecord::Migration
def change
create_table :shoes do |t|
t.string :name
t.boolean :leather
t.integer :season
t.timestamps null: false
end
end
end
the 'season' column should be called 'season_id'. I know that I have to write 't.rename :season, :season_id' as explained in http://edgeguides.rubyonrails.org/active_record_migrations.html#column-modifiers but I don't manage to find the right syntax. Should it be?
class CreateShoes < ActiveRecord::Migration
def change
create_table :shoes do |t|
t.string :name
t.boolean :leather
t.integer :season
t.timestamps null: false
end
change_table :products do |t|
t.rename :season, :season_id
end
end
end
Doesn't work. Anything I have to do in the Mac console? Thanks!
Run in your console:
$ rails g migration rename_season_to_season_id
Now file db/migrate/TIMESTAMP_rename_season_to_season_id.rb contains following:
class RenameSeasonToSeasonId < ActiveRecord::Migration
def change
end
end
Modify it as follows:
class RenameSeasonToSeasonId < ActiveRecord::Migration
def change
rename_column :shoes, :season, :season_id
end
end
Then run $ rake db:migrate in console.
Either fix your migration and do
rake db:rollback db:migrate
or make another migration like so:
rename_column :shoes, :season, :season_id if column_exists?(:shoes, :season) && !column_exists?(:shoes, :season_id)
and then do
rake db:migrate
If your intention is to rename column in table than you example migration is not making sense :)... Instead of this
class CreateShoes < ActiveRecord::Migration
def change
create_table :shoes do |t|
t.string :name
t.boolean :leather
t.integer :season
t.timestamps null: false
end
change_table :products do |t|
t.rename :season, :season_id
end
end
end
You just need table change migration, like this(Rails will take care rollback for your):
class RenameSessionColumnInsideShoes < ActiveRecord::Migration
def change
change_table :products do |t|
t.rename :season, :season_id
end
end
end
rename method on table object in rails is valid method, as you can see in Rails source code
https://github.com/rails/rails/blob/master/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb#L582
If your branch has been pushed to production then one probably needs to add the new migration by running the following command:
rails g migration RenameSeasonColumnNameToShoes
and if it hasn't been pushed to production or you have to currently make changes to your branch only, then do:
bundle exec rake db:rollback
Then make changes to your migration file inside /db/migrate/<your_migration_file_name>
Then in your migration file, using rename_column do as follows:
class RenameSeasonColumnNameToShoes < ActiveRecord::Migration
def change
rename_column :shoes, :season, :season_id
end
end
and then do
bundle exec rake db:migrate db:test:prepare

How can I add new attributes into my migrate?

I currently have the migrate thing like:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.string :email
t.string :encrypted_password
t.string :salt
t.timestamps
end
end
end
now, if I wanna add two new attributes into this file, one is: t.string :type , and the other one is: t.string :memory_token , how can I do this please?
If you have already run the migration you will have to create a new one.
rails g migration AddTypeToUsers
And then in the migration file you can edit in
change_table :users do |t|
t.string :type
t.string :memory_token
end
Then run a migration rake db:migrate to make the changes
If you haven't run the migration then you can simply add
t.string :type
t.string :memory_token
To that file you have showed us and then run your migration
+1 to #JTG
You can also make this just with one line:
rails g migration AddTypeAndMemoryTokenToUsers type:string memory_token:string
and you will get a following file:
class AddTypeAndMemoryTokenToUsers < ActiveRecord::Migration
def change
add_column :users, :type, :string
add_column :users, :memory_token, :string
end
end
which will make the changes after running rake db:migrate

No output after running rake db:migrate

I'm struggling here with the db migration for the acts_as_commentable_with_threading.
After generating the migration rails generate acts_as_commentable_with_threading_migration I proceeded to add the comment table rake db:migrate. Nothing happened, no error message, just returned to the regular prompt.
Looking at other response to this problem I tried rake db:migrate VERSION= # version number.
Yet here I get an Error response ActiveRecord::UnknownMigrationVersionError:
I must be doing something extremely wrong here since the computer doesn't validate the existence of my comment migration...
UPDATE from #Tiago answer
Ran rails generate migration acts_as_commentable_with_threading_migration
I had to manually create the migration by adding this code to the migration file. Then, thedb:migration worked perfectly.
Why wasn't it working in the first place? The Documentation clearly indicate to run rails generate acts_as_commentable_with_threading_migration.
Just create a migration for yourself as indicated in the gem's page:
rails g migration acts_as_commentable_with_threading_migration
And paste that to the file:
class ActsAsCommentableWithThreadingMigration < ActiveRecord::Migration
def self.up
create_table :comments, :force => true do |t|
t.integer :commentable_id, :default => 0
t.string :commentable_type
t.string :title
t.text :body
t.string :subject
t.integer :user_id, :default => 0, :null => false
t.integer :parent_id, :lft, :rgt
t.timestamps
end
add_index :comments, :user_id
add_index :comments, [:commentable_id, :commentable_type]
end
def self.down
drop_table :comments
end
end

Why directly changing a migrate file does not change the schema file?

My current migrate file is
class CreateMovies < ActiveRecord::Migration
def up
create_table :movies, :force => true do |t|
t.string :title
t.string :rating
t.text :description
t.datetime :release_date
# Add fields that let Rails automatically keep track
# of when movies are added or modified:
t.timestamps
end
end
def down
drop_table :movies
end
end
I try to change release_date type to integer. So I directly change the file to
class CreateMovies < ActiveRecord::Migration
def up
create_table :movies, :force => true do |t|
t.string :title
t.string :rating
t.text :description
t.integer :release_date
# Add fields that let Rails automatically keep track
# of when movies are added or modified:
t.timestamps
end
end
def down
drop_table :movies
end
end
Please pay attention, the release_date type has been changed. But after I run
bundle exec rake db:migrate
It still produce the same schema file as before. I am so confused.
It's probably because you've already run your migration. So before you want to change it, you should rollback it first:
bundle exec rake db:rollback
then you should modify it and run again:
bundle exec rake db:migrate
As an alternative to dropping and upping the migration, you could make a new migration to change the column type.
class ChangeMoviesReleaseTypeToInteger < ActiveRecord::Migration
def up
change_column :movies, :release_date, :integer
end
def down
change_column :movies, :release_date, :datetime
end
end
Just as a side note, release_date is a confusing name for an integer field - most people would expect it to be a datetime as you had originally.
down will remove the table
rake db:migrate:down VERSION=file_name(exclude extension)
up will create with new changes
rake db:migrate:up VERSION=file_name(exclude extension)

When I run the rake:db migrate command I get an error "Uninitialized constant CreateArticles"

I created a model ruby script/generate model Article (simple enuff)
Here is the migration file create_articles.rb:
def self.up
create_table :articles do |t|
t.column :user_id, :integer
t.column :title, :string
t.column :synopsis, :text, :limit => 1000
t.column :body, :text, :limit => 20000
t.column :published, :boolean, :default => false
t.column :created_at, :datetime
t.column :updated_at, :datetime
t.column :published_at, :datetime
t.column :category_id, :integer
end
def self.down
drop_table :articles
end
end
When I run the rake:db migrate command I receive an error rake aborted! "Uninitialized constant CreateArticles."
Does anyone know why this error keeps happening?
Be sure that your file name and class name say the same thing(except the class name is camel cased).The contents of your migration file should look something like this, simplified them a bit too:
#20090106022023_create_articles.rb
class CreateArticles < ActiveRecord::Migration
def self.up
create_table :articles do |t|
t.belongs_to :user, :category
t.string :title
t.text :synopsis, :limit => 1000
t.text :body, :limit => 20000
t.boolean :published, :default => false
t.datetime :published_at
t.timestamps
end
end
def self.down
drop_table :articles
end
end
It's possible to get the given error if your class names don't match inflections (like acronyms) from config/initializers/inflections.rb.
For example, if your inflections include:
ActiveSupport::Inflector.inflections(:en) do |inflect|
inflect.acronym 'DOG'
end
then you might need to make sure the class in your migration is:
class CreateDOGHouses < ActiveRecord::Migration[5.0]
rather than:
class CreateDogHouses < ActiveRecord::Migration[5.0]
Not super common, but if you generate a migration or a model or something, and then add part of it to inflections afterwards it may happen. (The example here will cause NameError: uninitialized constant CreateDOGHouses if your class name is CreateDogHouses, at least with Rails 5.)
If you're getting this error and it's NOT because of the migration file name, there is another possible solution. Open the class directly in the migration like this:
class SomeClass < ActiveRecord::Base; end
It should now be possible to use SomeClass within the migration.
The top answer solved for me. Just leaving this here in case it helps.
Example
If your migration file is called
20210213040840_add_first_initial_only_to_users.rb
then the class name in your migration file should be
AddFirstInitialOnlyToUsers
Note: if the class name doesn't match, it will error even if the difference is just a lower case t instead of an upper case 'T' in 'To' - so be careful of that!

Resources