Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Patching Rails database schema dumps to support multiple PostgreSQL schemas.
# Overrides Rails file activerecord/lib/active_record/schema_dumper.rb to
# include all schema information in the db/schema.rb file, for example, in the
# create_table statements. This allows for a working development database
# to be built from a schema.rb file generated by rake db:schema:dump.
#
# This is essentially a rebuild of the "schema_plus_multischema" gem (which is
# unfortunately only compatible with Rails ~> 4.2).
#
# Tested with Rails 6.0.
module ActiveRecord
class SchemaDumper
# Overridden in order to call new method "schemas".
def dump(stream)
header(stream)
extensions(stream)
schemas(stream)
tables(stream)
trailer(stream)
clean(stream)
stream
end
private
# Adds following lines just after the extensions:
# * connection.execute "CREATE SCHEMA ..."
# * connection.schema_search_path = ...
def schemas(stream)
@connection.schema_search_path.split(',').each do |name|
stream.puts %Q{ connection.execute "CREATE SCHEMA IF NOT EXISTS #{name}"}
end
stream.puts ""
stream.puts %Q{ connection.schema_search_path = #{@connection.schema_search_path.inspect}}
stream.puts ""
end
# Overridden in order to build a list of tables with their schema prefix
# (rest of the method is the same).
def tables(stream)
table_query = <<-SQL
SELECT schemaname, tablename
FROM pg_tables
WHERE schemaname = ANY(current_schemas(false))
SQL
sorted_tables = @connection.exec_query(table_query, 'SCHEMA').map do |table|
"#{table['schemaname']}.#{table['tablename']}"
end.sort
sorted_tables.each do |table_name|
table(table_name, stream) unless ignored?(table_name)
end
if @connection.supports_foreign_keys?
sorted_tables.each do |tbl|
foreign_keys(tbl, stream) unless ignored?(tbl)
end
end
end
end
end
@layerssss

This comment has been minimized.

Copy link

@layerssss layerssss commented Nov 26, 2020

Hey the clean stream line seems to have been removed in 6.0.4.3

https://github.com/rails/rails/blob/v6.0.3.4/activerecord/lib/active_record/schema_dumper.rb#L41

To anyone getting error undefined method clean for ... just removed that line it will still work!

@lucascaton

This comment has been minimized.

Copy link

@lucascaton lucascaton commented Jan 1, 2021

Hey Glen - this is great, thanks for sharing.
It'd be nice to get this merged into Rails! 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment