Class: PgDiff::Diff

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

Instance Method Summary collapse

Constructor Details

#initialize(old_db_spec, new_db_spec) ⇒ Diff

Returns a new instance of Diff.



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/diff.rb', line 3

def initialize(old_db_spec, new_db_spec)
  @old_conn = PG::Connection.new(old_db_spec)
  @new_conn = PG::Connection.new(new_db_spec)
  @sections = [
    :domains_drop,
    :domains_create,
    :schemas_drop,
    :schemas_create,
    :tables_drop,
    :tables_change,
    :tables_create,
    :sequences_drop,
    :sequences_create,
    :views_drop,
    :views_create,
    :constraints_drop,
    :constraints_change,
    :constraints_create,
    :indices_drop,
    :indices_create,
    :functions_drop,
    :functions_create ,
    :triggers_drop,
    :triggers_create ,
    :rules_drop,
    :rules_create
  ]
  @script = {}
  @sections.each {|s| @script[s] = []}
end

Instance Method Details

#add_cnstr(tablename, cnstrname, cnstrdef) ⇒ Object



309
310
311
312
313
314
315
316
317
# File 'lib/diff.rb', line 309

def add_cnstr(tablename, cnstrname, cnstrdef)
  c_string = "ALTER TABLE #{tablename} ADD CONSTRAINT #{cnstrname} #{cnstrdef} ;"
  case cnstrdef
    when /^CHECK /   then @c_check  << c_string
    when /^PRIMARY / then @c_primary << c_string
    when /^FOREIGN / then @c_foreign << c_string
    when /^UNIQUE /  then @c_unique  << c_string
  end
end

#add_script(section, statement) ⇒ Object



51
52
53
# File 'lib/diff.rb', line 51

def add_script(section, statement)
  @script[section] << statement
end

#compare_domainsObject



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/diff.rb', line 64

def compare_domains
  @old_database.domains.keys.each do |name|
    add_script(:domains_drop ,  "DROP DOMAIN #{name} CASCADE;") unless @new_database.domains.has_key?(name)
  end
  @new_database.domains.each do |name, df|
    add_script(:domains_create ,  "CREATE DOMAIN #{name} AS #{df};") unless @old_database.domains.has_key?(name)
    old_domain = @old_database.domains[name]
    if old_domain && old_domain != df
       add_script(:domains_drop, "DROP DOMAIN #{name} CASCADE;")
       add_script(:domains_create,  "-- [changed domain] :")
       add_script(:domains_create,  "-- OLD: #{old_domain}")
       add_script(:domains_create,  "CREATE DOMAIN #{name} AS #{df};")
    end
  end
end

#compare_functionsObject



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/diff.rb', line 89

def compare_functions
  @old_database.functions.keys.each do |name|
    add_script(:functions_drop ,  "DROP FUNCTION #{name} CASCADE;") unless @new_database.functions.has_key?(name)
  end
  @new_database.functions.each do |name, func|
    add_script(:functions_create ,   func.definition) unless @old_database.functions.has_key?(name)
    old_function = @old_database.functions[name]
    if old_function && old_function.definition != func.definition
      add_script(:functions_create , '-- [changed function] :')
      add_script(:functions_create , '-- OLD :')
      add_script(:functions_create ,  old_function.definition.gsub(/^/, "-->  ") )
      add_script(:functions_create ,   func.definition)
    end
  end
end

#compare_rules_createObject



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/diff.rb', line 111

def compare_rules_create
  @new_database.rules.each do |name, rule|
    add_script(:rules_create ,   rule.definition) unless @old_database.rules.has_key?(name)
    old_rule = @old_database.rules[name]
    if old_rule && old_rule != rule
      add_script(:rules_drop ,  "DROP RULE #{rule.name} ON #{rule.table_name} CASCADE;")
      add_script(:rules_create ,  "-- [changed rule] :")
      add_script(:rules_create ,  "-- OLD: #{old_rule.definition}")
      add_script(:rules_create ,   rule.definition )
    end
  end
end

#compare_rules_dropObject



105
106
107
108
109
# File 'lib/diff.rb', line 105

def compare_rules_drop
  @old_database.rules.each do |name, rule|
    add_script(:rules_drop ,  "DROP RULE #{rule.name} ON #{rule.table_name} CASCADE;") unless @new_database.rules.has_key?(name)
  end
end

#compare_schemasObject



55
56
57
58
59
60
61
62
# File 'lib/diff.rb', line 55

def compare_schemas
  @old_database.schemas.keys.each do |name|
    add_script(:schemas_drop ,  "DROP SCHEMA #{name};") unless @new_database.schemas.has_key?(name)
  end
  @new_database.schemas.keys.each do |name|
    add_script(:schemas_create ,  "CREATE SCHEMA #{name};") unless @old_database.schemas.has_key?(name)
  end
end

#compare_sequencesObject



80
81
82
83
84
85
86
87
# File 'lib/diff.rb', line 80

def compare_sequences
  @old_database.sequences.keys.each do |name|
    add_script(:sequences_drop ,  "DROP SEQUENCE #{name} CASCADE;") unless @new_database.sequences.has_key?(name)
  end
  @new_database.sequences.keys.each do |name|
    add_script(:sequences_create ,  "CREATE SEQUENCE #{name};") unless @old_database.sequences.has_key?(name)
  end
end

#compare_table_attrsObject



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
# File 'lib/diff.rb', line 162

def compare_table_attrs
  @old_database.tables.each do |name, table|
    add_script(:tables_drop, "DROP TABLE #{name} CASCADE;") unless @new_database.tables.has_key?(name)
  end
  @to_compare = []
  @new_database.tables.each do |name, table|
    unless @old_database.tables.has_key?(name)
      add_script(:tables_create ,  table.table_creation)
      add_script(:indices_create ,  table.index_creation) unless table.indexes.empty?
      @to_compare << name
    else
      diff_attributes(@old_database.tables[name], table)
      diff_indexes(@old_database.tables[name], table)
      @to_compare << name
    end
  end
end

#compare_table_constraintsObject



180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/diff.rb', line 180

def compare_table_constraints
  @c_check = []
  @c_primary = []
  @c_unique = []
  @c_foreign = []
  @to_compare.each do |name|
    if @old_database.tables[name]
      diff_constraints(@old_database.tables[name], @new_database.tables[name])
    else
      @new_database.tables[name].constraints.each do |cname, cdef|
        add_cnstr(name,  cname, cdef)
      end
    end
  end
  @script[:constraints_create] += @c_check
  @script[:constraints_create] += @c_primary
  @script[:constraints_create] += @c_unique
  @script[:constraints_create] += @c_foreign
end

#compare_triggers_createObject



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/diff.rb', line 130

def compare_triggers_create
  @new_database.triggers.each do |name, trigger|
    add_script(:triggers_create ,   trigger.definition) unless @old_database.triggers.has_key?(name)
    old_trigger = @old_database.triggers[name]
    if old_trigger && old_trigger != trigger
      add_script(:triggers_drop ,  "DROP trigger #{trigger.name} ON #{trigger.table_name} CASCADE;")
      add_script(:triggers_create ,  "-- [changed trigger] :")
      add_script(:triggers_create ,  "-- OLD #{old_trigger.definition}")
      add_script(:triggers_create ,   trigger.definition)
    end
  end
end

#compare_triggers_dropObject



124
125
126
127
128
# File 'lib/diff.rb', line 124

def compare_triggers_drop
  @old_database.triggers.each do |name, trigger|
    add_script(:triggers_drop ,  "DROP trigger #{trigger.name} ON #{trigger.table_name} CASCADE;") unless @new_database.triggers.has_key?(name)
  end
end

#compare_views_createObject



149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/diff.rb', line 149

def compare_views_create
  @new_database.views.each do |name, df|
    add_script(:views_create ,   df.definition) unless @old_database.views.has_key?(name)
    old_view = @old_database.views[name]
    if old_view && df.definition != old_view.definition
      add_script(:views_drop ,  "DROP VIEW #{name};")
      add_script(:views_create ,  "-- [changed view] :")
      add_script(:views_create ,  "-- #{old_view.definition.gsub(/\n/, ' ')}")
      add_script(:views_create ,  df.definition)
    end
  end
end

#compare_views_dropObject



143
144
145
146
147
# File 'lib/diff.rb', line 143

def compare_views_drop
  @old_database.views.keys.each do |name|
    add_script(:views_drop ,  "DROP VIEW #{name};") unless @new_database.views.has_key?(name)
  end
end

#diff_attributes(old_table, new_table) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# File 'lib/diff.rb', line 214

def diff_attributes(old_table, new_table)
  dropped = []
  added   = []
  changed = []

  order = []
  old_table.attributes.keys.each do |attname|
    if new_table.has_attribute?(attname)
      changed << attname if old_table.attributes[attname] != new_table.attributes[attname]
    else
      dropped << attname
    end
  end

  old_table.attributes.keys.each do |attname|
    if new_table.has_attribute?(attname)
      old_index = old_table.attribute_index(attname)
      new_index = new_table.attribute_index(attname)
      order << attname if old_index != new_index
    end
  end
  new_table.attributes.keys.each do |attname|
    added << attname unless old_table.has_attribute?(attname)
  end
  add_script(:tables_change ,  "--  [#{old_table.name}] dropped attributes") unless dropped.empty?
  dropped.each do |attname|
    add_script(:tables_change ,  "ALTER TABLE #{old_table.name} DROP COLUMN #{attname} CASCADE;")
  end
  add_script(:tables_change ,  "--  [#{old_table.name}] added attributes") unless added.empty?
  added.each do |attname|
    add_script(:tables_change ,  "ALTER TABLE #{old_table.name} ADD COLUMN #{new_table.attributes[attname].definition};")
  end
  add_script(:tables_change ,  "--  [#{old_table.name}] changed attributes") unless changed.empty?
  changed.each do |attname|
    old_att = old_table.attributes[attname]
    new_att = new_table.attributes[attname]
    add_script(:tables_change ,  "-- attribute: #{attname}")
    add_script(:tables_change ,  "-- OLD : #{old_att.definition}")
    add_script(:tables_change ,  "-- NEW : #{new_att.definition}")
    if old_att.type_def != new_att.type_def
      add_script(:tables_change ,  "ALTER TABLE #{old_table.name} ALTER COLUMN #{attname} TYPE #{new_att.type_def};")
    end
    if old_att.default != new_att.default
      if new_att.default.nil?
        add_script(:tables_change ,  "ALTER TABLE #{old_table.name} ALTER COLUMN #{attname} DROP DEFAULT;")
      else
        add_script(:tables_change ,  "ALTER TABLE #{old_table.name} ALTER COLUMN #{attname} SET DEFAULT #{new_att.default};")
      end
    end
    if old_att.notnull != new_att.notnull
      add_script(:tables_change ,  "ALTER TABLE #{old_table.name} ALTER COLUMN #{attname} #{new_att.notnull ? 'SET' : 'DROP'} NOT NULL;")
    end
  end

  add_script(:tables_change ,   "--  [#{old_table.name}] attribute order changed") unless order.empty?
  order.each do |attname|
    add_script(:tables_change , "     #{attname}.  Old index: #{old_table.attribute_index(attname)}, New index: #{new_table.attribute_index(attname)}")
  end
end

#diff_constraints(old_table, new_table) ⇒ Object



274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'lib/diff.rb', line 274

def diff_constraints(old_table, new_table)
  dropped = []
  added   = []
  changed = []

  old_table.constraints.keys.each do |conname|
    if new_table.has_constraint?(conname)
      if old_table.constraints[conname] != new_table.constraints[conname]
        changed << conname
      end
    else
      dropped << conname
    end
  end

  new_table.constraints.keys.each do |conname|
    added << conname unless old_table.has_constraint?(conname)
  end

  dropped.each do |name|
    add_script(:constraints_drop ,  "ALTER TABLE #{old_table.name} DROP CONSTRAINT #{name};")
  end

  added.each do |name|
    add_cnstr(old_table.name,  name, new_table.constraints[name])
  end

  changed.each do |name|
    add_script(:constraints_change,
               "-- Previous: #{old_table.constraints[name]}\n" +
               "ALTER TABLE #{old_table.name} DROP CONSTRAINT #{name};\n" +
               "ALTER TABLE #{new_table.name} ADD CONSTRAINT #{name} #{new_table.constraints[name]};\n")
  end
end

#diff_indexes(old_table, new_table) ⇒ Object



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'lib/diff.rb', line 319

def diff_indexes(old_table, new_table)
  dropped = []
  added   = []

  old_table.indexes.keys.each do |name|
    if new_table.has_index?(name)
      if old_table.indexes[name] != new_table.indexes[name]
        dropped << name
        added << name
      end
    else
      dropped << name
    end
  end
  new_table.indexes.keys.each do |name|
    added << name unless old_table.has_index?(name)
  end

  dropped.each do |name|
    add_script(:indices_drop ,  "DROP INDEX #{name};")
  end
  added.each do |name|
    add_script(:indices_create ,  (new_table.indexes[name] + ";")) if new_table.indexes[name]
  end
end

#outputObject



200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/diff.rb', line 200

def output
  out = []
  @sections.each do |sect|
    if @script[sect].empty?
       out << "-- [SKIP SECTION : #{sect.to_s.upcase}] : no changes\n"
    else
       out << "-- [START SECTION : #{sect.to_s.upcase}]"
       out += @script[sect]
       out << "-- [END SECTION : #{sect.to_s.upcase}]\n"
    end
  end
  out.join("\n")
end

#run_compareObject



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/diff.rb', line 34

def run_compare
  @old_database = Database.new(@old_conn)
  @new_database = Database.new(@new_conn)
  compare_schemas
  compare_domains
  compare_sequences
  compare_triggers_drop
  compare_rules_drop
  compare_views_drop
  compare_table_attrs
  compare_views_create
  compare_functions
  compare_rules_create
  compare_triggers_create
  compare_table_constraints
end