Skip to content

Instantly share code, notes, and snippets.

@manveru manveru/create_organizers.cr Secret
Created Dec 2, 2019

Embed
What would you like to do?
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
You can’t perform that action at this time.