Rails mountable engine: insert data rows in migration - ruby-on-rails

Inside Rails mountable engine I generated a model:
rails g model Sources domain:string source_alias:string is_social:boolean is_organic:boolean organic_query_param:string
Here is migration file (20131220160128_create_sourcebuster_sources.rb)
class CreateSourcebusterSources < ActiveRecord::Migration
def change
create_table :sourcebuster_sources do |t|
t.string :domain, null: false
t.string :source_alias
t.boolean :is_social, default: false
t.boolean :is_organic, default: false
t.string :organic_query_param
t.timestamps
end
add_index :sourcebuster_sources, :domain, unique: true
add_index :sourcebuster_sources, :is_social
add_index :sourcebuster_sources, :is_organic
Sourcebuster::Source.create domain: "yahoo.com",
source_alias: "yahoo.com",
is_social: false,
is_organic: true,
organic_query_param: "p"
end
end
Copy it to dummy app:
rake sourcebuster:install:migrations
Migration file with the different name — 20131220164445_create_sourcebuster_sources.sourcebuster.rb — but content is the same as above.
Ok, bundle exec rake db:migrate
And it gives me an error, when trying to insert data row:
== CreateSourcebusterSources: migrating ======================================
-- create_table(:sourcebuster_sources)
-> 0.0364s
-- add_index(:sourcebuster_sources, :domain, {:unique=>true})
-> 0.0066s
-- add_index(:sourcebuster_sources, :is_social)
-> 0.0069s
-- add_index(:sourcebuster_sources, :is_organic)
-> 0.0046s
rake aborted!
An error has occurred, this and all later migrations canceled:
uninitialized constant Sourcebuster::Source/Users/Alex/Ruby/sourcebuster/test/dummy/db/migrate/20131220164445_create_sourcebuster_sources.sourcebuster.rb:17:in `change'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:571:in `exec_migration'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:555:in `block (2 levels) in migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:554:in `block in migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:553:in `migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:709:in `migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:959:in `block in execute_migration_in_transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:1005:in `block in ddl_transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/transactions.rb:209:in `transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:1005:in `ddl_transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:958:in `execute_migration_in_transaction'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:920:in `block in migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:916:in `each'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:916:in `migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:764:in `up'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/migration.rb:742:in `migrate'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/gems/activerecord-4.0.2/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top (required)>'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/bin/ruby_noexec_wrapper:14:in `eval'
/Users/Alex/.rvm/gems/ruby-2.0.0-p353#sourcebuster/bin/ruby_noexec_wrapper:14:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Why insertion fails?

try inserting this method
class CreateSourcebusterSources < ActiveRecord::Migration
def migrate(direction)
super
# Create a default Sourcebuster
Sourcebuster::Source.create! domain: "yahoo.com",
source_alias: "yahoo.com",
is_social: false,
is_organic: true,
organic_query_param: "p" if direction == :up
end
#def change
...
end

Related

First course table to add a field open, type Boolean;rake db:migrate Operation error?

class AddOpenAttributeToCourses < ActiveRecord::Migration
def change
add_column :courses, :open, :boolean
change_column_default(:courses, :open, {:from=>true, :to=>false})
end
end
!!!!!then Operation!!!!!!
rake db:migrate
== 20161126133112 AddOpenAttributeToCourses: migrating ========================
-- add_column(:courses, :open, :boolean)
-> 0.1047s
-- change_column_default(:courses, :open, {:from=>true, :to=>false})
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::InvalidTextRepresentation: ERROR: invalid input syntax for type boolean: "---
:from: true
:to: false
"
: ALTER TABLE "courses" ALTER COLUMN "open" SET DEFAULT '---
:from: true
:to: false
'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `async_exec'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:155:in `block in execute'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract_adapter.rb:472:in `block in log'
/usr/local/rvm/gems/ruby-2.3.0/gems/activesupport-4.2.5.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract_adapter.rb:466:in `log'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:154:in `execute'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/postgresql/schema_statements.rb:455:in `change_column_default'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:665:in `block in method_missing'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:634:in `block in say_with_time'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:634:in `say_with_time'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:654:in `method_missing'
/home/ubuntu/workspace/db/migrate/20161126133112_add_open_attribute_to_courses.rb:4:in `change'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:608:in `exec_migration'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:591:in `block in migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:590:in `migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:768:in `migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:998:in `block in execute_migration_in_transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:1044:in `block in ddl_transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract/transaction.rb:184:in `within_new_transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/transactions.rb:220:in `transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:1044:in `ddl_transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:997:in `execute_migration_in_transaction'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:959:in `block in migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:955:in `each'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:955:in `migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:823:in `up'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/migration.rb:801:in `migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/usr/local/rvm/gems/ruby-2.3.0/gems/activerecord-4.2.5.2/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
/usr/local/rvm/gems/ruby-2.3.0/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
ActiveRecord::StatementInvalid: PG::InvalidTextRepresentation: ERROR: invalid input syntax for type boolean: "---
:from: true
:to: false
"
You're trying to use the Rails5 arguments to change_column_default:
change_column_default(table_name, column_name, default_or_changes)
[...]
Passing a hash containing :from and :to will make this change reversible in migration:
change_column_default(:posts, :state, from: nil, to: "draft")
with Rails4:
change_column_default(table_name, column_name, default)
Sets a new default value for a column: [...]
The funny looking stuff you see in the SQL:
---
:from: true
:to: false
is the YAML version of your { :from => true, :to => false } Ruby Hash BTW.
You need to use separate up and down methods and simpler version of change_column_default with Rails4:
def up
change_column_default(:courses, :open, false)
end
def down
change_column_default(:courses, :open, true)
end
or just set the default when you create the column:
class AddOpenAttributeToCourses < ActiveRecord::Migration
def change
add_column :courses, :open, :boolean, :default => false
end
end
BTW, since you didn't specify a :default when you added the column it would have a default of null, not true.
You can create the column and set a default in one line:
add_column :courses, :open, :boolean, default: true
Not sure exactly why you're getting that error. Maybe it's because you say "change the default from false to true" but the default was not false, actually it was nil.

Rails migration error with id default null

I'm trying to get a legacy Rails app up and running and when I do a
rake db:migrate
I get the following:
arx_rails [master●] % rake db:migrate
== AddSessionsTable: migrating ===============================================
-- create_table(:sessions)
rake aborted!
An error has occurred, all later migrations canceled:
Mysql2::Error: All parts of a PRIMARY KEY must be NOT NULL; if you need NULL in a key, use UNIQUE instead: CREATE TABLE `sessions` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `session_id` varchar(255) NOT NULL, `data` text, `created_at` datetime NOT NULL, `updated_at` datetime NOT NULL) ENGINE=InnoDB/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `query'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `block in execute'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activesupport-3.2.8/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:245:in `execute'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/mysql2_adapter.rb:211:in `execute'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/schema_statements.rb:170:in `create_table'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:434:in `create_table'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:466:in `block in method_missing'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:438:in `block in say_with_time'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:438:in `say_with_time'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:458:in `method_missing'
/Users/steve/projects/asu/arx_rails/db/migrate/20120825213542_add_sessions_table.rb:3:in `change'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:407:in `block (2 levels) in migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:407:in `block in migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:123:in `with_connection'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:389:in `migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:528:in `migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:720:in `block (2 levels) in migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:777:in `call'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:777:in `ddl_transaction'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:719:in `block in migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:700:in `each'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:700:in `migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:570:in `up'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/migration.rb:551:in `migrate'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/gems/activerecord-3.2.8/lib/active_record/railties/databases.rake:153:in `block (2 levels) in <top (required)>'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/bin/ruby_executable_hooks:15:in `eval'
/Users/steve/.rvm/gems/ruby-1.9.3-p194/bin/ruby_executable_hooks:15:in `<main>'
The migration file is:
class AddSessionsTable < ActiveRecord::Migration
def change
create_table :sessions do |t|
t.string :session_id, :null => false
t.text :data
t.timestamps
end
add_index :sessions, :session_id
add_index :sessions, :updated_at
end
end
Rails has always created the id field itself without having to have it explicitly coded in the migration file and that it makes it a primary key but without a default value. So, I'm wondering where it might be setting the id fields to a DEFAULT of NULL for newly created tables. In some configuration file somewhere I don't know about?
Has anyone seen this before?
It looks like an issue with Rails and MySQL:
https://github.com/rails/rails/pull/13247
In the notes, user "pjg" suggests the following:
With Rails 2.3.5, MySQL version 5.7.9 and mysql gem you need to have
this bit as an initializer in
config/initializers/abstract_mysql_adapter.rb:
class ActiveRecord::ConnectionAdapters::MysqlAdapter
NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
end
For mysql2 it should be
config/initializers/abstract_mysql2_adapter.rb:
class ActiveRecord::ConnectionAdapters::Mysql2Adapter
NATIVE_DATABASE_TYPES[:primary_key] = "int(11) auto_increment PRIMARY KEY"
end
Does that resolve the issue?
(EDIT: giving credit to solution provider)
For Rails 3.0.5 works that way:
class ActiveRecord::ConnectionAdapters::ColumnDefinition
def sql_type
type.to_sym == :primary_key ? 'int(11) auto_increment PRIMARY KEY' : base.type_to_sql(type.to_sym, limit, precision, scale) rescue type
end
end
With Rails 4.0.6 works fine:
config/initializers/abstract_mysql2_adapter.rb:
class ActiveRecord::ConnectionAdapters::Mysql2Adapter
NATIVE_DATABASE_TYPES[:primary_key] = "int(11) NOT NULL auto_increment PRIMARY KEY"
end
$ gem list mysql
* LOCAL GEMS *
mysql2 (0.3.18)

Error trying to deploy to heroku (following ror tutorial) PG::Error: ERROR: column "password_digest" of relation "users" already exists

So I'm following the http://www.railstutorial.org/book, and evrything works fine locally (running sqlight3).
I get the following error when I try
heroku run rake db:migrate
This is what the error message looks like
Running rake db:migrate attached to terminal... up, run.4049
Migrating to AddPasswordDigestToUsers (20140817014655)
== 20140817014655 AddPasswordDigestToUsers: migrating =========================
-- add_column(:users, :password_digest, :string) PG::Error: ERROR: column "password_digest" of relation "users" already exists : ALTER
TABLE "users" ADD COLUMN "password_digest" character varying(255) rake
aborted! StandardError: An error has occurred, this and all later
migrations canceled:
PG::Error: ERROR: column "password_digest" of relation "users"
already exists : ALTER TABLE "users" ADD COLUMN "password_digest"
character
varying(255)/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in
exec'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in
block in execute'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract_adapter.rb:442:in block in log'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.8/lib/active_support/notifications/instrumenter.rb:20:in
instrument'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract_adapter.rb:437:in log'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql/database_statements.rb:127:in
execute'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/schema_statements.rb:360:in
add_column'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql/schema_statements.rb:395:in
add_column'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:629:in
block in method_missing'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:601:in
block in say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:601:in
say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:621:in
method_missing'
/app/db/migrate/20140817014655_add_password_digest_to_users.rb:3:in
change'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:575:in
exec_migration'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:559:in
block (2 levels) in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:558:in
block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in
with_connection'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:557:in
migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:713:in
migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:963:in
block in execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:1009:in
block in ddl_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/database_statements.rb:203:in
block in transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in
within_new_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/database_statements.rb:203:in
transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/transactions.rb:209:in transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:1009:in
ddl_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:962:in
execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:924:in
block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:920:in
each'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:920:in
migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:768:in
up'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:746:in
migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/railties/databases.rake:42:in
`block (2 levels) in ' Tasks: TOP => db:migrate (See
full trace by running task with --trace)
I already tried heroku pg:reset DATABASE_URL and then retrying.
Also tried adding /spec, /lib, /script, /features, /cucumber.yml to .slugignore
This is what my schema looks like:
ActiveRecord::Schema.define(version: 20140818041701) do
create_table "users", force: true do |t|
t.string "name"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
t.string "password_digest"
t.string "remember_token"
t.boolean "admin", default: false
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["remember_token"], name: "index_users_on_remember_token"
end
And this is what my password digest migration looks like
class AddPasswordDigestToUsers < ActiveRecord::Migration
def change
add_column :users, :password_digest, :string
end
end
You already have password_digest column in your table and with migration AddPasswordDigestToUsers you are trying to create another one that's why it creating issue. Remove migration AddPasswordDigestToUsers and try run rake db:migrate again. Sqlite3 not producing any error in such cases. if you write intege instead of integer in migration it will not produce any error if you use sqlite.
If you simply delete the password digest migration file, it will solve the short term problem of deploying to heroku but then it won't be available for collaborators to pull down and set up their database locally. They will run into issues and may create another migration file which creates the same problem.
I'm not sure the best solution, but this seems to be working out for me so far:
class AddPasswordDigestToUsers < ActiveRecord::Migration
def change
if Rails.env == "production"
else
add_column :users, :password_digest, :string
end
end
end

rails production migration error - undefined method `-#' for nil:NilClass

I just faced with problem when my migration with custom sql works fine on development environment and failed on production.
My migration looks like this:
class DbPatch < ActiveRecord::Migration
def up
execute("START TRANSACTION")
execute("update users set status = 1 where status = 3")
execute("COMMIT")
end
def down
execute("START TRANSACTION")
execute("update users set status = 3 where status = 1")
execute("COMMIT")
end
end
And on production env migration failed with next error message:
== 20140627155848 DbPatch: migrating =================================
-- execute("START TRANSACTION")
-> 0.0001s
-- execute("update users set status = 1 where status = 3")
-> 0.0003s
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
undefined method `-#' for nil:NilClass/var/www/app/releases/20140628083752/db/migrate/20140627155848_db_patch.rb:4:in `up'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:598:in `exec_migration'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:579:in `block (2 levels) in migrate'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:578:in `block in migrate'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:577:in `migrate'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:752:in `migrate'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:994:in `block in execute_migration_in_transaction'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:1042:in `ddl_transaction'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:993:in `execute_migration_in_transaction'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:954:in `block in migrate'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:950:in `each'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:950:in `migrate'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:808:in `up'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/migration.rb:785:in `migrate'
/var/www/app/shared/bundle/ruby/2.0.0/gems/activerecord-4.1.0/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
After an half hour of research in ActiveRecord sources I recognized that problem was in my custom 'START TRANSACTION'.
It looks like rails automatically execute all migrations on production environment in transaction.
So the next code works correct:
class DbPatch < ActiveRecord::Migration
def up
execute("update users set status = 1 where status = 3")
end
def down
execute("update users set status = 3 where status = 1")
end
end
I'm posting this topic here to keep knowledges not only in my head :)

Heroku pg migration error "PG:DataCorrupted: Error"

I am getting the following error when trying to run heroku run rake db:migrate. I have tried heroku pg:reset, I have tried heroku restart. I also tried creating a new app and starting over, but the problem persists. What could be the problem here?
Running `rake db:migrate` attached to terminal... up, run.7662
Migrating to CreateChannels (20140424224543)
== CreateChannels: migrating =================================================
-- create_table(:channels)
PG::DataCorrupted: ERROR: could not read block 0 in file "base/57396/12684": read only 0 of 8192 bytes
: CREATE TABLE "channels" ("id" serial primary key, "name" character varying(255), "user_id" integer, "created_at" timestamp, "updated_at" timestamp)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::DataCorrupted: ERROR: could not read block 0 in file "base/57396/12684": read only 0 of 8192 bytes
: CREATE TABLE "channels" ("id" serial primary key, "name" character varying(255), "user_id" integer, "created_at" timestamp, "updated_at" timestamp) /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `async_exec'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `block in execute'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:435:in `block in log'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:430:in `log'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/postgresql/database_statements.rb:127:in `execute'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/schema_statements.rb:190:in `create_table'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:625:in `block in method_missing'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:597:in `block in say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:597:in `say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:617:in `method_missing'
/app/db/migrate/20140424224543_create_channels.rb:3:in `change'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:571:in `exec_migration'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:555:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:554:in `block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:553:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:709:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:959:in `block in execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:1005:in `block in ddl_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/transactions.rb:209:in `transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:1005:in `ddl_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:958:in `execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:920:in `block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:916:in `each'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:916:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:764:in `up'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/migration.rb:742:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.2/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
[Edit] My migration file for creating the channels table looks like the following (Nothing out of the ordinary)
class CreateChannels < ActiveRecord::Migration
def change
create_table :channels do |t|
t.string :name
t.integer :user_id
t.timestamps
end
end
end
I had the same issue, I deleted the database from https://dashboard.heroku.com/apps (this was Navy)
Added a new PG Database (this was blue)
From the terminal ran:
heroku config
You should see your db url
heroku config:set DATABASE_URL=postgres://your-db-url
Ran
heroku run rake db:migrate
And everything worked.
I am still not sure why that happened!
I contacted heroku support team last night, I was having the same issue, everything is back to normal now. bellow is their response.
Unfortunately, it looks like there was an issue with your database
last night which caused an outage for a couple hours, however it's
since been recovered by one of our on-call database engineers. This
issue wasn't caused by you at all, it was a hardware failure on the
host server.
Hope this helps.
I'm now having the same issue. I didn't have any production data so will just set up a new database. Very annoying though
Edit: Has just fixed itself when I ran the migration again
Edit 2: Is still broken. When I tried to run my second migration file I get the error
PG::DataCorrupted: ERROR: could not read block 1 in file "base/117039/12684": read only 0 of 8192 bytes
Very annoying
Just tried to file a ticket on Heroku. Filled out the form with loads of information and submitted only to get a message to say that I have not created any support tickets!

Resources