Skip to content
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

Adding support for fill_existing_with on add_belongs_to #444

Merged
merged 1 commit into from
Aug 24, 2020
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
3 changes: 2 additions & 1 deletion spec/migrator/alter_table_statement_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ describe Avram::Migrator::AlterTableStatement do
add_belongs_to post : Post?, on_delete: :restrict
add_belongs_to category_label : CategoryLabel, on_delete: :nullify, references: :custom_table
add_belongs_to employee : User, on_delete: :cascade
add_belongs_to line_item : LineItem, on_delete: :cascade, foreign_key_type: UUID
add_belongs_to line_item : LineItem, on_delete: :cascade, foreign_key_type: UUID, fill_existing_with: "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11"
end

built.statements.first.should eq <<-SQL
Expand All @@ -94,6 +94,7 @@ describe Avram::Migrator::AlterTableStatement do
built.statements[3].should eq "CREATE INDEX comments_category_label_id_index ON comments USING btree (category_label_id);"
built.statements[4].should eq "CREATE INDEX comments_employee_id_index ON comments USING btree (employee_id);"
built.statements[5].should eq "CREATE INDEX comments_line_item_id_index ON comments USING btree (line_item_id);"
built.statements[6].should eq "UPDATE comments SET line_item_id = 'a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11';"
end

it "raises error when on_delete strategy is invalid or nil" do
Expand Down
10 changes: 9 additions & 1 deletion src/avram/migrator/alter_table_statement.cr
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class Avram::Migrator::AlterTableStatement
end

# Adds a references column and index given a model class and references option.
macro add_belongs_to(type_declaration, on_delete, references = nil, foreign_key_type = Int64)
macro add_belongs_to(type_declaration, on_delete, references = nil, foreign_key_type = Int64, fill_existing_with = nil)
{% unless type_declaration.is_a?(TypeDeclaration) %}
{% raise "add_belongs_to expected a type declaration like 'user : User', instead got: '#{type_declaration}'" %}
{% end %}
Expand All @@ -99,6 +99,14 @@ class Avram::Migrator::AlterTableStatement
.set_references(references: %table_name.to_s, on_delete: {{ on_delete }})
.build_add_statement_for_alter

{% if fill_existing_with && fill_existing_with != :nothing %}
add_fill_existing_with_statements(
column: {{ foreign_key_name.stringify }},
type: {{ foreign_key_type }},
value: Avram::Migrator::Columns::{{ foreign_key_type }}Column.prepare_value_for_database({{ fill_existing_with }})
)
{% end %}

add_index :{{ foreign_key_name }}
end

Expand Down