class Perpetuity::Postgres::Table

Attributes

attributes[R]
name[R]

Public Class Methods

new(name, attributes) click to toggle source
# File lib/perpetuity/postgres/table.rb, line 9
def initialize name, attributes
  @name = TableName.new(name)
  @attributes = attributes.to_a

  generate_id_attribute unless has_id_attribute?
end

Public Instance Methods

create_table_sql() click to toggle source
# File lib/perpetuity/postgres/table.rb, line 16
def create_table_sql
  sql = "CREATE TABLE IF NOT EXISTS #{name} ("
  sql << attributes.map(&:sql_declaration).join(', ')
  sql << ')'
end
generate_id_attribute() click to toggle source
# File lib/perpetuity/postgres/table.rb, line 26
def generate_id_attribute
  id = Attribute.new('id', Attribute::UUID, primary_key: true, default: Expression.new('uuid_generate_v4()'))
  attributes.unshift id
end
has_id_attribute?() click to toggle source
# File lib/perpetuity/postgres/table.rb, line 22
def has_id_attribute?
  attributes.any? { |attr| attr.name.to_s == 'id' }
end
to_s() click to toggle source
# File lib/perpetuity/postgres/table.rb, line 31
def to_s
  name.to_s
end