Home

Awesome

Gem Version Build Status Coverage Status

SchemaPlusMultischema

SchemaPlusMultischema adds support for using multiple schemas with ActiveRecord. Note that the gem only adds functionality if the database schema search path is not DBMS default (schema_search_path='$user,public').

Then following features are supported:

Support is currently limited to Postgresql. See details below.

SchemaPlusMultischema is part of the SchemaPlus family of Ruby on Rails ActiveRecord extension gems.

Installation

<!-- SCHEMA_DEV: TEMPLATE INSTALLATION - begin --> <!-- These lines are auto-inserted from a schema_dev template -->

As usual:

gem "schema_plus_multischema"                # in a Gemfile
gem.add_dependency "schema_plus_multischema" # in a .gemspec
<!-- SCHEMA_DEV: TEMPLATE INSTALLATION - end -->

Compatibility

SchemaPlusMultischema is tested on:

<!-- SCHEMA_DEV: MATRIX - begin --> <!-- These lines are auto-generated by schema_dev based on schema_dev.yml --> <!-- SCHEMA_DEV: MATRIX - end -->

SchemaPlusMultischema should be a no-op if used with sqlite3 or mysql.

Background

Your PostgreSQL database might have multiple schemas, that provide namespaces for tables. For example, you could define:

connection.execute "CREATE SCHEMA first"
connection.execute "CREATE SCHEMA second"

ActiveRecord's PostgreSQL connection adapter lets you set PostgreSQL's search_path to choose which schemas to look at:

connection.schema_search_path = "first,second"

And ActiveRecord let you use schema names in migrations, such as

create_table 'first.my_table' do ...
create_table 'second.my_table' do ...

But without SchemaPlusMultischema, ActiveRecord's introspection doesn't handle them properly. It does find all tables that are in the current search path, but it doesn't prefix them with their schema names. The schema dump schema/dump.rb doesn't take into account the schemas at all and the dump will be invalid. Futhermore, if there are tables with the same name in different schemas, only 1 of the tables will be dumped.

Features

With SchemaPlusMultischema installed, it activates its features whenever your schema search path differs from PostgreSQL's default ("$user",public). If schema search path is PostgreSQL's default, then SchemaPlusMultischema stays out of the way and the behavior is the same as pure ActiveRecord.

connection.tables

The output of ActiveRecord's connection.tables method will have table name prefixed with its schema. E.g.

connection.tables  # => ["first.my_table", "second.my_table"]

Schema dump

The schema dump (db/schema.rb) will include the schema setup, and the table definitions will be prefixed with their schemas. E.g.

    connection.execute "CREATE SCHEMA IF NOT EXISTS first"
    connection.execute "CREATE SCHEMA IF NOT EXISTS second"
    connection.schema_search_path = "first,second"

    create_table "first.my_table" do ...
    create_table "second.my_table" do ...

If you're using the schema_plus_foreign_keys gem, foreign key associations will also contain table definitions with prefixed schemas. E.g.

    create_table "first.my_table" do
      ...
      t.integer :my_table_id, null: false, foreign_key: {references: "second.my_table"...

History

Development & Testing

Are you interested in contributing to SchemaPlusMultischema? Thanks! Please follow the standard protocol: fork, feature branch, develop, push, and issue pull request.

Some things to know about to help you develop and test:

<!-- SCHEMA_DEV: TEMPLATE USES SCHEMA_DEV - begin --> <!-- These lines are auto-inserted from a schema_dev template --> <!-- SCHEMA_DEV: TEMPLATE USES SCHEMA_DEV - end --> <!-- SCHEMA_DEV: TEMPLATE USES SCHEMA_PLUS_CORE - begin --> <!-- These lines are auto-inserted from a schema_dev template --> <!-- SCHEMA_DEV: TEMPLATE USES SCHEMA_PLUS_CORE - end --> <!-- SCHEMA_DEV: TEMPLATE USES SCHEMA_MONKEY - begin --> <!-- These lines are auto-inserted from a schema_dev template --> <!-- SCHEMA_DEV: TEMPLATE USES SCHEMA_MONKEY - end -->