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
Related
I want to add a secondary database to my RoR project, so I followed this: https://guides.rubyonrails.org/active_record_multiple_databases.html
So now I have two databases: jobtitleio & profiles
My issue is that when I run: rails db:migrate:profiles, the migration is applied to the other database (the primary one). => in this example, I want to add a table, and the table is added to the jobtitleio database.
Here is my database.yml:
development:
jobtitleio:
<<: *default
database: jobtitleio_development
profiles:
<<: *default
database: profiles_development
migrations_paths: db/profiles/migrate
I ran this to create the migration:
rails g migration CreateUsers url:text external_id:integer --database profiles
which created the migration file in the right folder (db/profiles/migrate).
So this is where I'm at, my migration is created in the right folder, and is ran to be applied to the profiles database (using rails db:migrate:profiles). So why is it applied to the other database ?
Thanks for your help !
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 used to develop on SQLite3 and deploy on PostgreSQL. However, after this question I realised the importance of having the same database for development and production.
After many problems, I finally managed to install the PostgreSQL on my Mac OS X Lion 10.7.4. However, I am still facing some problems:
Using SQLite3 the databases' path is defined on the database.yml, which rails automatically creates within each new project.
Using PostgreSQL the database name is defined on the database.yml, which rails doesn't create automatically.
My question:
Shall I create a new database for each new project or shall I change the database.yml to include the same database for all projects?
I saw people saying that each user in a computer should have one database, when using PostgreSQL.
Well, if the best practice is to create one database for each project (which I hope so)...
There is any way to configure rails to create the new database automatically within each new project?
Creating a DB is mostly one-time operation, so it's not a big deal to create it once for a project. A casual Postgresql admin can use pgAdmin graphical tool.
If you use Postgresql for development, you need to create database by your self.
But you can initialize you project for Postgresql.
rails new my_project -d postgresql
Have a good day.
Benjamin.
Problem solved:
I have followed this tutorial and I could finally solve the problem.
I have created a new user for my postgreSQL called user1 without any password, so each time I create a new project I just need to change the database.yml for the below:
development:
adapter: postgresql
encoding: unicode
database: newproject_development
pool: 5
username: user1 #I am changing this line
password:
test:
adapter: postgresql
encoding: unicode
database: newproject_test
pool: 5
username: user1 #I am changing this line
password:
production:
adapter: postgresql
encoding: unicode
database: newproject_production
pool: 5
username: user1 #I am changing this line
password:
As the the user1 doen't have any password, now I can just create the tables using the Rails rake.
rake db:create:all
OK, I'm building my first rails 3.0 app and want to test the postgreSQL server as production on my development machine (which is running 10.6). When you create a new app and rake db:migrate it creates sqlite db's for all three environments. Cool. Now I want to learn how to move to production and use postgres. I've used homebrew to install postgres, installed the pg (env ARCHFLAGS="-arch x86_64" gem install pg) and postgres-pr gems.
I've run rake db:migrate in hope that like with sqlite3 it will auto build my production server since I've updated my database.yml (see below).
OK, in my app's folder, I restart the server using 'rails s --environment=production' and it bails saying it cannot find my production database.
So all the google searches for 'rails 3 postgres install' got me this far, but I appear to be missing something because rails is failing to create the new pg database.
postgres is running as determined by ps.
createdb -Omysuperusername -Eutf8 vitae_production
createdb -Omysuperusername -Eutf8 /Users/sam/apps/vitae/db/vitae_production
But this directory does not have this database so I'm missing something. What am I overlooking?
this is my database.yml snippet:
production:
adapter: postgresql
host: localhost
database: db/vitae_production
pool: 5
timeout: 5000
username: mysuperusername
password:
There are a couple things going on here. First of all, you seem to be mixing the SQLite and PostgreSQL format for the database: setting in your database.yml. With SQLite, you specify the relative path to the SQLite database file with something like:
database: db/vitae_production.sqlite
but with PostgreSQL, you specify the database name with something like this:
development:
database: vitae_development
username: rails
...
Then, once database.yml is setup, you'd create the database user (if needed) from inside psql:
psql> create role rails login;
and then let Rails create the database:
$ rake db:create
Then you should be able to run your migrations to create your initial tables and away you go.
You probably want to read the create role documentation to make sure you get the right options. You probably want to be working in the development environment rather than production as well so I changed the names and YAML to reflect that, production is for deployment and I don't think you're deploying anything just yet.
Im'not familiar with rails but you could create your database as a postgres super user and then grant privileges, assuming that in your linux distribution the postgres super user is postgres:
su root
su postgres
createdb vitae_production
then in postgres grant privileges to another user
psql
CREATE USER rails WITH PASSWORD 'myPassword';
GRANT ALL PRIVILEGES ON DATABASE vitae_production TO rails;
ALTER DATABASE vitae_production OWNER TO rails;
then your config file should look like this:
production:
adapter: postgresql
host: localhost
database: vitae_production
pool: 5
timeout: 5000
username: rails
password: myPassword