Skip to content
Merged
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: 1 addition & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
annotato (0.2.3)
annotato (0.2.4)
rails (>= 6.0)

GEM
Expand Down
13 changes: 6 additions & 7 deletions lib/annotato/enum_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,18 @@ module Annotato
class EnumFormatter
def self.format(conn, columns)
# Collect columns backed by a native PG enum type.
# A native enum type is one that exists in pg_type with typtype = 'e'.
enum_columns = columns.select { |col| pg_enum_type?(conn, col.sql_type) }
return [] if enum_columns.empty?

enum_columns.map do |col|
enum_columns.flat_map do |col|
labels = pg_enum_labels(conn, col.sql_type)
lines = ["# #{col.name} (#{col.sql_type}): ["]
lines += labels.map.with_index { |label, i|
labels.each_with_index do |label, i|
comma = i == labels.size - 1 ? "" : ","
"# #{label}#{comma}"
}
lines << "# #{label}#{comma}"
end
lines << "# ]"
lines.join("\n")
lines
end
end

Expand All @@ -40,7 +39,7 @@ def self.format(conn, columns)
conn.exec_query(
"SELECT e.enumlabel FROM pg_enum e " \
"JOIN pg_type t ON e.enumtypid = t.oid " \
"WHERE t.typname = $1 " \
"WHERE t.typname = $1 AND t.typtype = 'e' " \
"ORDER BY e.enumsortorder",
"SQL",
[type_name]
Expand Down
2 changes: 1 addition & 1 deletion lib/annotato/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Annotato
VERSION = "0.2.3"
VERSION = "0.2.4"
end
38 changes: 18 additions & 20 deletions spec/annotato/enum_formatter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
RSpec.describe Annotato::EnumFormatter do
let(:connection) { double("Connection") }

def pg_type_result(exists)
exists ? [{ "typname" => "my_enum" }] : []
end

context "when no columns have a native PG enum type" do
let(:columns) do
[
Expand Down Expand Up @@ -38,17 +34,14 @@ def pg_type_result(exists)
end

before do
# bigint is not an enum
allow(connection).to receive(:exec_query)
.with(/SELECT 1 FROM pg_type/, anything, ["bigint"])
.and_return([])

# access_link_category is a native enum
allow(connection).to receive(:exec_query)
.with(/SELECT 1 FROM pg_type/, anything, ["access_link_category"])
.and_return([{ "typname" => "access_link_category" }])

# labels query
allow(connection).to receive(:exec_query)
.with(/SELECT e.enumlabel/, anything, ["access_link_category"])
.and_return([
Expand All @@ -58,16 +51,21 @@ def pg_type_result(exists)
])
end

it "formats the DB enum type definition" do
it "returns a flat array of comment lines" do
result = described_class.format(connection, columns)
expect(result).to eq([
"# category (access_link_category): [",
"# internal,",
"# external,",
"# partner",
"# ]"
])
end

it "includes all labels including the last one" do
result = described_class.format(connection, columns)
expect(result.size).to eq(1)
expect(result.first).to eq(<<~ENUM.strip)
# category (access_link_category): [
# internal,
# external,
# partner
# ]
ENUM
expect(result).to include("# partner")
expect(result.last).to eq("# ]")
end
end

Expand All @@ -91,10 +89,10 @@ def pg_type_result(exists)

it "strips the array suffix and formats the enum type" do
result = described_class.format(connection, columns)
expect(result.size).to eq(1)
expect(result.first).to include("roles (user_role[]): [")
expect(result.first).to include("# admin,")
expect(result.first).to include("# viewer")
expect(result).to include("# roles (user_role[]): [")
expect(result).to include("# admin,")
expect(result).to include("# viewer")
expect(result).to include("# ]")
end
end
end
Loading