Why is schema.rb not generated (properly) when running rake db:migrate? - ruby-on-rails

I've just started working through the Rails 3 tutorials so that I can develop a bit of familiarity with the framework, but I'm running into issues with the generation of schema.rb. My operating system is Windows 7 x64, Ruby 1.9.2, MySQL2 gem 0.2.6, Rails 3.0.3.
I have two migrations created, one for my lists:
class CreateLists < ActiveRecord::Migration
def self.up
create_table :lists do |t|
t.string :name
t.text :description
t.timestamps
end
end
def self.down
drop_table :lists
end
end
and one for my List items:
class CreateItems < ActiveRecord::Migration
def self.up
create_table :items do |t|
t.string :name
t.string :type
t.boolean :completed
t.references :list
t.timestamps
end
end
def self.down
drop_table :items
end
end
Rake migrates successfully and the application works as expected, but schema.rb shows only:
ActiveRecord::Schema.define(:version => 20101126074332) do
# Could not dump table "items" because of following ArgumentError
# invalid date
# Could not dump table "lists" because of following ArgumentError
# invalid date
Is anyone a bit more experienced with Rails that could offer advice on what might be causing the problem? Googling turned up nothing.

Get the mysql 5.1 libmysql.dll as described in:
https://github.com/brianmario/mysql2/issues#issue/71

Try rake db:schema:dump.

Related

What is the best practice when it comes to deleting a table in the Ruby on Rails migration?

I created a few tables last week and I was told to delete them. The suggested way in which I should do it looks like this:
Sample table:
class CreateMenuTable < ActiveRecord::Migration
def change
create_table :menus do |t|
t.string :name
t.timestamps null: false
end
end
end
class CreateSpreeMenuRole < ActiveRecord::Migration
def change
create_table :menu_roles do |t|
t.belongs_to :role, class_name: 'Role'
t.belongs_to :menu, class_name: 'Menu'
t.timestamps null: false
end
end
end
step 1. bin/rake db:rollback STEP=6
step 2. after rolling back I have to comment out those tables then
step 3. bin/rake db:migrate
Btw we still have multiple tables created after that, that's why we still need to run migrate.
Generate a migration to drop these tables rails g migration DropMenuTable and rails g migration DropMenuRolesTable
Drop the Tablet by either
migration not reversible
class DropMenuTable < ActiveRecord::Migration
def up
drop_table :menus
end
def down
fail ActiveRecord::IrreversibleMigration
end
end
class DropMenuRolesTable < ActiveRecord::Migration
def up
drop_table :menu_roles
end
def down
fail ActiveRecord::IrreversibleMigration
end
end
or
migration is reversible
class DropMenuTable < ActiveRecord::Migration
def change
drop_table :menues do |t|
t.string :name
t.timestamps null: false
end
end
end
class DropMenuRolesTable < ActiveRecord::Migration
def change
drop_table :menu_roles do |t|
t.belongs_to :role, class_name: 'Role'
t.belongs_to :menu, class_name: 'Menu'
t.timestamps null: false
end
end
end
run rake db:migrate
There will be unsuitable commit if you just comment out migration.
Also you will delete all data in other tables.
So the best practice to drop table via migration like this:
class DropMenuTable < ActiveRecord::Migration
def change
drop_table :menus
end
end
class DropMenuRolesTable < ActiveRecord::Migration
def change
drop_table :menu_roles
end
end
Migrations allow you to evolve your database schema overtime.
When your app is in development it may seem easy to just rollback the migration and make changes, or even delete it. But you shouldn't do it when you have code deployed to production or is sharing code with another developer. Your database will likely become corrupted and will produce hard to fix bugs.
The best thing you can do to delete a table is to create a new migration and drop_table as suggested by spickermann.

How do you migrate db structure changes to heroku postgres?

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.

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)

Migration a has_and_belongs_to_many join table don't create the table

I'm develop a web app with Rails 3.0.9 and Postgres 9.4
I'm trying to create a join table for a has_and_belongs_to_many association, but when execute "rake db:migrate" the only one not executed migration is the migration for join table.
Rails didn't show any error, only didn't create the table.
When I do the rollback, rails show a error because couldn't drop the table because don't exist.
Here is the code of migration:
class CreateCampanaLocalJoinTable < ActiveRecord::Migration
def self.up
def change
create_table :campanas_locals, :id => false do |t|
t.integer :campana_id
t.integer :local_id
end
end
end
def self.down
drop_table :campanas_locals
end
end
Anyone have an idea? Thanks!
Rails 3.0.X try:
class CreateCampanaLocalJoinTable < ActiveRecord::Migration
def self.up
create_table :campanas_locals, :id => false do |t|
t.integer :campana_id
t.integer :local_id
end
end
def self.down
drop_table :campanas_locals
end
end
Rails 3.1.X try:
class CreateCampanaLocalJoinTable < ActiveRecord::Migration
def change
create_table :campanas_locals, :id => false do |t|
t.integer :campana_id
t.integer :local_id
end
end
end

Resources