user3813256
user3813256

Reputation: 812

Unable to run rake db:migrate using ruby, sinatra and activerecord

I am unable to run rake db:migrate with two separate migration files inside the db/migrate directory. I am using sinatra with activerecord (not rails).

Migration1 with timestamp:

class CreateAdmins < ActiveRecord::Migration
  #def change
  #end
  def up
     create_table :admins do |t|
        t.string :email
        t.string :name
     end
  end

  def down
      drop table :admins
  end
end

Migration2

    class CreateBills < ActiveRecord::Migration
      #def change
      #end
      def up
         create_table: bills do |t|
             t.string :email
             t.string :title
             t.text :body
         end
      end

  def down
    drop_table :bills
   end
end

Gemfile:

# Gemfile

source 'https://rubygems.org'


gem "sinatra"
gem "activerecord"
gem "sinatra-activerecord"
gem 'sinatra-flash'
gem 'sinatra-redirect-with-flash'
gem 'rake'

group :development do
 gem 'sqlite3'
 gem "tux"
end

group :production do
 gem 'sqlite3'
end

Rakefile

# Rakefile

require './app'
require 'sinatra'
require 'active_record'
require 'sinatra/activerecord/rake'
#require 'rake'

When I run rake db:migrate, I get the error copied below.

rake aborted!
Errno::ENOENT: No such file or directory @ rb_sysopen - db:migrate
/opt/test/ruby/6/app.rb:31:in `gets'
/opt/test/ruby/6/app.rb:31:in `gets'
/opt/test/ruby/6/app.rb:31:in `<top (required)>'
/opt/test/ruby/6/Rakefile:3:in `require'
/opt/test/ruby/6/Rakefile:3:in `<top (required)>'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/rake_module.rb:28:in `load'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/rake_module.rb:28:in `load_rakefile'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:689:in `raw_load_rakefile'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:94:in `block in load_rakefile'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:93:in `load_rakefile'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:77:in `block in run'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:176:in `standard_exception_handling'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/rake/application.rb:75:in `run'
/usr/local/rvm/rubies/ruby-2.2.1/lib/ruby/gems/2.2.0/gems/rake-10.4.2/bin/rake:33:in `<top (required)>'
/usr/local/rvm/gems/ruby-2.2.1/bin/rake:23:in `load'
/usr/local/rvm/gems/ruby-2.2.1/bin/rake:23:in `<main>'
/usr/local/rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `eval'
/usr/local/rvm/gems/ruby-2.2.1/bin/ruby_executable_hooks:15:in `<main>'

Update: I commented out part of my app.rb file which allowed me to run rake db:migrate.

 rake db:migrate --trace
** Invoke db:migrate (first_time)
** Invoke db:environment (first_time)
** Execute db:environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
** Invoke db:_dump (first_time)
** Execute db:_dump
** Invoke db:schema:dump (first_time)
** Invoke db:environment
** Invoke db:load_config
** Execute db:schema:dump

However, even after running rake db:migrate, I could not find the tables in the database (using sqlite). I tried adding the admin model via my ruby file but going the following error:

/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:511:in `table_structure': Could not find table 'admins' (ActiveRecord::StatementInvalid)
        from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/connection_adapters/sqlite3_adapter.rb:385:in `columns'
        from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/connection_adapters/schema_cache.rb:43:in `columns'
        from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/attributes.rb:93:in `columns'
        from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/attributes.rb:98:in `columns_hash'
        from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/inheritance.rb:205:in `subclass_from_attributes?'
        from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/inheritance.rb:54:in `new'
        from /usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.4/lib/active_record/persistence.rb:50:in `create!'

ruby version is 2.2.1p85

When I try to run rake db:rollback, it fails complaining about the fact that the tables to be dropped are no there.

I can see that the schema.rb file is not updated after rake db:migrate

ActiveRecord::Schema.define(version: 20150907140840) do

end  

When I try running rake db:create or rake db:setup, both commands fail.

Upvotes: 1

Views: 982

Answers (1)

K M Rakibul Islam
K M Rakibul Islam

Reputation: 34338

In your first migration, change this:

  def up
     create_table :admins do |t|
        t.string = email
        t.string = name
     end
  end

To:

  def up
     create_table :admins do |t|
        t.string :email
        t.string :name
     end
  end

Another issue is, in your second migration, create_table: bills do |t| should be: create_table :bills do |t|

Upvotes: 2

Related Questions