-
-
Save manveru/0da874e522aeec9e8dd8a43c8d58a9ac to your computer and use it in GitHub Desktop.
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
class CreateOrganizers::V20191201190236 < Avram::Migrator::Migration::V1 | |
def migrate | |
create table_for(Organizer) do | |
primary_key id : UUID | |
add_timestamps | |
add name : String | |
add description : String | |
add_belongs_to editor : User, on_delete: :restrict, foreign_key_type: UUID | |
end | |
alter table_for(Event) do | |
add_belongs_to( | |
organizer : Organizer?, | |
on_delete: :cascade, | |
foreign_key_type: UUID, | |
) | |
end | |
user_id = UserQuery.new.first.id | |
org_id = UUID.random | |
execute <<-SQL | |
INSERT INTO organizers | |
( id | |
, name | |
, description | |
, editor_id | |
, created_at | |
, updated_at | |
) | |
VALUES | |
( '#{org_id}' | |
, 'ACME' | |
, 'Nothing' | |
, '#{user_id}' | |
, NOW() | |
, NOW() | |
); | |
SQL | |
execute "UPDATE events SET organizer_id = '#{org_id}';" | |
make_required :events, :organizer_id | |
end | |
def rollback | |
alter table_for(Event) do | |
remove_belongs_to :organizer | |
end | |
drop :organizers | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment