Module: DynamicMigrations::Postgres::Server::Database::Schema::Table::UniqueConstraints

Included in:
DynamicMigrations::Postgres::Server::Database::Schema::Table
Defined in:
lib/dynamic_migrations/postgres/server/database/schema/table/unique_constraints.rb

Overview

This module has all the tables methods for working with unique_constraints

Defined Under Namespace

Classes: UniqueConstraintAlreadyExistsError, UniqueConstraintDoesNotExistError

Instance Method Summary collapse

Instance Method Details

#add_unique_constraint(name, column_names, **unique_constraint_options) ⇒ Object

adds a new unique_constraint to this table, and returns it



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/unique_constraints.rb', line 41

def add_unique_constraint name, column_names, **unique_constraint_options
  if has_unique_constraint? name
    raise(UniqueConstraintAlreadyExistsError, "unique_constraint #{name} already exists")
  end
  columns = column_names.map { |column_name| column column_name }
  included_target = self
  if included_target.is_a? Table
    new_unique_constraint = @unique_constraints[name] = UniqueConstraint.new source, included_target, columns, name, **unique_constraint_options
  else
    raise ModuleIncludedIntoUnexpectedTargetError, included_target
  end
  # sort the hash so that the unique_constraints are in alphabetical order by name
  sorted_unique_constraints = {}
  @unique_constraints.keys.sort.each do |name|
    sorted_unique_constraints[name] = @unique_constraints[name]
  end
  @unique_constraints = sorted_unique_constraints
  # return the new unique_constraint
  new_unique_constraint
end

#has_unique_constraint?(name) ⇒ Boolean

returns true if this table has a unique_constraint with the provided name, otherwise false

Returns:

  • (Boolean)

Raises:



26
27
28
29
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/unique_constraints.rb', line 26

def has_unique_constraint? name
  raise ExpectedSymbolError, name unless name.is_a? Symbol
  @unique_constraints.key? name
end

#unique_constraint(name) ⇒ Object

returns the unique_constraint object for the provided unique_constraint name, and raises an error if the unique_constraint does not exist



19
20
21
22
23
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/unique_constraints.rb', line 19

def unique_constraint name
  raise ExpectedSymbolError, name unless name.is_a? Symbol
  raise UniqueConstraintDoesNotExistError unless has_unique_constraint? name
  @unique_constraints[name]
end

#unique_constraintsObject

returns an array of this tables unique_constraints



32
33
34
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/unique_constraints.rb', line 32

def unique_constraints
  @unique_constraints.values
end

#unique_constraints_hashObject



36
37
38
# File 'lib/dynamic_migrations/postgres/server/database/schema/table/unique_constraints.rb', line 36

def unique_constraints_hash
  @unique_constraints
end