I am a completely new at this; I used the psql console to create tables for the db, but I think I'm doing it wrong since when I run rake db:migrate, everything gets wiped. So my question is how do I get it to stop dropping all my tables upon a reset without typing all my tables again? I think I'm supposed to use models, is this correct?
EDIT - So I have wasted all my time making tables using the psql console?
You need to create tables "the Rails way" with migrations. You should read the link provided, but essentially you create migrations to track the changes in your database schema over time. You have migrations for creating tables, adding new columns, changing column definitions, etc. You can generate a migration to create a table from the command line like so:
rails generate model Fruit title:string amount:integer
rake db:migrate
This will not only create the table fruits with the specified name, it will also create a model Fruit with which you can query the table. You're also free to edit the migration created before running rake db:migrate.
You should create migration files to build your database. That is what you are doing when you run rake db:migrate: you are taking your migration files and applying them to your database. Look at this guide for more information about Rails migrations.
By running $ rake -T you can check every rake task. Everything is being wiped because other tasks are run along with certain rake tasks. Try
$ rake db:drop
$ rake db:create
$ rake db:migrate
Your database should be created properly, as long as you gave database creation permissions to your Postgres role.
Related
I have recently in my project needed to erase my databases and using rails g migrate DropTable. This has worked in making me deleting the schema, but now when I run rails db:migrate the schema does not repopulate with the db/migrate information. I am kind of stuck here. How should I go about filling up my schema.
Try to the following rake or rails, make sure your file exists under the db/migrate then take TIMESTAMP number from file name then run like below
rake db:migrate:up VERSION=20171212082249 #=> 20171212082249 that mean TIMESTAMP number from file name under the db/migrate
or
rake db:migrate:redo VERSION=20171212082249 # that will run the down and then the up step
You can see this article for more explanation.
Additional
rake db:rollback STEP=1
This will roll back lost one migration, if you need to rollback specific one from many then try this
rake db:migrate:down VERSION=TIMESTAMP #=> TIMESTAMP mean 20171212082249 this number from your migrated file name
I initially ran a migration when I created the database and everything worked fine. Then I went and deleted the table in postgresql manually. Now when I run rake db:migrate, it runs but doesn't create the table.
It depends on what your migrations do. rake db:migrate can create or change tables, depending on your definitions. To setup the database and the tables in the beginning you can use rake db:schema:load or rake db:setup. rake -T gives an overview over all available rake tasks.
Database migrations are stored in the table schema_migrations which has one column version. As long as the version of the migration in question can be found here, the migration is not executed again.
I'm currently following the ruby on rails tutorial: http://guides.rubyonrails.org/getting_started.html.
I am trying to save data into the database. However, when I run: rails server I get the following error:
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
I've looked at the other articles and when I run:
bin/rake db:migrate
I get a rake aborted!
After running:
rake db:abort_if_pending_migrations....
I see that:
You have 1 pending migration:
20150805200129 CreateDatabases
SQLite3::SQLException: table "databases" already exists:
and it just tells me to run rake db:migrate to start again.
It seems that it already exists. Is there a way to cancel the pending migration?
Sometimes, even dropping a local development database is not a good idea.
There are better ways to delete/destroy a specific migration in your Rails application.
You could use rails d migration command to destroy a particular migration:
rails d migration MigrationName
To undo the changes corresponding to a particular migration, you can use db:migrate:down method like this:
rake db:migrate:down VERSION=XXX
Sometimes, things could get more messy and in those situation another handy thing is to take a look at the schema_migrations table in your database which has all the migrations with their version saved in it.
You can delete a particular migration from this table like this:
delete from schema_migrations WHERE version = VERSION;
if you don't want that migration to be present anymore.
Your migration may have failed midway (so it created the table, but didn't finish).
You are just using development environment, so it's okay to just drop the database and rebuild it from scratch:
rake db:drop # THIS WILL DELETE YOUR DATABASE
rake db:create
rake db:migrate
If you are like me and maintain your database structure outside of Rails, you can just delete the migration file from db/migration. I got the error in the OP's question when I used the rails generate command to create a model class, forgetting that it also creates a migration file.
Do not use this method if you rely on Rails to maintain your database structure!
I keep my Rails structure file up to date by building it from the database using:
bundle exec rake db:structure:dump
I do not encourage to drop the database and start from the beginning especially when you already have the data inside the database.
My approach to this will be migrate first, then rollback. After that you can safely delete the migration file. So the procedure is as following.
rails db:migrate
rails db rollback
rm db/migrate/your_last_migration_file.rb
You can recreate database and run all migrations in your development environment with such command
rails db:migrate:reset
If you want to revert the wrong migrations, You can drop the whole db using this:
rake db:drop
Then remove the migrations file manually(This wont corrupt the db when you recreate as the Schema migrations would be dropped as well).
Then run
rake db:migrate
And if there is data to be seeded, then run this as well
rake db:setup
How do I undo bundle exec rake db:setup?
I ran it in the wrong rails app. I ran it in the blogger when I should have ran it in the blogger_advanced app.
You can do rake db:drop
.
It will drop all tables (thats any tables created by setup, any migrations run by setup and any seeds created by setup)
You may use rake db:rollback and it'll rollback migrations one by one, but if yours apps have same named models (User for example) it'll be lost. So you need to dump this tables first and recreate them later using database administrator tool.
And also it'll broke your schema_migrations table and you'll need to refill it by hands from your migration file names.
So if there is not much useful info, it's better to use #Alexphys approach.
Hello I am new to rails.
I am working with an existing postgres db. I would like to create CRUD for all of the tables.
I've started with:
rails g scaffold firstTableName
Then I start rails:
rails s
When I navigate to localhost:3000/firstTableName, rails gives the following error:
Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue.
I thought that 'rake db:migrate' was only for setting up your database. My db is already set up. If I run 'rake db:migrate', then I get an error that firstTableName already exists.
Please Help me understand how to make rails happy.
Thank you.
the error is because you already have the table firstTableName is already present and as you did scaffold, it will create the model, controller and view for you, so it has also create the migration for the firstTableName.
If you already have the migration in place try skipping the migration while scaffolding
rails g scaffold firstTableName --skip-migration
Your database should have a table named schema_migrations with only one column version. Then you should write all migration versions into that table by hands.
Migrations placed in project_root/db/migrate/, and the вшпшеы in the file name is the same as version migration.
When you run migrations rails keeps track of these by matching each migration in db/migrate folder with an entry in schema_migrations table that includes timestamp for all the migrations that rails has already run for that environment in versions column.
Reason is to allow you to migrate incrementally.
Earlier versions of rails had a strong philosophy of having a down migration as well to go back in time. However, that has been left as a choice now.
Your complaint is because it was not able to create schema_migrations table. My suggestion is to allow it to do so by running rake db:migrate on a fresh db.
If you have data in the tables on this instance run it on another database and then just copy across the versions table.
http://guides.rubyonrails.org/migrations.html