Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Postgres Multiple Schema Database in Rails

I'm developing a multitenant app following this article. The problem is when I run all migrations the first time. In schema.rb file are only the tables for the public schema but what happend with the others schemas? and how can I create others schemas with different structure to the public I don't want to use gems.

See the example below

Table to be created for public schema

class CreatePerspectives < ActiveRecord::Migration
  include MultiSchema
  def up
      with_in_schemas :only => :public do
         # Create table perspectives
      end
  end


  def down
    with_in_schemas :only => :public do
      drop_table :prespectives
    end
  end
end

Table to be created for private schemas

class CreateObjectives < ActiveRecord::Migration

  include MultiSchema

  def change
    with_in_schemas :except => :public do
        # Create objectives table
    end
  end
end

schema.rb

ActiveRecord::Schema.define(version: 20130810172443) do

  create_table "perspectives", force: true do |t|
    t.string   "name"
    t.datetime "created_at"
    t.datetime "updated_at"
  end
end
like image 413
Cristhian Boujon Avatar asked Aug 12 '13 12:08

Cristhian Boujon


1 Answers

do you know Apartment gem? https://github.com/influitive/apartment

I used in a project on this year, that one supports multi schema with postgresql. Everything is documented and easy to use. And you can chose the Middleware mode. For example, when you access the domain customer.applicationdomain.com the application can select the right schema for you. By the way you can use background jobs with sidekiq too.

like image 118
Ivan Santos Avatar answered Sep 18 '22 05:09

Ivan Santos