Want to re-create table when running rake db:seed - ruby-on-rails

I want to remove all tables and re-created the db when I run
rake db:create
The reason is the id's on the tables are not set to 1, so I figured i can just re-create the db if its possible, or call truncate.

rake db:reset
may be what you are looking for. or...
rake db:drop
rake db:create

Try rake db:reset (in Rails 3)
In Rails 2, maybe this will help: http://pivotallabs.com/users/alex/blog/articles/305-collapsing-migrations

Related

Is there a way to run rake db:setup on a single table?

I have a Ruby on rails app, and I modified db/seeds.rb file and I want to push my changes into my database.
is there an option in Rails where I can run rake db:setup and modify only that table without clearing all the other ones ?
Thanks
You use rake db:seed to run the seed file. rake db:setup is much more involved, it basically runs all of the following
db:create
db:schema:load
db:seed

Rails Project automatically deleted after rake db:reset

I was working on Nitrous.io on a Rails Project.
I have created a new branch and then after some coding , I reset the database with
rake db:reset.
After this action my Projects folders were automatically deleted.
Someone could help me understand what is wrong on my command?
db:reset will do exactly as described - it will "reset" the database. So db:reset is the same as executing the following commands: db:drop (which will drop/delete all entries in the database) + db:setup
I somehow doubt it that rake db:reset deletes your .rb files. Must be something else. Maybe you messed up with git?
rake db:reset
rake db:create
rake db:migrate
works just fine, normally.

Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue [unable to proceed]

I appear to have a circular issue in regards to Ruby on Rails migration procedure. I am following the introduction article and I have reached the point when I need to create my first table.
I have ran the following,
[tims#web2 working_ror]# rails generate model Homepage first_name:string last_name:string email:string message:text
invoke active_record
create db/migrate/20131119203948_create_homepages.rb
create app/models/homepage.rb
invoke test_unit
createtest /models/homepage_test.rb
createtest /fixtures/homepages.yml
I then proceeded with the migration,
[tims#web2 working_ror]# rake db:migrate
== CreateHomepages: migrating ================================================
-- create_table(:homepages)
-> 0.0493s
== CreateHomepages: migrated (0.0494s) =======================================
, however, when I run my application I see the following message,
Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue.
but, IF I run the above,
[tims#web2 working_ror]# rake db:migrate RAILS_ENV=development
[tims#web2 working_ror]#
and the message continues ...
I have spent considerable amount of time researching forums in-which the closest I could find was to drop and re-build everything, which have done the following.
rake db:drop
rake db:create
rake db:migrate
and the results are the same.
You need to do
bundle exec rake test:prepare
or
bundle exec rake db:test:prepare
and then
bundle exec rake db:migrate
before running the specs
Cheers
cited from : Why am I asked to run 'rake db:migrate RAILS_ENV=test'?
you can do
bundle exec rake test:prepare
In Rails 4.1+, they deprecated db:test:prepare
You can now just use:
ActiveRecord::Migration.maintain_test_schema!
If you need to do it manually
rake db:schema:load RAILS_ENV=test
and then
bundle exec rake db:migrate
try
In RAILS_ROOT/config/environments/development.rb Set the following setting to false:
config.active_record.migration_error = false#:page_load
One weird trick that you can use when your migrations are screwed (file deleted, manually renamed, etc.)
Fire up your favourite DB admin tool (eg. PGAdmin3) and browse to the database in question.
Look for a table called schema_migrations and browse its content. It should have a single column called version. This field is used by Rails to check whether migrations are up to date.
Make sure that your migration timestamps corresponds with the data in this column. If you have deleted an older migration, delete the corresponding timestamp.
Check to make sure that table doesn't already exist:
type - rails dbconsole
type - .tables (check to see if there was an error during the rake db:migrate that has the table name like -- create_table(:test) rake aborted!)
If you see the table name after running the .tables in the console type - drop table TABLENAME;
Then .quit to go back to the branch and run the rake db:migrate command again.
this was what i did:
rails db:environment:set RAILS_ENV=test
If you need to do it manually
rake db:schema:load RAILS_ENV=test
and then
bundle exec rake db:migrate
Thanks to Ahmed Ali....... your comment was helpful.

How to Rails rake multiple commands with one environment?

Is there a way to do multiple Rails 3 rake commands on one line, requiring the environment to be initiated only once?
I know this is possible:
rake db:rollback db:migrate
But if options are passed,
rake db:migrate VERSION=0 db:migrate
the second 'db:migrate' won't run.
I don't think that is possible.
The quickest solution I can think of is:
RAILS_ENV=test rake db:migrate VERSION=0 && rake db:migrate
The reason why I believe this is not possible is because VERSION is simply a constant, not an attribute that is passed as a db:migrate option. For example, all of these commands work:
rake db:migrate VERSION=0
rake VERSION=0 db:migrate
VERSION=0 rake db:migrate
And since you can't rewrite the constant in the same action again, you basically call db:migrate VERSION=0 twice.
Why not try Zeus?
It preloads environments for rails, rake, test environments, etc.

Under what circumstances would this happen in rails (db)?

Utterly confused at this mess:
rake db:drop
>
rake db:create
> my_database already exists
rake db:migrate
> unknown database my_database
Appreciate any insight.
That's because first time when u do rake db:create it creates two databases one is development and other is test. then when u do rake db:drop it drops the database, but it only drops the development database not the test database. so try removing the test database explicitly and everything should be fine.
Try :
rake db:drop
rake db:create RAILS_ENV=development
rake db:migrate RAILS_ENV=development
if getting same error then open mysql terminal and create database manually :
CREATE DATABASE database_name;
then run
rake db:migrate

Resources