schema.rb - dump errors - NoMethodError undefined method `default_function' - ruby-on-rails

I went to look at my schema.rb file and found the following:
ActiveRecord::Schema.define(version: 20140729164926) do
# Could not dump table "account_services_indices" because of following NoMethodError
# undefined method `default_function' for
#<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x757a77>
...
Those last three lines are repeated for every table in the database!
The database seems to be working fine. I can do a db:rollback and db:migrate with no problem except that the regenerated schema.rb still looks like the above. Looking back through my GIT repository, it happened about 7/29/14. The only other db related change in that commit was a minor migration, adding a column to each of two tables. I tried a db:rollback, then db:schema:dump, but no luck. Other commits at that point were changes to views and controllers only.
I'm using the 'activerecord-jdbcpostgresql-adapter' gem. Version 1.3.9 was released July 7, 2014. Forced that back to version 1.3.8 in the Gemfile, bundled and tried db:schema:dump again, but still the same thing. Dropping the database and starting over might work and wouldn't be catastrophic, but I hate to lose all my development data right now.
Renamed the database in database.yml. Created that new database and built it out with db:migrate. Resulting schema.rb is still a list of the same errors.
Any suggestions?

Solved it!
I'm still not sure of the exact cause but here's my resolution. I created a rails app from scratch called Blog, then started to replicate the gems I'm using in the real application. The test app was generated with
gem 'rails', '4.1.4'
and worked with no problem. I changed that to
gem 'rails', '4.0.0'
to match my real app and did a bundle update and bundle install. Doing a rake db:schema:dump then gave me the following error:
rake aborted!
NoMethodError: undefined method `configure' for #<Blog::Application:0x6932cb>
/home/mpipkorn/rails_projects/blog/config/environments/development.rb:1:in `(root)'
/home/mpipkorn/rails_projects/blog/config/environment.rb:5:in `(root)'
Tasks: TOP => db:schema:dump => environment
Following a lead in StackOverflow, I did the following:
Change the first line in app/config/initializers/development.rb from
Rails.application.configure do
to
Blog::Application.configure do
That matches the format in my real app. I did rake db:schema:dump again. This time the rake task ran with no errors, but looking at the resulting schema.rb file, I see
ActiveRecord::Schema.define(version: 20140806174338) do
# Could not dump table "articles" because of following NoMethodError
# undefined method `default_function' for #<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x183e66e>
end
Changing the Blog Gemfile (back) to
gem 'rails', '4.1.4'
and doing a bundle update and bundle install, then doing a rake db:schema:dump now results in a normal looking schema.rb file with no error messages!
The bottom line is that changing the rails gem from 4.0.0 to 4.1.4 in my real application solved my problem. What exactly caused this to begin with I'm still not certain, but an update along the way seems to be at fault and there are certain version combinations of gems that don't work, perhaps the combination of activerecord-jdbcpostgresql-adapter 1.3.9 with Rails 4.0.0.

Related

Unable to run ActiveRecord Migration

I added the gemfile:
gem 'activerecord-reputation-system', require: 'reputation_system'
to my Rails app in order to add upvote/downvote functionity to a model (I'm following Railscast #364). I installed the gem with no problems.
I entered 'rails g reputation_system' and rake db:migrate, but the rake was aborted:
Gem::LoadError: You have already activated rake 12.3.0, but your
Gemfile requires rake 12.0.0. Prepending `bundle exec` to your command
may solve this.
I tried it with 'bundle exec', but the rake was aborted again:
StandardError: An error has occurred, this and all later migrations
canceled:
Directly inheriting from ActiveRecord::Migration is not supported.
Please specify the Rails release the migration was written for:
I updated my gems to see if this was the trouble, but this made no difference.
I can't think of anything else I could do other than finding some way of removing rake 12.3.0 and installing 12.0.0, but this seems illogical.
Any help would be much appreciated, thanks :-)
In Rails 5 migrations require you to specify the Rails version you’re using (the migration is specified for) at the top of your migration file, as the error states.
This looks like this:
class CreateYourModels < ActiveRecord::Migration[5.1]
...your migration code
end
Where the bit in brackets is the Rails version you're using.
Then try to run bundle exec rake db:migrate.
Additional explanation: https://blog.bigbinary.com/2016/03/01/migrations-are-versioned-in-rails-5.html

rails_sql_views prevents migrations on Heroku

I have a rails app that requires multi-table inheritance for which I am using the CITIER gem which itself requires the rails_sql_views gem to produce database views representing objects.
So far, so good. All this works like a charm on my local machine using sqlite3. When I try to deploy the application to Heroku, however, I am neither able to perform a database migration nor able to access the rails console.
The reason seems to be that the rails_sql_views gem tries to access some methods of PostgreSQLAdapter which are claimed to be non-existent. However, when I check the files of the rails repository, I can find these methods in the code. What is going on here?
When I try to run "heroku run rake db:migrate", I'll end up with the following error trace:
Running rake db:migrate --trace attached to terminal... up, run.1 rake aborted!
undefined method `tables' for class `ActiveRecord::ConnectionAdapters::PostgreSQLAdapter'
/app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.3/lib/active_support/core_ext/module/aliasing.rb:31:in `alias_method'
/app/vendor/bundle/ruby/1.9.1/gems/activesupport-3.2.3/lib/active_support/core_ext/module/aliasing.rb:31:in `alias_method_chain'
/app/vendor/bundle/ruby/1.9.1/gems/rails_sql_views-0.8.0/lib/rails_sql_views/connection_adapters/postgresql_adapter.rb:5:in `included'
/app/vendor/bundle/ruby/1.9.1/gems/rails_sql_views-0.8.0/lib/rails_sql_views/loader.rb:12:in `include'
[....]
This looks like an incompatibility between sqlite3 and postgres adapters with that gem. You should try to keep development as close to production as possible. Try installing postgress locally https://devcenter.heroku.com/articles/local-postgresql and see if you get the same error locally. If you do, consider writing a detailed bug report and sending it to the author of the Gem, or attempt to fix it yourself and send a pull request.

Ruby on Rails - Heroku deployment issues

I seem to have reached a bump, searched here and on other foruns but nothing. I'm running rails 3.2.3 and ruby 1.9.3 and want to deploy my app on heroku.
I've created the cedar and although I can git push heroku master I am getting a complete 500 server error.
I suspect is it because my DB isn't there. However, I can't seem to get it there.
I've run:
heroku run rake db:create -> This gives out some warnings about deprecation and then dkhgclqccm already exists
So it exists already? So lets migrate it:
heroku run rake db:migrate
However this outputs:
<deprecation errors>
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::Error: ERROR: relation "hphotos" does not exist
: ALTER TABLE "hphotos" ADD COLUMN "description" character varying(255)
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
That particular migration is:
class AddDescriptionToHphotos < ActiveRecord::Migration
def change
add_column :hphotos, :description, :string
end
end
It seems good to me, don't know why its giving me this error :/
One Last thing, even if this migrations worked, my DB would be empty and me seeds.rb doesn't have all the necessary data for the database. So I though about pushing the hole DB.
heroku db:push
! Taps Load Error: cannot load such file -- sqlite3
! You may need to install or update the taps gem to use db commands.
! On most systems this will be:
!
! sudo gem install taps
Why is this showing?
I installed the 'heroku' gem and the 'taps' gem and I got this in my gem file:
group :development, :test do
gem 'mysql2'
end
group :production do
gem 'pg'
end
Also, when I run heroku run rake db:version, it shows: Current version: 20120508130957
This is actually 5 migrations short on my currrent version, but I can't migrate it as shows the error I spoke of above...
Thanks in advance
Heroku comes with a database set up (which is why db:create didn't work). have you tried heroku run rake db:schema:load? This should take your schema.rb file and load it up into the DB. This is a much better way of doing it than db:migrate every time you want to setup a new DB
Edit:
For your last question about taps, it looks like it's trying to use sqlite locally but you only have pg in your Gemfile. You probably have in config/database.yml adapter: sqlite. So either you need to use postgres locally and change that adapter to postgres, or go the easier route and use sqlite locally and add that to the :development group.
Note that heroku only uses postgres so I would not suggest developing off of mysql since there are some inconsistencies in some syntax and how you do a couple of things between the two platforms. Then again, if you're using only ANSI-compatible queries or just using rails' methods to activate queries then you should be ok either way.
I think you need to check your migrations closely to see if you actually have a file which says:
def up
create_table :hphotos do |t|
[...]
end
It seems that the table hasn't been created remotely and you are attempting to modify it.
The solution is to add not only taps gem but also sqlite3 gem into the Gemfile, into the :development group. If you are using sqlite3 in your development, then adding taps gem would be enough.
But since you are using mysql2 on your development so to solve that problem you have to add both.
group :development do
gem 'taps'
gem 'sqlite3'
end

Heroku rake db:migrate failing with following error

So after pushing my app to heroku, I tried to migrate database to heroku. However, I get the following error.
C:\Users\Administrator\Ruby\Myapp>heroku rake db:migrate
rake aborted!
undefined method `task' for #<MyApp::Application:0x7fc922c28dd0>
In local, everything works just fine including migration.
Also, migration worked just fine about few days ago, therefore it seems the error is after the rake version which changed from 0.8.7 to 0.9.0 in Heroku.
How can I resolve this problem?
had this problem yesterday and posted here: Rake 0.9.0 'undefined method 'task' '
Several solutions have been specified.
Try adding
gem 'rake', '0.8.7'
to your Gemfile, to control the version of Rake that Heroku uses on your app.

issues with Spree

I am trying to install and run Spree on my local machine by following the steps mentioned in Getting started with Spree
However, when I start the server I get the following error:
Could not find table 'pages'
Can someone please help me out with this?
Solved the issue!
Generated the following generators:
spree_static_content and spree_product_assembly which did the magic!
Could not find table 'pages' means rails is not being able to find that table in the database.
In those instructions, review the section 'configuring the database' and then '4.6 Populating the Database'.
It sounds like you want to give spree a go, but don't have experience with Rails.
The spot where you are stuck is not something specific to spree, its a step required in setting up all rails projects referred to as database migration.
For what you need to know about migrations the official Rails Guides are great.
http://guides.rubyonrails.org/migrations.html
For a comprehensive intro to Rails which may also answer a few of your other questions, check out http://railstutorial.org/ruby-on-rails-tutorial-book
If you just want to try stuff.. the spree tute is on track
Do these steps again..
(If it generates an error, to a rake db:drop first to get rid of what you have already done)
rake db:create
rails g spree:site
rake spree:install
rake spree_sample:install
rake db:bootstrap
rake db:migrate
rake db:seed
rake db:sample
rake db:admin:create
Here is what I did to get Spree up and running:
Create a new rails project:
$ rails new spree_project
Add these 5 gems to the projects Gemfile (/spree_project/Gemfile):
gem 'spree', :git => 'git://github.com/spree/spree.git'
gem 'spree_auth_devise', :git => 'git://github.com/spree/spree_auth_devise'
gem 'spree_gateway', :git => 'git://github.com/spree/spree_gateway.git'
gem 'spree_usa_epay'
gem 'spree_skrill'
Run a bundle install and setup the database (rake db:bootstrap did not work for me)
$ bundle install
$ rake db:migrate
$ rake db:seed
$ bundle exec rake spree_sample:load
The 'pages' table is used by the spree_static_content gem. You can either remove the gem from your gemfile, or you can generate migrations for the static content gem:
rails generate spree_static_content:install
If you've included the 'spree_product_assembly' gem as well, you'll want to do the same for it:
rails generate spree_product_assembly:install
Then, reset the database (just to make sure)
rake db:bootstrap
rake db:admin:create
I ran into similar issues after trying the http://spreecommerce.com/documentation/getting_started.html instructions, but it seems to have created a nice demo app after taking these additional steps.

Resources