rake db:migrate:reset to dump schema - ruby-on-rails

Hell, I'm following in Hartl's Ruby on Rails tutorial and I'm on chapter 9 adding administrative control to users.
I'm at the part where it says
reset the database:
$ bundle exec rake db:migrate:reset
$ bundle exec rake db:seed
Last time I followed this (much earlier in the tutorial), it reset the database as instructed but also deleted me schema.rb. Which was annoying.
Attempted it again, and yet again it deleted my database and erased my schema.rb. Surely this is not intended.
I'm having trouble figuring out what I should be doing anymore. I'm now getting all kinds of errors with my database. I get errors about pending migrations, but no db:xxx command works, every command just spits out this long list of errors such as
/Users/Ke0/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:994:in `execute_migration_in_transaction'
/Users/Ke0/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:956:in `block in migrate'
/Users/Ke0/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:952:in `each'
/Users/Ke0/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:952:in `migrate'
/Users/Ke0/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:820:in `up'
/Users/Ke0/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:798:in `migrate'
/Users/Ke0/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:137:in `migrate'
/Users/Ke0/.rvm/gems/ruby-2.2.0/gems/activerecord-4.2.0/lib/active_record/railties/databases.rake:44:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
What does one do in such a situation?

try to verify following :-
you have the db with username/password mentioned in database.yml
you have db empty
when you run rake db:migrate,then schema.rb is getting generated
you have a valid database.yml file

Related

Pre-existing database with new rails application

I have an pre-existing database named exam_prep in postgresql, which i am using in flask.
I have created a new Rails application and i want to use that database with it.
So far I have run rake db:structure:dump, which created a structure.sql under the db folder.
After that I ran db:structure:load, but got the following error:
psql:/Users/namangupta/Desktop/app/db/structure.sql:49: ERROR: type "batch_types_enum" already exists
rake aborted!
failed to execute:
psql -v ON_ERROR_STOP=1 -q -f /Users/namangupta/Desktop/app/db/structure.sql exam_prep
Please check the output above for any errors and make sure that `psql` is installed in your PATH and has proper permissions.
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/postgresql_database_tasks.rb:108:in `run_cmd'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/postgresql_database_tasks.rb:80:in `structure_load'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:223:in `structure_load'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:236:in `load_schema'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:255:in `block in load_schema_current'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:304:in `block in each_current_configuration'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:303:in `each'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:303:in `each_current_configuration'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/tasks/database_tasks.rb:254:in `load_schema_current'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/activerecord-5.1.4/lib/active_record/railties/databases.rake:290:in `block (3 levels) in <top (required)>'
/Users/namangupta/.rvm/gems/ruby-2.3.4/gems/rake-12.3.0/exe/rake:27:in `<top (required)>'
/Users/namangupta/.rvm/gems/ruby-2.3.4/bin/ruby_executable_hooks:15:in `eval'
/Users/namangupta/.rvm/gems/ruby-2.3.4/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:structure:load
(See full trace by running task with --trace)
I want Rails to use the database and make a models within this. Any ideas?
The db:structure:load rake task creates a tables and columns from the structure.sql or schema.rb. You shouldn't need to run it, as your existing database already has all the tables you need.
Your next step would be to go about creating your model files and pointing them at the appropriate tables.

Issue regarding running rake db:seed task

I have an existing rails project and I am using postgres database and after successful execution of bundle exec rake db:migrate command when I am going to execute the bundle exec rake db:seed command then it throws me the following error.
rake aborted!
PG::DuplicateTable: ERROR: relation "schema_migrations" already exists
I have commented out the lines where the definition of the schema_migration table creation is stored such as in the development_structure.sql and test_structure.sql files. But it is still throwing me the error.
I don't know from where the schema_migration table is being created. There is no schema_migration.rb file in my project.
Pleas help me to fix this. Thanks in advance!
This is my error log.
rake aborted!
PG::DuplicateTable: ERROR: relation "schema_migrations" already exists
: CREATE TABLE "schema_migrations" ("version" character varying(255) NOT NULL)
/home/bit/.rvm/gems/ruby-2.1.5/gems/activerecord-2.3.18/lib/active_record/connection_adapters/abstract_adapter.rb:227:in `rescue in log'
/home/bit/.rvm/gems/ruby-2.1.5/gems/activerecord-2.3.18/lib/active_record/connection_adapters/abstract_adapter.rb:204:in `log'
/home/bit/Development/app2/application/lib/core_extensions/postgresql_adapter/reconnect.rb:26:in `execute'
/home/bit/.rvm/gems/ruby-2.1.5/gems/activerecord-2.3.18/lib/active_record/connection_adapters/abstract/schema_statements.rb:109:in `create_table'
/home/bit/.rvm/gems/ruby-2.1.5/gems/activerecord-2.3.18/lib/active_record/connection_adapters/abstract/schema_statements.rb:371:in `initialize_schema_migrations_table'
/home/bit/.rvm/gems/ruby-2.1.5/gems/activerecord-2.3.18/lib/active_record/migration.rb:441:in `initialize'
/home/bit/.rvm/gems/ruby-2.1.5/gems/rails-2.3.18/lib/tasks/databases.rake:193:in `new'
/home/bit/.rvm/gems/ruby-2.1.5/gems/rails-2.3.18/lib/tasks/databases.rake:193:in `block (2 levels) in <top (required)>'
/home/bit/.rvm/gems/ruby-2.1.5/bin/ruby_executable_hooks:15:in `eval'
/home/bit/.rvm/gems/ruby-2.1.5/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => db:seed => db:seed:common => db:seed:original => db:abort_if_pending_migrations
(See full trace by running task with --trace)
If it's OK to drop your data locally, try
rake db:drop db:create db:migrate
and then seed again. I've had similar issues in the past where I deleted a migration without properly rolling it back and this was the solution.
If you need to keep it, open the rails database (rails db from terminal) and try
drop table schema_migrations
(Run the migrations with db:migrate again after this)
Edit: This has not solved the issue yet, but it seems there may be other issues that need to be factored in. OP cannot launch rails console and receives
undefined local variable or method `vars' for #<Rails::Generator::Commands::Create:0x00000001ca2578>
as the error message each time - if anyone has ideas here, please add an answer!

rails can't migrate: "don't know how to build task VERSION:0"

First post on here so apologies for any noobity.
I'm working through the Lynda.com Ruby on Rails 5 Essential training on C9 and encountered a problem trying to migrate back down to 'VERSION:0' with this error message:
rails aborted!
Don't know how to build task 'VERSION:0' (see --tasks)
/usr/local/rvm/gems/ruby-2.3.1/gems/railties-5.1.2/lib/rails/commands/rake/rake_command.rb:21:in block in perform'
/usr/local/rvm/gems/ruby-2.3.1/gems/railties-5.1.2/lib/rails/commands/rake/rake_command.rb:18:inperform'
/usr/local/rvm/gems/ruby-2.3.1/gems/railties-5.1.2/lib/rails/command.rb:46:in invoke'
/usr/local/rvm/gems/ruby-2.3.1/gems/railties-5.1.2/lib/rails/commands.rb:16:in'
/home/ubuntu/workspace/bin/rails:9:in require'
/home/ubuntu/workspace/bin/rails:9:in'
/usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/client/rails.rb:28:in load'
/usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/client/rails.rb:28:incall'
/usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/client/command.rb:7:in call'
/usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/client.rb:30:inrun'
/usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.2/bin/spring:49:in <top (required)>'
/usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/binstub.rb:31:inload'
/usr/local/rvm/gems/ruby-2.3.1/gems/spring-2.0.2/lib/spring/binstub.rb:31:in <top (required)>'
/home/ubuntu/workspace/bin/spring:15:inrequire'
/home/ubuntu/workspace/bin/spring:15:in <top (required)>'
bin/rails:3:inload'
bin/rails:3:in `'
Any help or advice much appreciated
When doing migration in Rails, you can do:
rake db:migrate, which will migrate your new migration to database and add them to scheme.rb.
You can also do rake db:rollback, which will rollback your last migration.
For rollback you can also do rake db:rollback STEP=n, which will roll you back n migrations where n is the number of recent migrations you want to rollback.
IF you want to rollback version of your migration you would apply the migration version as: rake db:migrate VERSION=0 (as #sjudge said in comments)
You can also do rake db:migrate:redo.
There also some other migrations like:
rake db:migrate:redo STEP=n
rake db:migrate:up VERSION=20080906120000
rake db:migrate:down VERSION=20080906120000

Rails Apps - scaffold error

I'm having a rails app problem when executing the scaffold command in my command line. I used the rails generate scaffold User name:string email:string command that our teacher showed us today. His worked, but mine gets the following.
c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/dynamic_matchers.rb:55:in `method_missing': undefined method `migration_error=' for ActiveRecord::Base:Class (NoMethodError)
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/railtie.rb:66:in `block (3 levels) in <class:Railtie>'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/railtie.rb:65:in `each'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.13/lib/active_record/railtie.rb:65:in `block (2 levels) in <class:Railtie>'
from c:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.13/lib/active_support/lazy_load_hooks.rb:36:in `instance_eval'
There are more lines, but I would hope that you get the idea. If I need to post the rest, I will. Beyond that, if there is another question that has the answer to this, please point me in that direction. Thank you in advance.
Try set up your database first by running this command
rake db:drop db:create db:migrate
or
rake db:reset

Rake db:migration aborted

I've got a unfinished project that a developer just didn't finish and didn't leave any documentation about the installation process. I've downloaded the production directory to my windows machine (running InstantRails 2), I created the databases as required in the database.yml and I tried to run the rake:db:migrate --trace but I'm receiving the following error message:
(in D:/projects/broke2)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
rake aborted!
uninitialized constant Admin
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/dependencies.rb:279:in `load_missing_constant'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/dependencies.rb:468:in `const_missing'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/dependencies.rb:480:in `const_missing'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/inflector.rb:285:in `constantize'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/inflector.rb:284:in `each'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/inflector.rb:284:in `constantize'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activesupport-2.1.1/lib/active_support/core_ext/string/inflections.rb:143:in `constantize'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/migration.rb:481:in `migrations'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/connection_adapters/mysql_adapter.rb:15:in `inject'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/migration.rb:465:in `each'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/migration.rb:465:in `inject'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/migration.rb:465:in `migrations'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/migration.rb:431:in `migrate'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/migration.rb:373:in `up'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/activerecord-2.1.1/lib/active_record/migration.rb:356:in `migrate'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rails-2.1.1/lib/tasks/databases.rake:99
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:617:in `call'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:617:in `execute'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:612:in `each'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:612:in `execute'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:578:in `invoke_with_call_chain'
D:/InstantRails-2.0-win/ruby/lib/ruby/1.8/monitor.rb:242:in `synchronize'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:571:in `invoke_with_call_chain'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:564:in `invoke'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:2019:in `invoke_task'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1997:in `top_level'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1997:in `each'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1997:in `top_level'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:2036:in `standard_exception_handling'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1991:in `top_level'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1970:in `run'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:2036:in `standard_exception_handling'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/lib/rake.rb:1967:in `run'
D:/InstantRails-2.0-win/ruby/lib/ruby/gems/1.8/gems/rake-0.8.3/bin/rake:31
D:/InstantRails-2.0-win/ruby/bin/rake:19:in `load'
D:/InstantRails-2.0-win/ruby/bin/rake:19
I'm a regular Rails developer (it's not my first app) but I never saw this error and I don't have a clue where to start to debug.
Sometimes Rails will throw this error if there's a syntax error where Admin is defined.
Try looking for admin.rb and make sure that it parses.
Also, you may want to try running the migrations one at a time (rake db:migrate VERSION=1, etc.) to see if that helps you track down which migration causes the error, or if it is a problem simply booting the application.
I suspect you had a migration that created a table and added some data using a model. Later in the project the model got renamed or removed (as did the table maybe?). As the model no longer existed the migrations failed to run, but no one noticed as by that point they were only running a few migrations at a time, not from a clean database.
The lesson here ... if you rename models or tables (or update their validations and fields) then check your migrations run from scratch as well on the current production version.
I'd say your problem is in the uninitialized constant Admin part of your migration issue. Have you tried finding where Admin is initialized and including the path to that wherever you're using it? (Also, what's the contents of the rake task you're running?)
It don't points me where i have a Admin constant. There is a way to check at least where should I look? my models, my controllers, etc?
rake db:migrate VERSION=1, gives me a error already. As I told you, am I just trying to finish a project unfinished by another guy.
How can i do a migration file per file?
Regards,
Victor
You can try loading the schema all at once instead of executing each migration:
rake db:schema:load
As RichH said there might have been a change to the schema not reflected in the migrations

Resources