-
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
Add support for optional arrays #471
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 |
---|---|---|
|
@@ -112,18 +112,17 @@ class Avram::Migrator::AlterTableStatement | |
end | ||
|
||
macro add(type_declaration, index = false, using = :btree, unique = false, default = nil, fill_existing_with = nil, **type_options) | ||
{% if type_declaration.type.is_a?(Union) %} | ||
{% type = type_declaration.type.types.first %} | ||
{% type = type_declaration.type %} | ||
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. Yeah, this is much cleaner! |
||
{% nilable = false %} | ||
{% array = false %} | ||
{% should_fill_existing = fill_existing_with && (fill_existing_with != :nothing) %} | ||
{% if type.is_a?(Union) %} | ||
{% type = type.types.first %} | ||
{% nilable = true %} | ||
{% array = false %} | ||
{% elsif type_declaration.type.is_a?(Generic) %} | ||
{% type = type_declaration.type.type_vars.first %} | ||
{% nilable = false %} | ||
{% end %} | ||
{% if type.is_a?(Generic) %} | ||
{% type = type.type_vars.first %} | ||
{% array = true %} | ||
{% else %} | ||
{% type = type_declaration.type %} | ||
{% nilable = (fill_existing_with != nil) && (fill_existing_with != :nothing) %} | ||
{% array = false %} | ||
{% end %} | ||
|
||
{% if !nilable && default == nil && fill_existing_with == nil %} | ||
|
@@ -133,9 +132,9 @@ class Avram::Migrator::AlterTableStatement | |
|
||
Try one of these... | ||
|
||
▸ add #{type_declaration.var} : #{type}, default: "Something" | ||
▸ add #{type_declaration.var} : #{type}, fill_existing_with: "Something" | ||
▸ add #{type_declaration.var} : #{type}, fill_existing_with: :nothing | ||
▸ add #{type_declaration}, default: "Something" | ||
▸ add #{type_declaration}, fill_existing_with: "Something" | ||
▸ add #{type_declaration}, fill_existing_with: :nothing | ||
ERROR | ||
%} | ||
{% end %} | ||
|
@@ -145,10 +144,10 @@ class Avram::Migrator::AlterTableStatement | |
{% end %} | ||
|
||
rows << Avram::Migrator::Columns::{{ type }}Column( | ||
{% if array %}Array({% end %}{{ type }}{% if array %}){% end %} | ||
{% if array %}Array({{ type }}){% else %}{{ type }}{% end %} | ||
).new( | ||
name: {{ type_declaration.var.stringify }}, | ||
nilable: {{ nilable }}, | ||
nilable: {{ nilable || should_fill_existing }}, | ||
default: {{ default }}, | ||
{{ **type_options }} | ||
) | ||
|
@@ -157,12 +156,12 @@ class Avram::Migrator::AlterTableStatement | |
{% end %} | ||
.build_add_statement_for_alter | ||
|
||
{% if fill_existing_with && fill_existing_with != :nothing %} | ||
{% if should_fill_existing %} | ||
add_fill_existing_with_statements( | ||
column: {{ type_declaration.var.stringify }}, | ||
type: {{ type }}, | ||
value: Avram::Migrator::Columns::{{ type }}Column.prepare_value_for_database({{ fill_existing_with }}), | ||
nilable: {{ type_declaration.type.is_a?(Union) }} | ||
nilable: {{ nilable }} | ||
) | ||
{% end %} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -247,13 +247,8 @@ abstract class Avram::Model | |
macro setup_getters(columns, *args, **named_args) | ||
{% for column in columns %} | ||
{% db_type = column[:type].is_a?(Generic) ? column[:type].type_vars.first : column[:type] %} | ||
def {{column[:name]}} : {% if column[:nilable] %}::Union({{db_type}}, ::Nil){% else %}{{column[:type]}}{% end %} | ||
%from_db = {{ db_type }}::Lucky.from_db!(@{{column[:name]}}) | ||
{% if column[:nilable] %} | ||
%from_db.as?({{db_type}}) | ||
{% else %} | ||
%from_db.as({{column[:type]}}) | ||
{% end %} | ||
def {{column[:name]}} : {{column[:type]}}{{(column[:nilable] ? "?" : "").id}} | ||
{{ db_type }}::Lucky.from_db!(@{{column[:name]}}) | ||
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. 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. I think they just use the default 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. ohh... sneaky 😂 |
||
end | ||
{% if column[:type].id == Bool.id %} | ||
def {{column[:name]}}? : Bool | ||
|
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'm pretty sure it works, but just for peace of mind, can you add to this spec where we take this bucket and update it to have an empty array? Or maybe add another spec in here that just tests a SaveOperation can have a nilable array but be updated to an empty array?