Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do rails db:migrate on multiple shards that are not master slave relationship at once on rails?

I have an app which which uses different database based on the subdomain. So essentially, the schema would be the same, but the data would differ for each databases. But when I release some new features and it would require some schema changes, I would need to run a command that would run on all databases configured in the shards.yml.

database.yml

default: &default
  adapter: postgresql
  encoding: unicode
  pool: 15
  host: localhost
  port: 5432
  username: postgres
  password:

development:
  <<: *default
  database: app_default
production:
  <<: *default
  database: app_default
  username: <%= ENV['BACKEND_DATABASE_USERNAME'] %>
  password: <%= ENV['BACKEND_DATABASE_PASSWORD'] %>

shards.yml

shared: &shared
  adapter: postgresql
  encoding: unicode
  pool: 15
  host: localhost
  username: postgres
  password: 
  port: 5432

octopus:
  environments:
    - development
    - test
    - production
  development:
    default:
      <<: *shared
      database: app
    first:
      <<: *shared
      database: first
    second:
      <<: *shared
      database: second
    ....
  test:
    test:
      host: postgres
      adapter: postgresql
      database: app_test
  production:
    default:
      <<: *shared
      database: app
    first:
      <<: *shared
      database: first
    second:
      <<: *shared
      database: second
    ....

I am using Octopus to set the shard based on subdomain, which works fine. The problems I have are:

  1. I cannot do rails db:reset . Getting the error ActiveRecord::StatementInvalid: PG::ObjectInUse: ERROR: cannot drop the currently open database
  2. I cannot do rails db:migrate that would migrate on all databases
like image 754
Suthan Bala Avatar asked Sep 14 '17 13:09

Suthan Bala


People also ask

How do I migrate a specific migration in Rails?

To run a specific migration up or down, use db:migrate:up or db:migrate:down . The version number in the above commands is the numeric prefix in the migration's filename. For example, to migrate to the migration 20160515085959_add_name_to_users. rb , you would use 20160515085959 as the version number.

How does Rails migration work internally?

Internally Rails only uses the migration's number (the timestamp) to identify them. Prior to Rails 2.1 the migration number started at 1 and was incremented each time a migration was generated. With multiple developers it was easy for these to clash requiring you to rollback migrations and renumber them.

What is reversible migration in Rails?

Reversible Migrations Rails allows us to rollback changes to the database with the following command. rails db:rollback. This command reverts the last migration that was run on the database. If the migration added a column event_type then the rollback will remove that column.

What is sharding in Rails?

Rails provides shards as a way to connect with multiple databases, much more complex than replicating. Each shard can either be a vertical or a horizontal splice of the entire application database. With vertical sharding, one can read and write to more tables than available in the primary one.


1 Answers

You have to add using to your migrations

class CreateComments < ActiveRecord::Migration
  using :first, :second

  def change
    create_table :comments do |t|
      t.belongs_to :page, index: true
      t.text :body

      t.timestamps
    end
  end
end

the above migration will run on both first and second

like image 188
Tony Vincent Avatar answered Oct 04 '22 00:10

Tony Vincent