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 !
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 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
I have a rails app setup on a google cloud instance. I want to have the db in a SQL instance for the extra performance. But I cant see how to do this for a rails app.
I understand you create the SQL instance, start it, install mysql, on it but then how can I have the db and tables added? Creating them manually isn't going to be the solution because normally with rails apps you run rake db:create and rake db:migrate create the DB with tables and columns but this just makes a development.sqlite3 file not a mysql db..
I haven't deployed a rails app before so I think I'm missing something.
Here is my config/databast.yml file
default: &default
adapter: sqlite3
pool: 5
timeout: 5000
development:
<<: *default
database: db/development.sqlite3
mysql_settings: &mysql_settings
adapter: mysql2
encoding: utf8
pool: 5
username: root
password: root
host: 130.211.71.150
database: dbname
test:
<<: *default
database: db/test.sqlite3
production:
<<: *default
database: db/production.sqlite3
I cant find out what needs to be done to have the db be created and tables and columns migrated into the mysql DB.
In your gem file you can put the sqlite gem under a development/test block and you can add the mysql gem in a production block.
In your database.yml file you can keep the development settings you have currently but then add another setting for production settings. Here you can include your mysql db settings (including the host and port of your SQL instance node)
When you launch your app, you can then launch it locally in development mode to use sqlite for development, but when deploying you can launch in production mode to utilize the mysql specific settings. From there you should be able to use db:create db:migrate etc to connect to that host and setup your Db.
Here is a nice article describes this process.
https://www.digitalocean.com/community/tutorials/scaling-ruby-on-rails-setting-up-a-dedicated-mysql-server-part-2
As a team, we chose to use mysql for local development as it more closely mimics what your prod environment will be like.
So after having a problem deploying to Heroku for the first time, I wanted to get my dev environment running Postgres..
That's when i couldn't get my dev environment working. My errors lie in the database. I'm running Rails 3.2, Postgres.app, I got a database setup, I think database.yml is good to go.
As far as I can tell, my schema.rb is blank. It originally was not. When i delete it and reset the database, the new schema.rb is still blank. Where does schema get it's information from?
Rake db:migrate returns this SELECT "schema_migrations"."version" FROM "schema_migrations"
The rails s error complains about the lack of a users table.. doh.
I admittedly edited the schema.rb early on when i was first starting. But I don't know what caused this sort of reset.
database.yml
development:
adapter: postgresql
database: phriends
encoding: utf8
username: mrbubbles
password:
test:
adapter: postgresql
database: test
encoding: utf8
username: mrbubbles
password:
production:
adapter: postgresql
database: production_database
encoding: utf8
username: mrbubbles
password:
Sounds like you didn't rake db:setup, db:setup will
Create the database, load the schema, and initialize with the seed data (use db:reset to also drop the db first)
Note the "load the schema" part, that would read schema.rb and build the tables based on what it says. So restore your schema.rb and do a db:setup.
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