Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a6c54a0
commit b0a42e0
Showing
5 changed files
with
47 additions
and
2 deletions.
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,13 @@ | ||
class AddUsernameToUser::V20190120112946 < LuckyRecord::Migrator::Migration::V1 | ||
def migrate | ||
alter :users do | ||
add username : String?, fill_existing_with: :nothing, unique: true | ||
end | ||
end | ||
|
||
def rollback | ||
alter :users do | ||
remove :username | ||
end | ||
end | ||
end |
18 changes: 18 additions & 0 deletions
18
db/migrations/20190120141952_generate_usernames_for_users.cr
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,18 @@ | ||
class GenerateUsernamesForUsers::V20190120141952 < LuckyRecord::Migrator::Migration::V1 | ||
class User < BaseModel | ||
table :users do | ||
column username : String? | ||
end | ||
end | ||
|
||
def migrate | ||
UserQuery.all.each do |user| | ||
new_username = user.email.split("@").first | ||
User::BaseForm.update!(user, username: new_username) | ||
puts "Generated username #{new_username} for #{user.email}" | ||
end | ||
end | ||
|
||
def rollback | ||
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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
class RequireUsernameOnUser::V20190120142017 < LuckyRecord::Migrator::Migration::V1 | ||
def migrate | ||
execute "ALTER TABLE users ALTER COLUMN username SET NOT NULL" | ||
end | ||
|
||
def rollback | ||
execute "ALTER TABLE users ALTER COLUMN username DROP NOT NULL" | ||
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
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