class DbSchema::Definitions::Table

Attributes

checks[R]
fields[R]
foreign_keys[R]
indexes[R]
name[R]

Public Class Methods

new(name, fields: [], indexes: [], checks: [], foreign_keys: []) click to toggle source
# File lib/db_schema/definitions/table.rb, line 7
def initialize(name, fields: [], indexes: [], checks: [], foreign_keys: [])
  @name         = name.to_sym
  @fields       = fields
  @indexes      = indexes
  @checks       = checks
  @foreign_keys = foreign_keys
end

Public Instance Methods

[](field_name)
Alias for: field
check(check_name) click to toggle source
# File lib/db_schema/definitions/table.rb, line 58
def check(check_name)
  checks.find { |check| check.name == check_name } || NullCheckConstraint.new
end
field(field_name) click to toggle source
# File lib/db_schema/definitions/table.rb, line 21
def field(field_name)
  fields.find { |field| field.name == field_name } || NullField.new
end
Also aliased as: []
foreign_key(fkey_name) click to toggle source
# File lib/db_schema/definitions/table.rb, line 66
def foreign_key(fkey_name)
  foreign_keys.find { |fkey| fkey.name == fkey_name } || NullForeignKey.new
end
has_check?(check_name) click to toggle source
# File lib/db_schema/definitions/table.rb, line 62
def has_check?(check_name)
  !check(check_name).is_a?(NullCheckConstraint)
end
has_expressions?() click to toggle source
# File lib/db_schema/definitions/table.rb, line 15
def has_expressions?
  fields.any?(&:default_is_expression?) ||
    indexes.any?(&:has_expressions?) ||
    checks.any?
end
has_field?(field_name) click to toggle source
# File lib/db_schema/definitions/table.rb, line 26
def has_field?(field_name)
  !field(field_name).is_a?(NullField)
end
has_foreign_key?(fkey_name) click to toggle source
# File lib/db_schema/definitions/table.rb, line 70
def has_foreign_key?(fkey_name)
  !foreign_key(fkey_name).is_a?(NullForeignKey)
end
has_foreign_key_to?(other_table_name) click to toggle source
# File lib/db_schema/definitions/table.rb, line 74
def has_foreign_key_to?(other_table_name)
  foreign_keys.any? { |fkey| fkey.table == other_table_name }
end
has_index?(index_name) click to toggle source
# File lib/db_schema/definitions/table.rb, line 34
def has_index?(index_name)
  !index(index_name).is_a?(NullIndex)
end
has_index_on?(*field_names) click to toggle source
# File lib/db_schema/definitions/table.rb, line 38
def has_index_on?(*field_names)
  indexes.any? do |index|
    index.columns.none?(&:expression?) && index.columns.map(&:name) == field_names
  end
end
has_primary_key?() click to toggle source
# File lib/db_schema/definitions/table.rb, line 54
def has_primary_key?
  !primary_key.is_a?(NullIndex)
end
has_unique_index_on?(*field_names) click to toggle source
# File lib/db_schema/definitions/table.rb, line 44
def has_unique_index_on?(*field_names)
  indexes.any? do |index|
    index.unique? && index.columns.none?(&:expression?) && index.columns.map(&:name) == field_names
  end
end
index(index_name) click to toggle source
# File lib/db_schema/definitions/table.rb, line 30
def index(index_name)
  indexes.find { |index| index.name == index_name } || NullIndex.new
end
primary_key() click to toggle source
# File lib/db_schema/definitions/table.rb, line 50
def primary_key
  indexes.find(&:primary?) || NullIndex.new
end
with_checks(new_checks) click to toggle source
# File lib/db_schema/definitions/table.rb, line 108
def with_checks(new_checks)
  Table.new(
    name,
    fields:       fields,
    indexes:      indexes,
    checks:       new_checks,
    foreign_keys: foreign_keys
  )
end
with_fields(new_fields) click to toggle source
# File lib/db_schema/definitions/table.rb, line 88
def with_fields(new_fields)
  Table.new(
    name,
    fields:       new_fields,
    indexes:      indexes,
    checks:       checks,
    foreign_keys: foreign_keys
  )
end
with_foreign_keys(new_foreign_keys) click to toggle source
# File lib/db_schema/definitions/table.rb, line 118
def with_foreign_keys(new_foreign_keys)
  Table.new(
    name,
    fields:       fields,
    indexes:      indexes,
    checks:       checks,
    foreign_keys: new_foreign_keys
  )
end
with_indexes(new_indexes) click to toggle source
# File lib/db_schema/definitions/table.rb, line 98
def with_indexes(new_indexes)
  Table.new(
    name,
    fields:       fields,
    indexes:      new_indexes,
    checks:       checks,
    foreign_keys: foreign_keys
  )
end
with_name(new_name) click to toggle source
# File lib/db_schema/definitions/table.rb, line 78
def with_name(new_name)
  Table.new(
    new_name,
    fields:       fields,
    indexes:      indexes,
    checks:       checks,
    foreign_keys: foreign_keys
  )
end