-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from SchemaPlus/middleware
Use middleware; include schema definitions in dump; minor cleanups
- Loading branch information
Showing
9 changed files
with
232 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,6 @@ | ||
require 'schema_plus/core' | ||
|
||
require_relative 'schema_plus_multischema/version' | ||
require_relative 'schema_plus_multischema/active_record/connection_adapters/postgresql_adapter' | ||
|
||
module SchemaPlusMultischema | ||
module ActiveRecord | ||
end | ||
end | ||
require_relative 'schema_plus_multischema/middleware' | ||
|
||
SchemaMonkey.register SchemaPlusMultischema |
30 changes: 0 additions & 30 deletions
30
lib/schema_plus_multischema/active_record/connection_adapters/postgresql_adapter.rb
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
module SchemaPlusMultischema | ||
module Middleware | ||
|
||
module PostgreSQL | ||
DEFAULT_SCHEMA_SEARCH_PATH = %q{"$user",public} | ||
|
||
module Schema | ||
module Tables | ||
|
||
def implement(env) | ||
use_prefix = (env.connection.schema_search_path != DEFAULT_SCHEMA_SEARCH_PATH) | ||
query = <<-SQL | ||
SELECT schemaname, tablename | ||
FROM pg_tables | ||
WHERE schemaname = ANY(current_schemas(false)) | ||
SQL | ||
env.tables += env.connection.exec_query(query, 'SCHEMA').map { |row| | ||
if use_prefix | ||
"#{row['schemaname']}.#{row['tablename']}" | ||
else | ||
row['tablename'] | ||
end | ||
} | ||
end | ||
|
||
end | ||
end | ||
|
||
module Dumper | ||
module Initial | ||
|
||
def after(env) | ||
if (path = env.connection.schema_search_path) != DEFAULT_SCHEMA_SEARCH_PATH | ||
path.split(',').each do |name| | ||
env.initial << %Q{ connection.execute "CREATE SCHEMA IF NOT EXISTS #{name}"} | ||
end | ||
env.initial << %Q{ connection.schema_search_path = #{path.inspect}} | ||
end | ||
end | ||
|
||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,75 @@ | ||
require 'spec_helper' | ||
|
||
describe 'Schema dump' do | ||
before(:each) do | ||
ActiveRecord::Migration.suppress_messages do | ||
let(:connection) { ActiveRecord::Base.connection } | ||
|
||
ActiveRecord::Schema.define do | ||
connection.schema_search_path='first,second' | ||
connection.tables.each do |table| drop_table table, force: :cascade end | ||
context "with multiple schemas" do | ||
|
||
execute <<-SQL | ||
CREATE SCHEMA IF NOT EXISTS first; | ||
CREATE TABLE first.dogs | ||
( | ||
id INTEGER PRIMARY KEY | ||
); | ||
SQL | ||
around(:each) do |example| | ||
with_schemas %w[first second] do | ||
example.run | ||
end | ||
end | ||
|
||
it "includes the schema definitions and path in the dump" do | ||
expect(dump_schema).to include('CREATE SCHEMA IF NOT EXISTS first') | ||
expect(dump_schema).to include('CREATE SCHEMA IF NOT EXISTS second') | ||
expect(dump_schema).to include('schema_search_path = "first,second"') | ||
end | ||
|
||
context 'with a table that is created without a schema prefix' do | ||
before(:each) do | ||
schema_definitions do | ||
create_table 'no_schema_prefix' | ||
end | ||
end | ||
it 'includes schema prefix in dump' do | ||
expect(dump_schema).to include('create_table "first.no_schema_prefix"') | ||
end | ||
end | ||
|
||
execute <<-SQL | ||
CREATE SCHEMA IF NOT EXISTS second; | ||
CREATE TABLE second.dogs | ||
( | ||
id INTEGER PRIMARY KEY | ||
); | ||
SQL | ||
context 'tables with same name in different schemas' do | ||
before(:each) do | ||
schema_definitions do | ||
create_table 'first.dogs' | ||
create_table 'second.dogs' | ||
end | ||
end | ||
|
||
execute <<-SQL | ||
CREATE SCHEMA IF NOT EXISTS second; | ||
CREATE TABLE first.owners | ||
( | ||
id INTEGER PRIMARY KEY, | ||
dog_id INTEGER NOT NULL | ||
); | ||
CREATE INDEX fk__first_owners_second_dogs ON first.owners USING btree (dog_id); | ||
it 'includes both tables with schema prefixes' do | ||
expect(dump_schema).to include('create_table "first.dogs"') | ||
expect(dump_schema).to include('create_table "second.dogs"') | ||
end | ||
|
||
ALTER TABLE ONLY first.owners | ||
ADD CONSTRAINT fk_first_owners_dog_id FOREIGN KEY (dog_id) REFERENCES second.dogs(id) ON DELETE CASCADE; | ||
SQL | ||
context 'with schema_plus_foreign_keys support' do | ||
before(:each) do | ||
schema_definitions do | ||
create_table 'first.owners' do |t| | ||
t.integer :dog_id, null: false, references: 'second.dogs' | ||
end | ||
end | ||
end | ||
|
||
execute <<-SQL | ||
CREATE SCHEMA IF NOT EXISTS second; | ||
CREATE TABLE no_schema_prefix | ||
( | ||
id INTEGER PRIMARY KEY | ||
); | ||
SQL | ||
it 'includes foreign key references with schema prefixes' do | ||
expect(dump_schema).to include('foreign_key: {references: "second.dogs", name: "fk_first_owners_dog_id"') | ||
end | ||
end | ||
end | ||
end | ||
|
||
def dump_schema(opts={}) | ||
stream = StringIO.new | ||
ActiveRecord::SchemaDumper.ignore_tables = Array.wrap(opts[:ignore]) || [] | ||
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) | ||
stream.string | ||
context "without multiple schemas" do | ||
it "does not include schema setup" do | ||
expect(dump_schema).not_to include('CREATE SCHEMA') | ||
expect(dump_schema).not_to include('schema_search_path') | ||
end | ||
end | ||
|
||
it 'should dump tables which are created without schema prefix' do | ||
expect(dump_schema).to include('create_table "first.no_schema_prefix"') | ||
end | ||
private | ||
|
||
it 'should dump tables with same names from different schemas' do | ||
expect(dump_schema).to include('create_table "first.dogs"') | ||
expect(dump_schema).to include('create_table "second.dogs"') | ||
def dump_schema | ||
stream = StringIO.new | ||
ActiveRecord::SchemaDumper.dump(ActiveRecord::Base.connection, stream) | ||
stream.string | ||
end | ||
|
||
context 'when foreign key schema plus gem required' do | ||
it 'should dump foreign key references with schema names' do | ||
expect(dump_schema).to include('foreign_key: {references: "second.dogs", name: "fk_first_owners_dog_id"') | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
def with_schemas(names) | ||
connection = ActiveRecord::Base.connection | ||
begin | ||
previous_schemas = connection.schema_search_path | ||
names.each do |name| | ||
connection.execute "CREATE SCHEMA IF NOT EXISTS #{name}" | ||
end | ||
connection.schema_search_path = names.join(',') | ||
yield | ||
ensure | ||
drop_all_tables | ||
names.each do |name| | ||
connection.execute "DROP SCHEMA IF EXISTS #{name}" | ||
end | ||
connection.schema_search_path = previous_schemas | ||
end | ||
end | ||
|
||
def schema_definitions(&block) | ||
ActiveRecord::Schema.define &block | ||
end | ||
|
||
def drop_all_tables | ||
ActiveRecord::Base.connection.tables.each do |table| | ||
ActiveRecord::Base.connection.drop_table table, force: :cascade | ||
end | ||
end | ||
|
||
|
Oops, something went wrong.