class DbSchema::DSL

Attributes

migrations[R]
schema[R]

Public Class Methods

new(block) click to toggle source
# File lib/db_schema/dsl.rb, line 7
def initialize(block)
  @schema     = Definitions::Schema.new
  @migrations = []

  block.call(self)
end

Public Instance Methods

enum(name, values) click to toggle source
# File lib/db_schema/dsl.rb, line 26
def enum(name, values)
  @schema.enums << Definitions::Enum.new(name.to_sym, values.map(&:to_sym))
end
extension(name) click to toggle source
# File lib/db_schema/dsl.rb, line 30
def extension(name)
  @schema.extensions << Definitions::Extension.new(name.to_sym)
end
migrate(name, &block) click to toggle source
# File lib/db_schema/dsl.rb, line 34
def migrate(name, &block)
  migrations << Migration.new(name, block).migration
end
table(name, &block) click to toggle source
# File lib/db_schema/dsl.rb, line 14
def table(name, &block)
  table_yielder = TableYielder.new(name, block)

  @schema.tables << Definitions::Table.new(
    name,
    fields:       prepare_fields(table_yielder),
    indexes:      table_yielder.indexes,
    checks:       table_yielder.checks,
    foreign_keys: table_yielder.foreign_keys
  )
end

Private Instance Methods

prepare_fields(table_yielder) click to toggle source
# File lib/db_schema/dsl.rb, line 39
def prepare_fields(table_yielder)
  primary_key = table_yielder.indexes.find(&:primary?)
  return table_yielder.fields if primary_key.nil?

  table_yielder.fields.map do |field|
    if primary_key.columns.map(&:name).include?(field.name)
      field.with_null(false)
    else
      field
    end
  end
end