I deleted a migration file I didnt need anymore (deleted, not rollback). Then tried to run migrations and my rake tasks stopped doing anything altogether. What ended up fixing my problem (or so I thought) was adding another migration which ended up working for running rake db:migrate for the latest migration. Then after the latest schema was set my rake tasks stopped working again altogether.
I am using rails 5. I tried using the new rails command for db tasks, but that didnt work.
I know I'm not showing code here, but its kind of difficult for this problem. I included a screenshot of what happens when I run tasks for my app. Youll see various ruby warnings because I should upgrade my version, but those are not related.
Has anyone faced this problem before or have any ideas on how to solve it?
I have had this before, We don't have enough information to give a perfect answer, so I have given you a few suggestions that have worked for me.
before you try anything drastic, run rails db:reset.
Hopefully that works, if not hopefully you are using git or svn. Go back to when it worked, then don't delete the migration file. Create a new migration that drops the table you don't need anymore.
to do this run 'rails g migration drop_TABLE_NAME' that will make a new migration file, you can drop a table like this below.
def change
drop_table :TABLENAME
end
If you aren't using git, I would try editing db/schema.rb.
I had my database in my db.yml set to default. I had to declare actual database names and then run migrations in order to get it to work.
For me my db.yml now looks like this:
default: &default
adapter: postgresql
template: template0
database: job-board-development
pool: 5
timeout: 5000
While previously it looked like this:
default: &default
adapter: postgresql
template: template0
pool: 5
timeout: 5000
Related
I have a Rails app which uses db my_database_development in my config/database.yml:
development:
<<: *default
database: my_database_development
Works correctly when I run rails server.
Now I want to use another db, so I change my config/database.yml:
development:
<<: *default
database: my_prev_database
Now when I run rails server, they give me an ActiveRecord::PendingMigrationError. To resolve this issue, run: bin/rails db:migrate RAILS_ENV=development. When I run that command, my_prev_database gets cleared. I don't want that to happen. I want to use my_prev_database and all the data it has (which I backed up from somewhere)
How can I switch database in Rails effectively?
Thank you!
When you switch database, you will have new schema_migrations table. In new database, schema_migrations is empty so Rails will think you have pending_migration. I think you need to re-migrate in your new database. You can use some feature like database dump to migrate date from old database to new database
I am now unable to replicate the problem above. (Not sure why, maybe I was copying over my database incorrectly.)
Also, a possible resolution was to copy tables over individually from my_prev_database to my_database_development
Note for anyone troubleshooting similar problems:
The commenters mentioned that,
- Running rails db:migrate shouldn't delete database data
I recently set up postgresql in a new workspace. I set it up the same way I usually do. Or at least I thought I did. But then I got this error
Failure/Error: ActiveRecord::Migration.maintain_test_schema!
ActiveRecord::NoDatabaseError:
FATAL: database "app_test" does not exist
It only happens when I change to a branch and run rspec. So far I've just been creating the database and running migrations each time I switch between branches as a solution.
It doesn't impact the development database. And as long as I remain on that branch I don't have to add the test database again. But if I have to leave it for any reason, I'll have to add the database again.
It seems to impact any branch that isn't the master. And this only occurs locally. I am using Cloud 9 and my app is a Rails app. Should I uninstall and reinstall postgresql, then set it up again?
Recently I've had to work on a few branches at the same time, so it's becoming a hassle. I'm tempted to just make a new workspace.
here is my database.yml(with some things hidden of course, app is a stand in for my app's name).
default: &default
adapter: postgresql
encoding: unicode
host: localhost
username: postgres
password: secret
pool: 5
development:
<<: *default
database: app_development
test:
<<: *default
database: app_test
production:
<<: *default
url: <%= ENV['DATABASE_URL'] %>
Thank you in advance
You should be able to reset your databases by running:
rake db:drop db:create db:migrate db:test:prepare
That'll delete your dev database (along with any data in it), recreate it, rebuild the schema from your migration files, and rebuild the test database. If it doesn't work at that point you may be a permissions issue
I faced with similar issue.
Just try to change this line of database.yml
host: 127.0.0.1
when i provided localhost it used default settings for connection. (i have many DB server version on different ports)
First I deleted my local branches to start clean (it may not be necessary but as most of them altered the schema I thought it'd be cleaner). My changes where all stored remotely so this was wasn't a big deal.
Then I added template: template0 to my database.yml in the development and test sections. I followed this suggestion Fix for PG::Error: ERROR: new encoding (UTF8) is incompatible
because I had this error:
"PG::InvalidParameterValue: ERROR: new encoding (UTF8) is incompatible with the encoding of the template database (SQL_ASCII)
HINT: Use the same encoding as in the template database, or use template0 as template."
whenever I would try to use rake db:create
After that I followed NM Pennypacker's guidance and did, rake db:drop, rake db:create, rake db:migrate and rake db:test:prepare.
I've made some branches, pulled in the remote changes and run rspec with no problem. Thank you everyone for your advice!
I have created a new rails project and I have added a migration to create a table called blogs. When I ran rake db:migrate it created this table along with other tables I have set in different projects.
Under db/migrate I have only 1 file with the migration I added, there's no reference in the project of the other tables, for some reason it is pulling the migrations from other projects and adding it to my DB project
Rails version:
5.1.4
Ruby version:
2.4.1
Probably you're using the same database. You need to specify different database in the config\database.yml file for each project. You can have same username, same password, etc. but At least the database: (database name) have to be different if you don't want all your tables in one database
You need to configure your database.yml for each project.
And create 3 databases for development, test and production.
And specify all the information needed to access your database.
Eg:
development:
adapter: postgresql
username: postgres
password: blogs_postgres
database: blogs_development
encoding: utf8
host: localhost
pool: 5
timeout: 5000
Refer Here for more info.
I just solved my issue, seems like Spring was mixing things up in my environment.
Reference: https://github.com/rails/rails/issues/31529#issuecomment-353269787
So I started working on a Rails app recently and we decided (well not me, the person working on it with me) that we should switch from Sqlite3 to Postgresql. I've installed Postgresql on our server properly, created the databases for dev, prod, and test, and updated my Gemfile and database.yml files with the code for Postgres. The thing I'm unsure of now, is how to switch out all the files in the db directory with the Postgres databases. Do I just delete the contents of the db directory in my app and run rake db:create?
You'll want to edit config/database.yml to use postgresql instead of sqlite.
The migrations in db/migrate/*.rb are hopefully cross-database compatible, and wont need to be changed.
Running rake db:create db:migrate with the new database.yml should create the PostgreSQL database and you'll be up and running.
In reality, you'll probably run into various problems, but this will be a starting point.
From what I can gather, Heroku is supposed to generate a database.yml file automatically, and ignore the local one. However, I am seeing an error where that is not true, and my changes to the local database.yml are affecting the Heroku app. This is problematic because I have no idea how I should setup production portion of the file so Heroku can find the right database.
For instance with the following
production:
adapter: sqlite3
database: db/production.sqlite3
pool: 5
timeout: 5000
followed by the db:migration
$:~/Apps/DancingCupid/DancingCupid$ heroku rake --trace db:migrate
spits out
rake aborted!
unable to open database file
/app/.bundle/gems/ruby/1.8/gems/activerecord-3.0.5/lib/active_record/connection_adapters/sqlite3_adapter.rb:27:in `initialize'
...
I can get different errors depending on what type of database I sent for production.
Besides deleting the app and making a new one, is there a way to fix this problem?
Heroku definitely rewrite your database.yml on push so it doesn't matter what is in there in source control.
To confirm this do heroku run bash which will connect you to a bash session in your app then look do a cat config\database.yml and you will see how they have rewritten it.
The other answers are NOT TRUE anymore as of Rails 4.1.
config/database.yml won’t be overwritten anymore when a Rails 4.1.0
RC1 app is detected. Instead, it will be merged with DATABASE_URL so
additional options like pool size can be set in config/database.yml.
To double-check the contents of your database.yml on the Heroku server, you can run remote bash via heroku run bash and then cat config/database.yml to see its contents on the server and compare with your local one.
I don't think you are insane! ( But I thought I was )
I have been beating my way around this problem for a few days, and finally found this article:
http://article.gmane.org/gmane.comp.lang.ruby.rails.heroku/1003/match=database+yml
It led me to believe that maybe it wasn't my code at all!
I then simply destroyed my heroku app and created a new one, and pushed to it. Suddenly everything works fine! I don't know how or when or why, but I think it is possible to overwrite or corrupt the database.yml file that Heroku creates.
Hope this helps!
Try removing database.yml from version control. It's good practice to make a copy of database.yml into something like database.yml.example and adding database.yml to your .gitignore file.
That way when you push to Heroku it won't have any database configuration to refer to.
You probably also don't want the sqlite3 gem in production. Make sure it's in the development/test groups in your Gemfile.
database.yml should not be pushed to Heroku. It will try to connect to that database, timeout and then crash.
Add it to your .gitignore so it doesn't get up there.