# frozen_string_literal: true module Torque module PostgreSQL module Adapter module SchemaDumper def dump(stream) # :nodoc: @connection.dump_mode! super @connection.dump_mode! stream end def extensions(stream) # :nodoc: super user_defined_schemas(stream) user_defined_types(stream) end # Translate +:enum_set+ into +:enum+ def schema_type(column) column.type == :enum_set ? :enum : super end # Adds +:enum_type+ option to the default set def prepare_column_options(column) spec = super if enum_type = schema_enum_type(column) spec[:enum_type] = enum_type end spec end private def schema_enum_type(column) column.sql_type.to_sym.inspect if column.type == :enum || column.type == :enum_set end def tables(stream) # :nodoc: inherited_tables = @connection.inherited_tables sorted_tables = (@connection.tables - @connection.views).sort_by do |table_name| table_name.split(/(?:public)?\./).reverse end stream.puts " # These are the common tables" (sorted_tables - inherited_tables.keys).each do |table_name| table(table_name, stream) unless ignored?(table_name) end if inherited_tables.present? stream.puts " # These are tables that have inheritance" inherited_tables.each do |table_name, inherits| next if ignored?(table_name) sub_stream = StringIO.new table(table_name, sub_stream) # Add the inherits setting sub_stream.rewind inherits.map! { |parent| parent.to_s.sub(/\Apublic\./, '') } inherits = inherits.first if inherits.size === 1 inherits = ", inherits: #{inherits.inspect} do |t|" table_dump = sub_stream.read.gsub(/ do \|t\|$/, inherits) # Ensure bodyless definitions table_dump.gsub!(/do \|t\|\n end/, '') stream.print table_dump end end # Dump foreign keys at the end to make sure all dependent tables exist. if @connection.supports_foreign_keys? sorted_tables.each do |tbl| foreign_keys(tbl, stream) unless ignored?(tbl) end end # Scenic integration views(stream) if defined?(::Scenic) # FX integration functions(stream) if defined?(::Fx::SchemaDumper::Function) triggers(stream) if defined?(::Fx::SchemaDumper::Trigger) end # Make sure to remove the schema from the table name def remove_prefix_and_suffix(table) super(table.sub(/\A[a-z0-9_]*\./, '')) end # Dump user defined schemas def user_defined_schemas(stream) return if (list = (@connection.user_defined_schemas - ['public'])).empty? stream.puts " # Custom schemas defined in this database." list.each { |name| stream.puts " create_schema \"#{name}\", force: :cascade" } stream.puts end # Dump user defined types like enum def user_defined_types(stream) types = @connection.user_defined_types('e') return unless types.any? stream.puts " # Custom types defined in this database." stream.puts " # Note that some types may not work with other database engines. Be careful if changing database." types.sort_by(&:first).each { |(name, type)| send(type.to_sym, name, stream) } stream.puts rescue => e stream.puts "# Could not dump user-defined types because of following #{e.class}" stream.puts "# #{e.message}" stream.puts end # Dump enum custom type def enum(name, stream) values = @connection.enum_values(name).map { |v| "\"#{v}\"" } stream.puts " create_enum \"#{name}\", [#{values.join(', ')}], force: :cascade" end end ActiveRecord::ConnectionAdapters::PostgreSQL::SchemaDumper.prepend SchemaDumper end end end