Skip to content

Instantly share code, notes, and snippets.

@jwoertink
Created July 22, 2021 15:01
Show Gist options
  • Save jwoertink/97db7318dc3b37cd14497c3993c22b5e to your computer and use it in GitHub Desktop.
Save jwoertink/97db7318dc3b37cd14497c3993c22b5e to your computer and use it in GitHub Desktop.
Custom primary key ID for Avram
alias CustomID = String
module Avram::Migrator::Columns::PrimaryKeys
class CustomIDPrimaryKey < Avram::Migrator::Columns::PrimaryKeys::Base
def initialize(@name)
end
def column_type : String
"character varying(18)"
end
end
end
module Avram::Migrator::Columns
class CustomIDColumn < Avram::Migrator::Columns::Base
@default : String? = nil
def initialize(@name, @nilable, @default)
end
def column_type : String
"character varying(18)"
end
end
end
class SaveUser < User::SaveOperation
include CustomIDGenerator
before_save do
id.value ||= generate_new_custom_id
end
end
class User < BaseModel
skip_default_columns
table do
primary_key id : CustomID
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment