-
Notifications
You must be signed in to change notification settings - Fork 64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds in Array enums. #1009
Adds in Array enums. #1009
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
class AddEnumsToBucket::V20240309222910 < Avram::Migrator::Migration::V1 | ||
def migrate | ||
alter table_for(Bucket) do | ||
add enums : Array(Int32), default: [] of Int32 | ||
end | ||
end | ||
|
||
def rollback | ||
alter table_for(Bucket) do | ||
remove :enums | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -212,7 +212,7 @@ abstract class Avram::Model | |
{% end %} | ||
end | ||
|
||
macro column(type_declaration, autogenerated = false, serialize is_serialized = false, allow_blank = false) | ||
macro column(type_declaration, autogenerated = false, serialize is_serialized = false, allow_blank = false, converter = nil) | ||
{% if type_declaration.type.is_a?(Union) %} | ||
{% data_type = type_declaration.type.types.first %} | ||
{% nilable = true %} | ||
|
@@ -233,6 +233,8 @@ abstract class Avram::Model | |
converter: JSONConverter({{ data_type }}), | ||
{% elsif data_type.id == Array(Float64).id %} | ||
converter: PG::NumericArrayFloatConverter, | ||
{% elsif converter %} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Originally I was going to use {% elsif data_type.resolve < Enum %} This would let us pick up the type and set the converter automatically; however, once I did this, it starts to resolve ALL the types. This means that in the |
||
converter: {{ converter }}, | ||
{% end %} | ||
)] | ||
{% if data_type.is_a?(Generic) || is_serialized %} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
# Extends the PG shard and adds a converter for | ||
# converting `Array(Int)` columns to `Array(Enum)`. This | ||
# can be used with raw SQL queries. | ||
# ``` | ||
# enum Colors | ||
# Red | ||
# end | ||
# @[DB::Field(converter: PG::EnumArrayConverter(Colors))] | ||
# property colors : Array(Colors) | ||
# ``` | ||
module PG::EnumArrayConverter(T) | ||
def self.from_rs(rs : DB::ResultSet) | ||
rs.read(Array(typeof(T.values.first.value))).map { |i| T.from_value(i) } | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually don't know why this needs to be String.... but if you look at the method below, it's also converting the enum value to a string. Maybe a
PQ
thing? 🤔