sorted_tables = @connection .tables.sort not_ignored_tables = sorted_tables.reject { | table_name| ignored?( table_name) } not_ignored_tables.each_with_index do |table_name, index| table(table_name, stream) stream.puts if index < not_ignored_tables.count - 1 end # dump foreign keys at the end to make sure all dependent tables exist. if @connection .supports_foreign_keys? foreign_keys_stream = StringIO.new not_ignored_tables.each do |tbl| foreign_keys( tbl, foreign_keys_stream) end foreign_keys_string = foreign_keys_stream.string stream.puts if foreign_keys_string.length > 0 stream.print foreign_keys_string end end def table(table, stream) columns = @connection .columns(table) https://github.com/rails/rails/blob/v8.0.2/activerecord/lib/active_record/schema_dumper.rb#L134-L159
sorted_tables = @connection.tables.sort not_ignored_tables = sorted_tables.reject { | table_name| ignored?( table_name) } not_ignored_tables.each_with_index do |table_name, index| table(table_name, stream) stream.puts if index < not_ignored_tables.count - 1 end # dump foreign keys at the end to make sure all dependent tables exist. if @connection.supports_foreign_keys? foreign_keys_stream = StringIO.new not_ignored_tables.each do | tbl| foreign_keys( tbl, foreign_keys_stream) end foreign_keys_string = foreign_keys_stream.string stream.puts if foreign_keys_string.length > 0 stream.print foreign_keys_string end end def table(table, stream) columns = @connection .columns(table) https://github.com/rails/rails/blob/v8.0.2/activerecord/lib/active_record/schema_dumper.rb#L134-L159
column_definitions (table_name) query(<<~SQL, "SCHEMA") SELECT a.attname, format_type(a.atttypid, a.atttypmod), pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod, c.collname, col_description(a.attrelid, a.attnum) AS comment, #{supports_identity_columns? ? 'attidentity' : quote('')} AS identity, #{supports_virtual_columns? ? 'attgenerated' : quote('')} as attgenerated FROM pg_attribute a LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum LEFT JOIN pg_type t ON a.atttypid = t.oid LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation WHERE a.attrelid = #{quote(quote_table_name( table_name))}::regclass AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum SQL end https://github.com/rails/rails/blob/v8.0.2/activerecord/lib/active_record/connection_adapters/postgresq l_adapter.rb#L1034-L1049
column_definitions (table_name) query(<<~SQL, "SCHEMA") SELECT a.attname, format_type(a.atttypid, a.atttypmod), pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod, c.collname, col_description(a.attrelid, a.attnum) AS comment, #{supports_identity_columns? ? 'attidentity' : quote('')} AS identity, #{supports_virtual_columns? ? 'attgenerated' : quote('')} as attgenerated FROM pg_attribute a LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum LEFT JOIN pg_type t ON a.atttypid = t.oid LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation WHERE a.attrelid = #{quote(quote_table_name( table_name))}::regclass AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum SQL end https://github.com/rails/rails/blob/v8.0.2/activerecord/lib/active_record/connection_adapters/postgresq l_adapter.rb#L1034-L1049
column_definitions (table_name) query(<<~SQL, "SCHEMA") SELECT a.attname, format_type(a.atttypid, a.atttypmod), pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod, c.collname, col_description(a.attrelid, a.attnum) AS comment, #{supports_identity_columns? ? 'attidentity' : quote('')} AS identity, #{supports_virtual_columns? ? 'attgenerated' : quote('')} as attgenerated FROM pg_attribute a LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum LEFT JOIN pg_type t ON a.atttypid = t.oid LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation WHERE a.attrelid = #{quote(quote_table_name( table_name))}::regclass AND a.attnum > 0 AND NOT a.attisdropped ORDER BY a.attnum SQL end https://github.com/rails/rails/blob/v8.0.2/activerecord/lib/active_record/connection_adapters/postgresq l_adapter.rb#L1034-L1049
def preload_column_definitions (table_names) table_name_map = ( query(<<~SQL, "SCHEMA") SELECT (a.attrelid::regclass)::text , a.attnum, a.attname, format_type(a.atttypid, a.atttypmod), pg_get_expr(d.adbin, d.adrelid), a.attnotnull, a.atttypid, a.atttypmod, c.collname, col_description(a.attrelid, a.attnum) AS comment, #{supports_identity_columns? ? 'attidentity' : quote('')} AS identity, #{supports_virtual_columns? ? 'attgenerated' : quote('')} as attgenerated FROM pg_attribute a LEFT JOIN pg_attrdef d ON a.attrelid = d.adrelid AND a.attnum = d.adnum LEFT JOIN pg_type t ON a.atttypid = t.oid LEFT JOIN pg_collation c ON a.attcollation = c.oid AND a.attcollation <> t.typcollation WHERE a.attnum > 0 AND NOT a.attisdropped SQL ).group_by(&:first) ... https://github.com/rerost/activerecord_bulk_postgresql_adapter/blob/master/lib/activerecord_bulk_po stgresql_adapter.rb#L56-L70