Skip to content

Commit

Permalink
Merge pull request #10 from shiftcommerce/add_support_for_structure_sql
Browse files Browse the repository at this point in the history
Add support for structure.sql files
  • Loading branch information
hubertpompecki authored Mar 14, 2018
2 parents aeaf74a + cad3fc7 commit da97d86
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
52 changes: 48 additions & 4 deletions lib/penthouse/tenants/migratable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,60 @@ def migrate(db_schema_file: Penthouse.configuration.db_schema_file)
if File.exist?(db_schema_file)
# run the migrations within this schema
call do
# don't output all the log messages
ActiveRecord::Schema.verbose = false
# run the schema file to migrate this tenant
load(db_schema_file)
read_schema(db_schema_file)
end
else
raise ArgumentError, "#{db_schema_file} does not exist"
end
end

private

def read_schema(db_schema_file)
case db_schema_file.extname
when '.rb'
load_ruby_schema(db_schema_file)
when '.sql'
load_sql_schema(db_schema_file)
else
raise ArgumentError, "Unrecognized schema file extension"
end
end

def load_ruby_schema(db_schema_file)
# don't output all the log messages
ActiveRecord::Schema.verbose = false
# run the schema file to migrate this tenant
load(db_schema_file)
end

def load_sql_schema(db_schema_file)
sql = process_schema_file(db_schema_file)

ActiveRecord::Base.transaction do
with_limited_logging { ActiveRecord::Base.connection.execute(sql) }
end
end

def with_limited_logging
temp_logger = Logger.new(STDOUT).tap { |l| l.level = Logger::ERROR }
current_logger = ActiveRecord::Base.logger

ActiveRecord::Base.logger = temp_logger
yield
ActiveRecord::Base.logger = current_logger
end

def process_schema_file(db_schema_file)
sql = File.read(db_schema_file)
sanitize_sql(sql)
end

def sanitize_sql(sql)
sql
.gsub(/SET search_path.*;/, '')
.gsub(/CREATE SCHEMA/, 'CREATE SCHEMA IF NOT EXISTS')
end
end
end
end
2 changes: 1 addition & 1 deletion lib/penthouse/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Penthouse
VERSION = "0.12.2"
VERSION = "0.13"
end
13 changes: 11 additions & 2 deletions spec/penthouse/tenants/octopus_schema_tenant_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
let(:schema_name) { "octopus_schema_tenant_test" }
let(:persistent_schemas) { ["shared_extensions"] }
let(:default_schema) { "public" }
let(:db_schema_file) { File.join(File.dirname(__FILE__), '../../support/schema.rb') }
let(:db_schema_file) { Pathname.new(File.join(File.dirname(__FILE__), '../../support/schema.rb')) }
let(:db_structure_file) { Pathname.new(File.join(File.dirname(__FILE__), '../../support/structure.sql')) }

subject(:octopus_schema_tenant) {
described_class.new(
Expand All @@ -32,13 +33,21 @@
end

context "when running migrations" do
it "should create the relevant Postgres schema and tables" do
it "should create the relevant Postgres schema and tables for ActiveRecord schema" do
octopus_schema_tenant.create(run_migrations: true, db_schema_file: db_schema_file)
octopus_schema_tenant.call do |tenant|
expect(Post.create(title: "test", description: "test")).to be_persisted
expect(Post.count).to eq(1)
end
end

it "should create the relevant Postgres schema and tables for SQL schema" do
octopus_schema_tenant.create(run_migrations: true, db_schema_file: db_structure_file)
octopus_schema_tenant.call do |tenant|
expect(Post.create(title: "test", description: "test")).to be_persisted
expect(Post.count).to eq(1)
end
end
end
end
end
23 changes: 23 additions & 0 deletions spec/support/structure.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
SET search_path = this_should_get_replaced;

CREATE TABLE posts (
id integer NOT NULL,
title character varying NOT NULL,
description character varying NOT NULL,
created_at timestamp without time zone NOT NULL,
updated_at timestamp without time zone NOT NULL
);

CREATE SEQUENCE posts_id_seq
START WITH 1
INCREMENT BY 1
NO MINVALUE
NO MAXVALUE
CACHE 1;

ALTER SEQUENCE posts_id_seq OWNED BY posts.id;

ALTER TABLE ONLY posts ALTER COLUMN id SET DEFAULT nextval('posts_id_seq'::regclass);

ALTER TABLE ONLY posts
ADD CONSTRAINT posts_pkey PRIMARY KEY (id);

0 comments on commit da97d86

Please sign in to comment.