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

Support belongs to with namespaced classes #685

Merged
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
7 changes: 5 additions & 2 deletions spec/migrator/alter_table_statement_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ describe Avram::Migrator::AlterTableStatement do
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, fill_existing_with: "a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11"
add_belongs_to subscription_item : Subscription::Item, on_delete: :cascade
end

built.statements.first.should eq <<-SQL
Expand All @@ -122,15 +123,17 @@ describe Avram::Migrator::AlterTableStatement do
ADD post_id bigint REFERENCES posts ON DELETE RESTRICT,
ADD category_label_id bigint NOT NULL REFERENCES custom_table ON DELETE SET NULL,
ADD employee_id bigint NOT NULL REFERENCES users ON DELETE CASCADE,
ADD line_item_id uuid NOT NULL REFERENCES line_items ON DELETE CASCADE;
ADD line_item_id uuid NOT NULL REFERENCES line_items ON DELETE CASCADE,
ADD subscription_item_id bigint NOT NULL REFERENCES subscription_items ON DELETE CASCADE;
SQL

built.statements[1].should eq "CREATE UNIQUE INDEX comments_user_id_index ON comments USING btree (user_id);"
built.statements[2].should eq "CREATE INDEX comments_post_id_index ON comments USING btree (post_id);"
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';"
built.statements[6].should eq "CREATE INDEX comments_subscription_item_id_index ON comments USING btree (subscription_item_id);"
built.statements[7].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
5 changes: 4 additions & 1 deletion spec/migrator/create_table_statement_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ describe Avram::Migrator::CreateTableStatement do
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 subscription_item : Subscription::Item, on_delete: :cascade
end

built.statements.first.should eq <<-SQL
Expand All @@ -199,14 +200,16 @@ describe Avram::Migrator::CreateTableStatement do
post_id bigint REFERENCES posts ON DELETE RESTRICT,
category_label_id bigint NOT NULL REFERENCES custom_table ON DELETE SET NULL,
employee_id bigint NOT NULL REFERENCES users ON DELETE CASCADE,
line_item_id uuid NOT NULL REFERENCES line_items ON DELETE CASCADE);
line_item_id uuid NOT NULL REFERENCES line_items ON DELETE CASCADE,
subscription_item_id bigint NOT NULL REFERENCES subscription_items ON DELETE CASCADE);
SQL

built.statements[1].should eq "CREATE INDEX comments_user_id_index ON comments USING btree (user_id);"
built.statements[2].should eq "CREATE INDEX comments_post_id_index ON comments USING btree (post_id);"
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 "CREATE INDEX comments_subscription_item_id_index ON comments USING btree (subscription_item_id);"
end

it "can create tables with association on composite primary keys" do
Expand Down
5 changes: 3 additions & 2 deletions src/avram/migrator/alter_table_statement.cr
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,11 @@ class Avram::Migrator::AlterTableStatement
{% optional = type_declaration.type.is_a?(Union) %}

{% if optional %}
{% underscored_class = type_declaration.type.types.first.stringify.underscore %}
{% underscored_class = type_declaration.type.types.first %}
{% else %}
{% underscored_class = type_declaration.type.stringify.underscore %}
{% underscored_class = type_declaration.type %}
{% end %}
{% underscored_class = underscored_class.stringify.underscore.gsub(/::/, "_") %}

{% foreign_key_name = type_declaration.var + "_id" %}
%table_name = {{ references }} || Wordsmith::Inflector.pluralize({{ underscored_class }})
Expand Down
5 changes: 3 additions & 2 deletions src/avram/migrator/create_table_statement.cr
Original file line number Diff line number Diff line change
Expand Up @@ -125,10 +125,11 @@ class Avram::Migrator::CreateTableStatement
{% optional = type_declaration.type.is_a?(Union) %}

{% if optional %}
{% underscored_class = type_declaration.type.types.first.stringify.underscore %}
{% underscored_class = type_declaration.type.types.first %}
{% else %}
{% underscored_class = type_declaration.type.stringify.underscore %}
{% underscored_class = type_declaration.type %}
{% end %}
{% underscored_class = underscored_class.stringify.underscore.gsub(/::/, "_") %}

{% foreign_key_name = type_declaration.var + "_id" %}
%table_name = {{ references }} || Wordsmith::Inflector.pluralize({{ underscored_class }})
Expand Down