Module: DbAgile::Core::Schema

Extended by:
Robustness
Defined in:
lib/dbagile/core/schema.rb,
lib/dbagile/core/schema/part.rb,
lib/dbagile/core/schema/builder.rb,
lib/dbagile/core/schema/logical.rb,
lib/dbagile/core/schema/physical.rb,
lib/dbagile/core/schema/composite.rb,
lib/dbagile/core/schema/robustness.rb,
lib/dbagile/core/schema/schema_object.rb,
lib/dbagile/core/schema/logical/relvar.rb,
lib/dbagile/core/schema/migrate/stager.rb,
lib/dbagile/core/schema/physical/index.rb,
lib/dbagile/core/schema/database_schema.rb,
lib/dbagile/core/schema/logical/heading.rb,
lib/dbagile/core/schema/logical/relview.rb,
lib/dbagile/core/schema/builder/coercion.rb,
lib/dbagile/core/schema/physical/indexes.rb,
lib/dbagile/core/schema/logical/attribute.rb,
lib/dbagile/core/schema/migrate/drop_view.rb,
lib/dbagile/core/schema/migrate/operation.rb,
lib/dbagile/core/schema/computations/merge.rb,
lib/dbagile/core/schema/computations/minus.rb,
lib/dbagile/core/schema/computations/split.rb,
lib/dbagile/core/schema/logical/constraint.rb,
lib/dbagile/core/schema/migrate/drop_table.rb,
lib/dbagile/core/schema/computations/filter.rb,
lib/dbagile/core/schema/logical/constraints.rb,
lib/dbagile/core/schema/migrate/create_view.rb,
lib/dbagile/core/schema/migrate/create_table.rb,
lib/dbagile/core/schema/migrate/expand_table.rb,
lib/dbagile/core/schema/migrate/collapse_table.rb,
lib/dbagile/core/schema/builder/concept_factory.rb,
lib/dbagile/core/schema/migrate/abstract_script.rb,
lib/dbagile/core/schema/logical/constraint/foreign_key.rb,
lib/dbagile/core/schema/logical/constraint/candidate_key.rb

Defined Under Namespace

Modules: Computations, Migrate, Robustness Classes: Builder, Composite, DatabaseSchema, Logical, Part, Physical, SchemaObject

Constant Summary collapse

EMPTY_SCHEMA =

An empty schema

Schema::DatabaseSchema.new
TO_CREATE =

Status of objects that need to be created (on left)

:to_create
TO_DROP =

Status of objects that need to be dropped (on left)

:to_drop
TO_ALTER =

Status of objects that need to be altered (on left)

:to_alter
NO_CHANGE =

Status of objects that need not being altered in any way

:no_change
CREATED =

Status of objects that have been created

:created
DROPPED =

Status of objects that have been dropped

:dropped
ALTERED =

Status of objects that have been altered

:altered
PENDING =

Status of objects whose migrayion is currently pending

:pending
DEFERED =

Status of objects whose migration has been defered

:defered
STATUS_TO_COLOR =
{
  TO_CREATE => :green,
  TO_DROP   => :red,
  TO_ALTER  => :cyan,
  NO_CHANGE => :black,
  #
  CREATED   => :green,
  DROPPED   => :green,
  ALTERED   => :green,
  PENDING   => :red,
  DEFERED   => :red
}
DEFAULT_CONFLICT_RESOLVER_BLOCK =

Resolver block that raises a SchemaConflictError

lambda{|left,right|
  raise SchemaConflictError.new(left, right)
}

Class Method Summary collapse

Methods included from Robustness

builder!, hash!, schema!

Class Method Details

.builder(schema = nil) ⇒ Object

Creates a builder instance



78
79
80
# File 'lib/dbagile/core/schema.rb', line 78

def builder(schema = nil)
  DbAgile::Core::Schema::Builder.new(schema)
end

.create_script(schema) ⇒ Migrate::AbstractScript

Computes a create abstract script for a given schema.

Parameters:

Returns:



236
237
238
# File 'lib/dbagile/core/schema.rb', line 236

def create_script(schema)
  stage_script(EMPTY_SCHEMA + schema)
end

.drop_script(schema) ⇒ Migrate::AbstractScript

Computes a drop abstract script for a given schema.

Parameters:

Returns:



247
248
249
# File 'lib/dbagile/core/schema.rb', line 247

def drop_script(schema)
  stage_script(schema + EMPTY_SCHEMA)
end

.filter(schema, options = {}, builder = DbAgile::Core::Schema::builder, &filter_block) ⇒ Object

Filters a schema according to a block



187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/dbagile/core/schema.rb', line 187

def filter(schema, options = {}, 
           builder = DbAgile::Core::Schema::builder, 
           &filter_block)
  schema!(schema, :schema, caller)
  hash!(options, :options, caller)
  builder!(builder, :builder, caller)
  options = Computations::Filter::DEFAULT_OPTIONS.merge(options)
  filtered = Schema::Computations::filter(schema, options, builder, &filter_block)._strip!
  if options[:identifier]
    filtered.schema_identifier = options[:identifier]
  end
  filtered
end

.merge(left, right, builder = DbAgile::Core::Schema::builder, &conflict_resolver) ⇒ Object

Computes a new schema by merging two other schemas.

Parameters:



171
172
173
174
175
176
177
178
179
180
181
# File 'lib/dbagile/core/schema.rb', line 171

def merge(left, right, 
          builder = DbAgile::Core::Schema::builder, 
          &conflict_resolver)
  schema!(left, :left, caller)
  schema!(right, :right, caller)
  builder!(builder, :builder, caller)
  if conflict_resolver.nil?
    conflict_resolver = DEFAULT_CONFLICT_RESOLVER_BLOCK
  end
  Computations::merge(left, right, builder, &conflict_resolver)
end

.minus(left, right, builder = DbAgile::Core::Schema::builder) ⇒ Object

Computes a new schema by difference.

Parameters:



153
154
155
156
157
158
159
# File 'lib/dbagile/core/schema.rb', line 153

def minus(left, right, 
          builder = DbAgile::Core::Schema::builder)
  schema!(left, :left, caller)
  schema!(right, :right, caller)
  builder!(builder, :builder, caller)
  Computations::minus(left, right, builder)
end

.new(schema_identifier = nil) ⇒ DatabaseSchema

Creates a DatabaseSchema instance.

Intent of this method is to hide implementation details of this package. It should ALWAYS be used for creating schema instances (no direct accesses to the DatabaseSchema should be made!).

Parameters:

  • optional (Object)

    tracability identifier

Returns:



65
66
67
# File 'lib/dbagile/core/schema.rb', line 65

def new(schema_identifier = nil)
  Schema::DatabaseSchema.new(schema_identifier)
end

.split(schema, options = {}, &filter_block) ⇒ Object

Splits a schema according to a block



205
206
207
208
209
210
# File 'lib/dbagile/core/schema.rb', line 205

def split(schema, options = {}, &filter_block)
  schema!(schema, :schema, caller)
  hash!(options, :options, caller)
  options = Computations::Split::DEFAULT_OPTIONS.merge(options)
  Schema::Computations::split(schema, options, &filter_block)
end

.stage_script(schema, options = Migrate::Stager::DEFAULT_OPTIONS) ⇒ Migrate::AbstractScript

Computes and returns a list of abstract operations to perform on a database given a annotated schema (typically, the result of a merge operation)

Parameters:

Returns:



225
226
227
# File 'lib/dbagile/core/schema.rb', line 225

def stage_script(schema, options = Migrate::Stager::DEFAULT_OPTIONS)
  Migrate::Stager.new.run(schema, options)
end

.yaml_builder(schema = Schema.new) ⇒ Object

Factors a Builder instance ready for loading a yaml schema file



86
87
88
# File 'lib/dbagile/core/schema.rb', line 86

def yaml_builder(schema = Schema.new)
  DbAgile::Core::Schema::Builder.new(schema)
end

.yaml_file_load(path_or_io, builder = yaml_builder) ⇒ Object

Loads a schema from a YAML file

Parameters:



124
125
126
127
128
129
130
131
132
133
# File 'lib/dbagile/core/schema.rb', line 124

def yaml_file_load(path_or_io, builder = yaml_builder)
  case path_or_io
    when String
      File.open(path_or_io, 'r'){|io| yaml_load(io, builder) }
    when IO, File, Tempfile
      yaml_load(path_or_io, builder)
    else 
      raise ArgumentError, "Unable to load schema from #{path_or_io}"
  end
end

.yaml_load(str, builder = yaml_builder) ⇒ Object

Loads a database schema from a YAML string

Parameters:



105
106
107
108
109
110
111
112
# File 'lib/dbagile/core/schema.rb', line 105

def yaml_load(str, builder = yaml_builder)
  YAML::each_document(str){|doc|
    builder._natural(doc)
  }
  builder._dump
rescue SByC::TypeSystem::CoercionError => ex
  raise DbAgile::SchemaSyntaxError, "Syntax error in schema: #{ex.message}"
end