Class: DBLeftovers::DSL

Inherits:
Object
  • Object
show all
Defined in:
lib/db_leftovers/dsl.rb

Constant Summary collapse

STATUS_EXISTS =
'exists'
STATUS_CHANGED =
'changed'
STATUS_NEW =
'new'

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ DSL

Returns a new instance of DSL.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/db_leftovers/dsl.rb', line 9

def initialize(opts={})
  @verbose = !!opts[:verbose]
  @db = opts[:db_interface] || get_database_interface

  @ignored_tables = Set.new(['delayed_jobs', 'schema_migrations'].map{|x| [x.to_s, x.to_sym]}.flatten)

  @indexes_by_table = {}      # Set from the DSL
  @old_indexes = @db.lookup_all_indexes
  @new_indexes = {}
  
  @foreign_keys_by_table = {}   # Set from the DSL
  @old_foreign_keys = @db.lookup_all_foreign_keys
  @new_foreign_keys = {}

  @constraints_by_table = {}    # Set from the DSL
  @old_constraints = @db.lookup_all_constraints
  @new_constraints = {}
end

Instance Method Details

#check(table_name, constraint_name, check_expression) ⇒ Object



76
77
78
# File 'lib/db_leftovers/dsl.rb', line 76

def check(table_name, constraint_name, check_expression)
  add_constraint(Constraint.new(constraint_name, table_name, check_expression))
end

#define(&block) ⇒ Object



28
29
30
# File 'lib/db_leftovers/dsl.rb', line 28

def define(&block)
  instance_eval(&block)
end

#foreign_key(from_table, from_column = nil, to_table = nil, to_column = nil, opts = {}) ⇒ Object

foreign_key(from_table, [from_column], to_table, [to_column], [opts]):

foreign_key(:books, :publishers)                   -> foreign_key(:books, nil, :publishers, nil)
foreign_key(:books, :co_author_id, :authors)       -> foreign_key(:books, :co_author_id, :authors, nil)
foreign_key(:books, :publishers, opts)             -> foreign_key(:books, nil, :publishers, nil, opts)
foreign_key(:books, :co_author_id, :authors, opts) -> foreign_key(:books, :co_author_id, :authors, nil, opts)


54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/db_leftovers/dsl.rb', line 54

def foreign_key(from_table, from_column=nil, to_table=nil, to_column=nil, opts={})
  # First get the options hash into the right place:
  if to_column.class == Hash
    opts = to_column
    to_column = nil
  elsif to_table.class == Hash
    opts = to_table
    to_table = to_column = nil
  end

  # Sort out implicit arguments:
  if from_column and not to_table and not to_column
    to_table = from_column
    from_column = "#{to_table.to_s.singularize}_id"
    to_column = :id
  elsif from_column and to_table and not to_column
    to_column = :id
  end

  add_foreign_key(ForeignKey.new(name_constraint(from_table, from_column), from_table, from_column, to_table, to_column, opts))
end

#ignore(*table_names) ⇒ Object



32
33
34
35
36
# File 'lib/db_leftovers/dsl.rb', line 32

def ignore(*table_names)
  table_names = [table_names] unless table_names.is_a? Array
  table_names = table_names.map{|x| [x.to_s, x.to_sym]}.flatten
  @ignored_tables = Set.new(table_names)
end

#index(table_name, column_names, opts = {}) ⇒ Object



43
44
45
46
47
# File 'lib/db_leftovers/dsl.rb', line 43

def index(table_name, column_names, opts={})
  column_names = [column_names].flatten
  # puts "#{table_name}.[#{column_names.join(',')}]"
  add_index(Index.new(table_name, column_names, opts))
end

#record_constraintsObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/db_leftovers/dsl.rb', line 142

def record_constraints
  # First create any new constraints:
  @constraints_by_table.each do |table_name, chks|
    chks.each do |chk|
      next if ignore_constraint?(chk)
      case constraint_status(chk)
      when STATUS_EXISTS
        puts "Constraint already exists: #{chk.constraint_name} on #{chk.on_table}" if @verbose
      when STATUS_CHANGED
        @db.execute_drop_constraint(chk.constraint_name, chk.on_table)
        @db.execute_add_constraint(chk)
        log_new_constraint(chk, true)
      when STATUS_NEW
        @db.execute_add_constraint(chk)
        log_new_constraint(chk, false)
      end
      @new_constraints[chk.constraint_name] = chk
    end
  end

  # Now drop any old constraints that are no longer in the definition file:
  @old_constraints.each do |constraint_name, chk|
    next if ignore_constraint?(chk)
    if not @new_constraints[constraint_name]
      @db.execute_drop_constraint(constraint_name, chk.on_table)
      puts "Dropped CHECK constraint: #{constraint_name} on #{chk.on_table}"
    end
  end
end

#record_foreign_keysObject



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/db_leftovers/dsl.rb', line 112

def record_foreign_keys
  # First create any new foreign keys:
  @foreign_keys_by_table.each do |table_name, fks|
    fks.each do |fk|
      next if ignore_foreign_key?(fk)
      case foreign_key_status(fk)
      when STATUS_EXISTS
        puts "Foreign Key already exists: #{fk.constraint_name} on #{fk.from_table}" if @verbose
      when STATUS_CHANGED
        @db.execute_drop_foreign_key(fk.constraint_name, fk.from_table, fk.from_column)
        @db.execute_add_foreign_key(fk)
        puts "Dropped & re-created foreign key: #{fk.constraint_name} on #{fk.from_table}"
      when STATUS_NEW
        @db.execute_add_foreign_key(fk)
        puts "Created foreign key: #{fk.constraint_name} on #{fk.from_table}"
      end
      @new_foreign_keys[fk.constraint_name] = fk
    end
  end

  # Now drop any old foreign keys that are no longer in the definition file:
  @old_foreign_keys.each do |constraint_name, fk|
    next if ignore_foreign_key?(fk)
    if not @new_foreign_keys[constraint_name]
      @db.execute_drop_foreign_key(constraint_name, fk.from_table, fk.from_column)
      puts "Dropped foreign key: #{constraint_name} on #{fk.from_table}"
    end
  end
end

#record_indexesObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/db_leftovers/dsl.rb', line 80

def record_indexes
  # First create any new indexes:
  @indexes_by_table.each do |table_name, indexes|
    indexes.each do |idx|
      next if ignore_index?(idx)
      # puts "#{idx.table_name}.[#{idx.column_names.join(',')}]"
      case index_status(idx)
      when STATUS_EXISTS
        puts "Index already exists: #{idx.index_name} on #{idx.table_name}" if @verbose
      when STATUS_CHANGED
        @db.execute_drop_index(idx.table_name, idx.index_name)
        @db.execute_add_index(idx)
        log_new_index(idx, true)
      when STATUS_NEW
        @db.execute_add_index(idx)
        log_new_index(idx, false)
      end
      @new_indexes[idx.index_name] = table_name
    end
  end

  # Now drop any old indexes that are no longer in the definition file:
  @old_indexes.each do |index_name, idx|
    next if ignore_index?(idx)
    if not @new_indexes[index_name]
      # puts "#{index_name} #{table_name}"
      @db.execute_drop_index(idx.table_name, index_name)
      puts "Dropped index: #{index_name} on #{idx.table_name}"
    end
  end
end

#table(table_name, &block) ⇒ Object



38
39
40
41
# File 'lib/db_leftovers/dsl.rb', line 38

def table(table_name, &block)
  table_dsl = TableDSL.new(self, table_name)
  table_dsl.define(&block)
end