I'm a bit stuck in migrating database in Heroku. I've deployed app and in final stage I've done this to have PostgreSQL database migration:
ubuntu#ubuntu-xenial:/vagrant/myappname$ heroku run rake db:migrate --trace
Running rake db:migrate --trace on ⬢ myappname... up, run.5129 (Free)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
(12.1ms) CREATE TABLE "schema_migrations" ("version" character varying PRIMARY KEY)
(6.8ms) CREATE TABLE "ar_internal_metadata" ("key" character varying PRIMARY KEY, "value" character varying, "created_at" timestamp NOT NULL, "updated_at" timestamp NOT NULL)
(1.1ms) SELECT pg_try_advisory_lock(5132908415839787310);
ActiveRecord::SchemaMigration Load (1.2ms) SELECT "schema_migrations".* FROM "schema_migrations"
ActiveRecord::InternalMetadata Load (1.1ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2 [["key", :environment], ["LIMIT", 1]]
(1.4ms) BEGIN
SQL (1.2ms) INSERT INTO "ar_internal_metadata" ("key", "value", "created_at", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "key" [["key", "environment"], ["value", "production"], ["created_at", 2017-01-26 05:33:42 UTC], ["updated_at", 2017-01-26 05:33:42 UTC]]
(1.7ms) COMMIT
(0.9ms) SELECT pg_advisory_unlock(5132908415839787310)
** Invoke db:_dump (first_time)
** Execute db:_dump
ubuntu#ubuntu-xenial:/vagrant/myappname$ heroku pg:psql
--> Connecting to postgresql-cylindrical-63703
psql (9.6.1)
SSL connection (protocol: TLSv1.2, cipher: ECDHE-RSA-AES256-GCM-SHA384, bits: 256, compression: off)
Type "help" for help.
mydatabasename::DATABASE=> \dt
List of relations
Schema | Name | Type | Owner
--------+----------------------+-------+----------------
public | ar_internal_metadata | table | OWNER
public | schema_migrations | table | OWNER
(2 rows)
It looks like it is not loading my Rails5 app schema.rb
What could be wrong, why there is no tables of my app? Thank you!
Related
I have a simple migration which fails: The database is not affected, it still contains the old column name after migrating.
class RenameTypeToLicenseTypeInLicenses < ActiveRecord::Migration[5.1]
def change
rename_column :licenses, :type, :license_type
end
end
Renaming type to license_type is necessary because of this error:
The single-table inheritance mechanism failed to locate the subclass: 'creative commons'. This error is raised because the column 'type' is reserved for storing the class in case of inheritance. Please rename this column if you didn't intend it to be used for storing the inheritance class or overwrite License.inheritance_column to use another column for that information.
This is the output of rails db:migrate
igrating to RenameTypeToLicenseTypeInLicenses (20210422110849)
(0.2ms) BEGIN
== 20210422110849 RenameTypeToLicenseTypeInLicenses: migrating ================
== 20210422110849 RenameTypeToLicenseTypeInLicenses: migrated (0.0000s) =======
SQL (0.9ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) RETURNING "version" [["version", "20210422110849"]]
(1.4ms) COMMIT
ActiveRecord::InternalMetadata Load (0.5ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2 [["key", "environment"], ["LIMIT", 1]]
(0.3ms) BEGIN
(0.3ms) COMMIT
(0.4ms) SELECT pg_advisory_unlock(2195010657070977375)
(0.6ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
How can I rename the type column?
edit:
As there's only little data in the table, I tried another approach:
remove_column :licenses, :type, :string
add_column :licenses, :license_type, :string
It doesn't work either. Output is
[NAME COLLISION] `type` is a reserved key in LicenseResource.
(0.7ms) SELECT pg_try_advisory_lock(2195010657070977375);
(1.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Migrating to ChangeTypeToLicenseTypeFromLicenses (20210422122638)
(0.5ms) BEGIN
== 20210422122638 ChangeTypeToLicenseTypeFromLicenses: migrating ==============
== 20210422122638 ChangeTypeToLicenseTypeFromLicenses: migrated (0.0000s) =====
SQL (1.0ms) INSERT INTO "schema_migrations" ("version") VALUES ($1) RETURNING "version" [["version", "20210422122638"]]
(4.6ms) COMMIT
ActiveRecord::InternalMetadata Load (0.4ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2 [["key", "environment"], ["LIMIT", 1]]
(0.4ms) BEGIN
(0.4ms) COMMIT
(0.6ms) SELECT pg_advisory_unlock(2195010657070977375)
(0.9ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Yeah, I wonder if ActiveRecord's migration code might be trying its best to keep type intact, on the assumption that it's currently being used for single table inheritance; it's oblivious to the fact you want to change it because it currently isn't 😊
There may be an idiomatic Rails way of telling the migration that what you're doing is fine, but to be honest I'd be tempted to drop down to the SQL level, thus bypassing Rails' erroneous protection:
class RenameTypeToLicenseTypeInLicenses < ActiveRecord::Migration[5.1]
def up
execute <<~SQL
ALTER TABLE licenses RENAME COLUMN type TO license_type;
SQL
end
def down
execute <<~SQL
ALTER TABLE licenses RENAME COLUMN license_type TO type;
SQL
end
end
As you're not using migration commands, you have to provide separate up/down code for both directions of the migration.
I believe this answer should help you: issue with column name 'type' in rails 3
type is one of those reserved words we shouldn't use. Check other reserved words here: https://riptutorial.com/ruby-on-rails/example/32446/reserved-word-list
I am currently trying to set up my Rails 5.1.6 system tests (using ruby 2.3.7) on Travis CI. In my travis CI tests I am using docker-compose to connect my app to a postgres database. Before each of my system tests, I am running
rake db:schema:load --trace
to clear the database to a clean state. This works fine locally in my System Tests (use postgreSQL from my dev machine). However when I run my system tests with docker-compose which use a postgres container, my rake db:schema:load --trace output is not calling all the create table commands and therefore not clearing my database as expected. Here is the output:
** Invoke db:schema:load (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Invoke db:check_protected_environments (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:check_protected_environments
(0.6ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
(0.4ms) SELECT "ar_internal_metadata"."value" FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 [["key", "environment"]]
(0.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
(0.3ms) SELECT "ar_internal_metadata"."value" FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 [["key", "environment"]]
(0.3ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
(0.3ms) SELECT "ar_internal_metadata"."value" FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 [["key", "environment"]]
** Execute db:schema:load
[4ed5f501-7177-49e5-973e-f49e0d91ff89] Started GET "/" for 127.0.0.1 at 2018-04-03 22:28:21 +0000
I am expecting it to look like this:
** Invoke db:schema:load (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Invoke db:check_protected_environments (first_time)
** Invoke environment
** Invoke db:load_config
** Execute db:check_protected_environments
(0.5ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
(0.3ms) SELECT "ar_internal_metadata"."value" FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 [["key", "environment"]]
(0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
(0.1ms) SELECT "ar_internal_metadata"."value" FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 [["key", "environment"]]
(0.1ms) SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
(0.1ms) SELECT "ar_internal_metadata"."value" FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 [["key", "environment"]]
** Execute db:schema:load
-- enable_extension("plpgsql")
SQL (0.3ms) CREATE EXTENSION IF NOT EXISTS "plpgsql"
-> 0.0123s
-- create_table("builds", {:id=>:serial, :force=>:cascade})
followed by the rest of the create_table commands until my database schema is set.
Here is my docker-compose.yml
version: '3'
services:
postgres:
image: postgres:9.5.3
volumes:
- data:/var/lib/postgresql/data
app:
image: $DOCKER_USERNAME/vizzy:$TRAVIS_COMMIT
build:
context: .
args:
RAILS_MASTER_KEY: $RAILS_MASTER_KEY
command: bundle exec rails s -p 3000 -b '0.0.0.0'
environment:
- RAILS_MASTER_KEY=$RAILS_MASTER_KEY
- POSTGRES_USER=postgres
- RAILS_ENV=ci_test
- HEADLESS=yes
volumes:
- .:/app
ports:
- 3000:3000
depends_on:
- postgres
volumes:
data:
external: true
Any help would be greatly appreciated. Weird how my system tests are passing locally but not running with docker-compose
I have tested the new rails code on c9.io and it is working fine to select the record when the emailReceived field is false on Invitation table,
After I updated the new code into Heroku, it is failed to do it. And showing the error message.
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column
"emailreceived" does not exist
I used Invitation.column_names and can find the "emailReceived". Does the database corrupted? Here is the console information for your reference. Please help to advise how to fix it.
irb(main):005:0> Invitation.column_names
=> ["id", "church", "telephone1", "telephone2", "worshipTime", "contactName", "contactNumber", "priest", "preacher", "emailReceived", "acceptPromote", "promotionTime", "other", "created_at", "updated_at"]
irb(main):006:0> Invitation.where("emailReceived = false")
Invitation Load (3.0ms) SELECT "invitations".* FROM "invitations" WHERE (emailReceived = false)
ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "emailreceived" does not exist
LINE 1: SELECT "invitations".* FROM "invitations" WHERE (emailReceiv...
^
HINT: Perhaps you meant to reference the column "invitations.emailReceived".
: SELECT "invitations".* FROM "invitations" WHERE (emailReceived = false)
from /app/vendor/bundle/ruby/2.2.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/postgresql_adapter.rb:598:in async_exec'
from /app/vendor/bundle/ruby/2.2.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/postgresql_adapter.rb:598:inblock in exec_no_cache'
from /app/vendor/bundle/ruby/2.2.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:566:in block in log'
from /app/vendor/bundle/ruby/2.2.0/gems/activesupport-5.0.0.1/lib/active_support/notifications/instrumenter.rb:21:ininstrument'
from /app/vendor/bundle/ruby/2.2.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:560:in log'
from /app/vendor/bundle/ruby/2.2.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/postgresql_adapter.rb:598:inexec_no_cache'
from /app/vendor/bundle/ruby/2.2.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/postgresql_adapter.rb:585:in execute_and_clear'
from /app/vendor/bundle/ruby/2.2.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:103:inexec_query'
from /app/vendor/bundle/ruby/2.2.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:373:in select'
from /app/vendor/bundle/ruby/2.2.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:41:inselect_all'
from /app/vendor/bundle/ruby/2.2.0/gems/activerecord-5.0.0.1/lib/active_record/connection_adapters/abstract/query_cache.rb:70:in select_all'
from /app/vendor/bundle/ruby/2.2.0/gems/activerecord-5.0.0.1/lib/active_record/querying.rb:39:infind_by_sql'
from /app/vendor/bundle/ruby/2.2.0/gems/activerecord-5.0.0.1/lib/active_record/relation.rb:699:in exec_queries'
from /app/vendor/bundle/ruby/2.2.0/gems/activerecord-5.0.0.1/lib/active_record/relation.rb:580:inload'
from /app/vendor/bundle/ruby/2.2.0/gems/activerecord-5.0.0.1/lib/active_record/relation.rb:260:in records'
from /app/vendor/bundle/ruby/2.2.0/gems/activerecord-5.0.0.1/lib/active_record/relation.rb:683:ininspect'
from /app/vendor/bundle/ruby/2.2.0/gems/railties-5.0.0.1/lib/rails/commands/console.rb:65:in start'
from /app/vendor/bundle/ruby/2.2.0/gems/railties-5.0.0.1/lib/rails/commands/console_helper.rb:9:instart'
from /app/vendor/bundle/ruby/2.2.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:78:in console'
from /app/vendor/bundle/ruby/2.2.0/gems/railties-5.0.0.1/lib/rails/commands/commands_tasks.rb:49:inrun_command!'
from /app/vendor/bundle/ruby/2.2.0/gems/railties-5.0.0.1/lib/rails/commands.rb:18:in <top (required)>'
from bin/rails:8:inrequire'
from bin/rails:8:in `'irb(main):007:0>
One more information on this case, I run "heroku run rake --trace db:migrate VERSION=20161007083159" and get the below result. And it still doesn't work
heroku run rake --trace db:migrate VERSION=20161007083159
Running rake --trace db:migrate VERSION=20161007083159 on ⬢ shrouded-scrubland-30708... up, run.1694
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
ActiveRecord::SchemaMigration Load (0.6ms) SELECT "schema_migrations".* FROM "schema_migrations"
ActiveRecord::SchemaMigration Load (0.5ms) SELECT "schema_migrations".* FROM "schema_migrations"
(0.5ms) SELECT pg_try_advisory_lock(129277373589159705);
ActiveRecord::SchemaMigration Load (0.5ms) SELECT "schema_migrations".* FROM "schema_migrations"
ActiveRecord::InternalMetadata Load (0.8ms) SELECT "ar_internal_metadata".* FROM "ar_internal_metadata" WHERE "ar_internal_metadata"."key" = $1 LIMIT $2 [["key", :environment], ["LIMIT", 1]]
(0.5ms) BEGIN
(0.4ms) COMMIT
(0.5ms) SELECT pg_advisory_unlock(129277373589159705)
** Invoke db:_dump (first_time)
** Execute db:_dump
Thanks
Patrick Lee
Eventually, I found that the problem is occurred due to the case sensitive of postgresql. So I changed the code from Invitation.where("emailReceived is ? OR emailReceived = ?", nil,false) to Invitation.where("emailReceived" => nil) on model.
But one more question, how can use OR for Invitation.where("emailReceived" => nil) Invitation.where("emailReceived" => false)?
Thanks
Patrick Lee
I am trying to migrate my app to heroku and this error came up, causing a rollback of my migration. Can anyone tell me why is there an error with date_time?
remembrance:~/rails_project/alpha-blog (master) $ heroku run rake db:migrate
Running rake db:migrate on ⬢ alpha-blog-javier... up, run.4829
ActiveRecord::SchemaMigration Load (1.9ms) SELECT "schema_migrations".* FROM "schema_migrations"
Migrating to AddDescriptionToArticles (20160816052220)
(1.7ms) BEGIN
== 20160816052220 AddDescriptionToArticles: migrating =========================
-- add_column(:articles, :description, :text)
(2.1ms) ALTER TABLE "articles" ADD "description" text
-> 0.0024s
-- add_column(:articles, :created_at, :date_time)
(4.1ms) ALTER TABLE "articles" ADD "created_at" date_time
(1.7ms) ROLLBACK
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedObject: ERROR: type "date_time" does not exist
LINE 1: ALTER TABLE "articles" ADD "created_at" date_time
It should be datetime not date_time. Read the documentation.
Change this line
add_column(:articles, :created_at, :date_time)
in your migration to
add_column(:articles, :created_at, :datetime)
When i try to do
rake db:reset
or
rake db:drop
rake db:create
rake db:schema:load
rake db:seed
I randomly get a NoMethodError: undefined method 'fields' for nil:NilClass when the seed occurs.
Here's one:
[1m[35m (186.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20130318105449')
[1m[36m (187.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20130319154146')[0m
[1m[35m (189.0ms)[0m INSERT INTO "schema_migrations" (version) VALUES ('20130322132730')
[1m[36m (104.0ms)[0m [1mINSERT INTO "schema_migrations" (version) VALUES ('20130322142814')[0m
NoMethodError: undefined method `fields' for nil:NilClass: SELECT COUNT(*)
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE c.relkind in ('v','r')
AND c.relname = 'schema_migrations'
AND n.nspname = ANY (current_schemas(false))
Here's an other one:
[1m[35m (39.0ms)[0m BEGIN
[1m[36mCategory Exists (107.0ms)[0m [1mSELECT 1 AS one FROM "categories" WHERE "categories"."hash" = 'cat1' LIMIT 1[0m
[1m[35mSQL (124.0ms)[0m INSERT INTO "categories" ("category_id", "created_at", "label", "hash", "updated_at") VALUES ($1, $2, $3, $4, $5) RETURNING "id" [["category_id", 58], ["created_at", Mon, 25 Mar 2013 15:07:53 CET +01:00], ["label", "Cat1"], ["hash", "cat1"], ["updated_at", Mon, 25 Mar 2013 15:07:53 CET +01:00]]
[1m[36m (117.0ms)[0m [1mCOMMIT[0m
[1m[35m (162.0ms)[0m BEGIN
[1m[36mCategory Exists (136.0ms)[0m [1mSELECT 1 AS one FROM "categories" WHERE "categories"."hash" = 'cat2' LIMIT 1[0m
[1m[35mSQL (104.0ms)[0m INSERT INTO "categories" ("created_at", "label", "hash", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id" [["created_at", Mon, 25 Mar 2013 15:07:53 CET +01:00], ["label", "Cat2"], ["hash", "cat2"], ["updated_at", Mon, 25 Mar 2013 15:07:53 CET +01:00]]
[1m[36m (99.0ms)[0m [1mCOMMIT[0m
[1m[35m (108.0ms)[0m BEGIN
[1m[36mCategory Exists (173.0ms)[0m [1mSELECT 1 AS one FROM "categories" WHERE "categories"."hash" = 'cat3' LIMIT 1[0m
NoMethodError: undefined method `fields' for nil:NilClass: SELECT 1 AS one FROM "categories" WHERE "categories"."hash" = 'cat3' LIMIT 1
I really do not understand where this can be coming from. Sometimes i don't even have an error and everything is perfectly inserted.
After doing some tests it seems that it depends on the speed of insertion. If i execute this from my development server, it almost always succeed. If i execute this remotly from my computer, it almost always fails.
I'm using ruby 1.9.3 locally and ruby 2 on my development server. On both i'm using rails4 (edge) and my driver is postgresql (pg, no version specified)
I'm experiencing this problem as well on rails4 (edge) and postgres. I'm running into it on a solr:reindex, so I don't think its necessarily related to insertions in any way. I'll update this when I figure out whats going wrong.
Edit:
Try updating to rails revision 1a838ccda4a31bb023985f6c977e6bc3e238cda9, it solved my problem.
This is the github issue on it: https://github.com/rails/rails/issues/9710
I found it after a quick git bisect. It's definitely fixed in git now.