lib/ahnnotate/tables.rb in ahnnotate-0.4.0 vs lib/ahnnotate/tables.rb in ahnnotate-0.5.0
- old
+ new
@@ -2,10 +2,11 @@
class Tables
include Enumerable
def initialize(connection = ActiveRecord::Base.connection)
@connection = connection
+ @abilities = Abilities.new(connection)
end
def to_h
map { |table| [table.name, table] }.to_h
end
@@ -26,11 +27,11 @@
primary_key == c.name
end
Column.new(
name: c.name,
- type: c.type.to_s,
+ sql_type: c.sql_type,
nullable: c.null,
primary_key: is_primary_key,
default: c.default
)
end
@@ -47,13 +48,28 @@
unique: i.unique,
comment: comment
)
end
+ foreign_keys =
+ if @abilities.foreign_key?
+ @connection.foreign_keys(table_name).map do |fk|
+ ForeignKey.new(
+ name: fk.name,
+ from_column: fk.column,
+ to_table: fk.to_table,
+ to_column: fk.primary_key
+ )
+ end
+ else
+ []
+ end
+
yield Table.new(
name: table_name,
columns: columns,
- indexes: indexes
+ indexes: indexes,
+ foreign_keys: foreign_keys,
)
end
end
def data_sources_method_name