-
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.
dump inherited table options to schema.rb (#42)
* dump inherited table options to schema.rb * fix for no inherited tables * dump inherited tables after * control ordering of dumping * stree
- Loading branch information
Showing
8 changed files
with
121 additions
and
10 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
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
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,25 @@ | ||
# frozen_string_literal: true | ||
|
||
module Hoardable | ||
module SchemaDumper | ||
def ignored?(table_name) | ||
super || @connection.inherited_table?(table_name) | ||
end | ||
|
||
def tables(stream) | ||
super | ||
dump_inherited_tables(stream) | ||
empty_line(stream) | ||
triggers(stream) | ||
end | ||
|
||
private def dump_inherited_tables(stream) | ||
sorted_tables = @connection.tables.filter { |table| @connection.inherited_table?(table) }.sort | ||
sorted_tables.each do |table_name| | ||
table(table_name, stream) | ||
foreign_keys(table_name, stream) | ||
end | ||
end | ||
end | ||
private_constant :SchemaDumper | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# frozen_string_literal: true | ||
|
||
module Hoardable | ||
module SchemaStatements | ||
def table_options(table_name) | ||
options = super || {} | ||
if inherited_table_names = parent_table_names(table_name) | ||
options[:options] = "INHERITS (#{inherited_table_names.join(", ")})" | ||
end | ||
options | ||
end | ||
|
||
private def parent_table_names(table_name) | ||
scope = quoted_scope(table_name, type: "BASE TABLE") | ||
|
||
query_values(<<~SQL.presence, "SCHEMA").presence | ||
SELECT parent.relname | ||
FROM pg_catalog.pg_inherits i | ||
JOIN pg_catalog.pg_class child ON i.inhrelid = child.oid | ||
JOIN pg_catalog.pg_class parent ON i.inhparent = parent.oid | ||
LEFT JOIN pg_namespace n ON n.oid = child.relnamespace | ||
WHERE child.relname = #{scope[:name]} | ||
AND child.relkind IN (#{scope[:type]}) | ||
AND n.nspname = #{scope[:schema]} | ||
SQL | ||
end | ||
|
||
def inherited_table?(table_name) | ||
parent_table_names(table_name).present? | ||
end | ||
end | ||
private_constant :SchemaStatements | ||
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,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require "helper" | ||
|
||
class TestSchemaDumper < ActiveSupport::TestCase | ||
test "it dumps table inheritance options to schema.rb" do | ||
output = dump_table_schema("post_versions") | ||
assert_match(<<~SCHEMA.squish, output) | ||
create_table \"post_versions\", | ||
id: :bigint, | ||
default: -> { \"nextval('posts_id_seq'::regclass)\" }, | ||
options: \"INHERITS (posts)\" | ||
SCHEMA | ||
end | ||
|
||
test "it does not dump table inheritance options for non inherited table" do | ||
output = dump_table_schema("posts") | ||
assert_match("create_table \"posts\", force: :cascade do |t|\n", output) | ||
end | ||
|
||
test "it dumps inherited table after parent table, and trigger after both" do | ||
output = dump_table_schema("post_versions", "posts") | ||
assert posts_index = output.index(/create_table "posts"/) | ||
assert post_versions_index = output.index(/create_table "post_versions"/) | ||
assert( | ||
post_versions_trigger_index = output.index(/create_trigger :post_versions_prevent_update/) | ||
) | ||
assert post_versions_index > posts_index | ||
assert post_versions_trigger_index > post_versions_index | ||
end | ||
|
||
private def dump_table_schema(*table_names) | ||
connection = ActiveRecord::Base.connection | ||
ActiveRecord::SchemaDumper.ignore_tables = connection.data_sources - table_names | ||
stream = StringIO.new | ||
ActiveRecord::SchemaDumper.dump(connection, stream).string | ||
end | ||
end |