Module: DBNazi

Defined in:
lib/db_nazi.rb,
lib/db_nazi/table.rb,
lib/db_nazi/schema.rb,
lib/db_nazi/version.rb,
lib/db_nazi/migration.rb,
lib/db_nazi/migration_proxy.rb,
lib/db_nazi/abstract_adapter.rb,
lib/db_nazi/table_definition.rb

Defined Under Namespace

Modules: AbstractAdapter, Migration, MigrationProxy, Schema, Table, TableDefinition

Constant Summary collapse

ArgumentError =
Class.new(::ArgumentError)
NullabilityRequired =
Class.new(ArgumentError)
VarcharLimitRequired =
Class.new(ArgumentError)
IndexUniquenessRequired =
Class.new(ArgumentError)
VERSION =
[0, 0, 5]

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.enabledObject

Returns the value of attribute enabled.



16
17
18
# File 'lib/db_nazi.rb', line 16

def enabled
  @enabled
end

.from_versionObject

Returns the value of attribute from_version.



17
18
19
# File 'lib/db_nazi.rb', line 17

def from_version
  @from_version
end

.require_index_uniquenessObject

Returns the value of attribute require_index_uniqueness.



20
21
22
# File 'lib/db_nazi.rb', line 20

def require_index_uniqueness
  @require_index_uniqueness
end

.require_nullabilityObject

Returns the value of attribute require_nullability.



18
19
20
# File 'lib/db_nazi.rb', line 18

def require_nullability
  @require_nullability
end

.require_varchar_limitsObject

Returns the value of attribute require_varchar_limits.



19
20
21
# File 'lib/db_nazi.rb', line 19

def require_varchar_limits
  @require_varchar_limits
end

Class Method Details

.check_column(type, options) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
# File 'lib/db_nazi.rb', line 61

def check_column(type, options)
  if DBNazi.enabled?(:require_nullability) && type != :primary_key
    options.key?(:null) or
      raise NullabilityRequired, "[db_nazi] :null parameter required"
  end
  if DBNazi.enabled?(:require_varchar_limits)
    # AR calls #to_sym on type, so do the same here.
    type.to_sym == :string && !options.key?(:limit) and
      raise VarcharLimitRequired, "[db_nazi] string column requires :limit parameter"
  end
end

.check_index(options) ⇒ Object



73
74
75
76
77
78
# File 'lib/db_nazi.rb', line 73

def check_index(options)
  if DBNazi.enabled?(:require_index_uniqueness)
    options.key?(:unique) or
      raise IndexUniquenessRequired, "[db_nazi] :unique parameter required"
  end
end

.disableObject



36
37
38
39
40
41
42
43
44
# File 'lib/db_nazi.rb', line 36

def disable
  original_enabled = @enabled
  @enabled = false
  begin
    yield
  ensure
    @enabled = original_enabled
  end
end

.enableObject



26
27
28
29
30
31
32
33
34
# File 'lib/db_nazi.rb', line 26

def enable
  original_enabled = @enabled
  @enabled = true
  begin
    yield
  ensure
    @enabled = original_enabled
  end
end

.enabled?(setting) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/db_nazi.rb', line 22

def enabled?(setting)
  @enabled && send(setting)
end

.enabled_for_migration?(migration, version) ⇒ Boolean

Returns:

  • (Boolean)


46
47
48
49
50
51
# File 'lib/db_nazi.rb', line 46

def enabled_for_migration?(migration, version)
  return false if !@enabled
  return false if migration.nazi_disabled?
  return false if DBNazi.from_version && DBNazi.from_version > version
  true
end

.resetObject



53
54
55
56
57
58
59
# File 'lib/db_nazi.rb', line 53

def reset
  self.enabled = true
  self.from_version = nil
  self.require_nullability      = true
  self.require_varchar_limits   = true
  self.require_index_uniqueness = true
end