Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/fixturebot/rails/schema_loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def build_table(name)
columns = @connection.columns(name)
.reject { |c| c.name == pk_column_name || timestamp_column?(c.name) }
.map { |c| c.name.to_sym }
.sort

fk_associations = @connection.foreign_keys(name).map do |fk|
Schema::BelongsTo.new(
Expand Down Expand Up @@ -96,6 +97,7 @@ def foreign_key_columns(table_name)
@connection.columns(table_name)
.select { |c| foreign_key_column?(c) }
.map { |c| c.name }
.sort
end

def association_name(column_name)
Expand Down
5 changes: 5 additions & 0 deletions spec/fixturebot/schema_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
expect(schema.tables[:tags].columns).to contain_exactly(:name)
end

it "orders columns alphabetically regardless of database column order" do
expect(schema.tables[:users].columns).to eq([:email, :name])
expect(schema.tables[:posts].columns).to eq([:author_id, :body, :title])
end

it "detects belongs_to associations from foreign keys" do
associations = schema.tables[:posts].belongs_to_associations
expect(associations.size).to eq(1)
Expand Down