-
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 rename and rename_belongs_to for migrations. #366
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
class TestRenameColumns::V20200513065134 < Avram::Migrator::Migration::V1 | ||
def migrate | ||
create table_for(RenamableOwner) do | ||
primary_key id : Int64 | ||
add_timestamps | ||
end | ||
|
||
create table_for(Renamable) do | ||
primary_key id : Int64 | ||
add_timestamps | ||
add thingies : JSON::Any | ||
add gsm_number : String | ||
add_belongs_to boss : RenamableOwner, on_delete: :cascade | ||
end | ||
|
||
alter table_for(Renamable) do | ||
rename :gsm_number, :mobile | ||
rename_belongs_to :boss, :owner | ||
rename :thingies, :options | ||
end | ||
end | ||
|
||
def rollback | ||
drop table_for(Renamable) | ||
drop table_for(RenamableOwner) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,11 +16,18 @@ describe Avram::Migrator::AlterTableStatement do | |
add future_time : Time, default: Time.local | ||
add new_id : UUID, default: UUID.new("46d9b2f0-0718-4d4c-a5a1-5af81d5b11e0") | ||
add numbers : Array(Int32), fill_existing_with: [1] | ||
rename :old_name, :new_name | ||
rename_belongs_to :owner, :boss | ||
remove :old_column | ||
remove_belongs_to :employee | ||
end | ||
|
||
built.statements.first.should eq <<-SQL | ||
built.statements.size.should eq 11 | ||
|
||
built.statements[0].should eq "ALTER TABLE users RENAME COLUMN old_name TO new_name;" | ||
built.statements[1].should eq "ALTER TABLE users RENAME COLUMN owner_id TO boss_id;" | ||
|
||
built.statements[2].should eq <<-SQL | ||
ALTER TABLE users | ||
ADD name text, | ||
ADD email text, | ||
|
@@ -39,13 +46,12 @@ describe Avram::Migrator::AlterTableStatement do | |
DROP employee_id; | ||
SQL | ||
|
||
built.statements.size.should eq 9 | ||
built.statements[1].should eq "CREATE UNIQUE INDEX users_age_index ON users USING btree (age);" | ||
built.statements[2].should eq "CREATE INDEX users_num_index ON users USING btree (num);" | ||
built.statements[3].should eq "UPDATE users SET email = '[email protected]';" | ||
built.statements[4].should eq "ALTER TABLE users ALTER COLUMN email SET NOT NULL;" | ||
built.statements[5].should eq "UPDATE users SET updated_at = NOW();" | ||
built.statements[6].should eq "ALTER TABLE users ALTER COLUMN updated_at SET NOT NULL;" | ||
built.statements[3].should eq "CREATE UNIQUE INDEX users_age_index ON users USING btree (age);" | ||
built.statements[4].should eq "CREATE INDEX users_num_index ON users USING btree (num);" | ||
built.statements[5].should eq "UPDATE users SET email = '[email protected]';" | ||
built.statements[6].should eq "ALTER TABLE users ALTER COLUMN email SET NOT NULL;" | ||
built.statements[7].should eq "UPDATE users SET updated_at = NOW();" | ||
built.statements[8].should eq "ALTER TABLE users ALTER COLUMN updated_at SET NOT NULL;" | ||
end | ||
|
||
it "does not build statements if nothing is altered" do | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
This is a really cool idea 👍
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.
Yeah, I was really happy with that one. 😊