Module: PgDelta

Defined in:
lib/pg_delta.rb,
lib/pg_delta/version.rb

Defined Under Namespace

Modules: Version Classes: Error

Class Method Summary collapse

Class Method Details

.alter_domain(statement) ⇒ Object

Domain mutations



266
267
268
269
270
271
272
273
274
# File 'lib/pg_delta.rb', line 266

def self.alter_domain(statement)
  {
    mutation: :alter_domain,
    type: :domain,
    details: {
      domain_name: statement.type_name.first.string.sval
    }
  }
end

.alter_enum(statement) ⇒ Object

Enum mutations



298
299
300
301
302
303
304
305
306
# File 'lib/pg_delta.rb', line 298

def self.alter_enum(statement)
  {
    mutation: :alter_enum,
    type: :enum,
    details: {
      enum_name: statement.type_name.first.string.sval
    }
  }
end

.alter_table_add_column(statement) ⇒ Object

Column mutations



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/pg_delta.rb', line 182

def self.alter_table_add_column(statement)
  {
    mutation: :alter_table_add_column,
    type: :column,
    details: {
      table_name: statement.relation.relname,
      column_name: statement.cmds.first.alter_table_cmd.def.column_def.colname,
      column_type: statement.cmds.first.alter_table_cmd.def.column_def.type_name.names.last.string.sval
    }
  }
end

.alter_table_add_constraint(statement) ⇒ Object

Constraint mutations



599
600
601
602
603
604
605
606
607
608
# File 'lib/pg_delta.rb', line 599

def self.alter_table_add_constraint(statement)
  {
    mutation: :alter_table_add_constraint,
    type: :constraint,
    details: {
      table_name: statement.relation.relname,
      constraint_name: statement.cmds.first.alter_table_cmd.def.constraint.conname
    }
  }
end

.alter_table_alter_column_type(statement) ⇒ Object

Type mutations



532
533
534
535
536
537
538
539
540
541
542
# File 'lib/pg_delta.rb', line 532

def self.alter_table_alter_column_type(statement)
  {
    mutation: :alter_table_alter_column_type,
    type: :column,
    details: {
      table_name: statement.relation.relname,
      column_name: statement.cmds.first.alter_table_cmd.name,
      new_type_name: statement.cmds.first.alter_table_cmd.def.column_def.type_name.names.last.string.sval
    }
  }
end

.alter_table_column_default(statement) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/pg_delta.rb', line 194

def self.alter_table_column_default(statement)
  # PgQuery sadly doesn't differentiate between adding and dropping
  # column defaults at the node level, so we have to do it here.
  if statement.cmds.first.alter_table_cmd.def
    # The query is setting a default.
    {
      mutation: :alter_table_set_column_default,
      type: :column,
      details: {
        table_name: statement.relation.relname,
        column_name: statement.cmds.first.alter_table_cmd.name
      }
    }
  else
    {
      mutation: :alter_table_drop_column_default,
      type: :column,
      details: {
        table_name: statement.relation.relname,
        column_name: statement.cmds.first.alter_table_cmd.name
      }
    }
  end
end

.alter_table_disable_trigger(statement) ⇒ Object



429
430
431
432
433
434
435
436
437
438
# File 'lib/pg_delta.rb', line 429

def self.alter_table_disable_trigger(statement)
  {
    mutation: :disable_trigger,
    type: :trigger,
    details: {
      table_name: statement.relation.relname,
      trigger_name: statement.cmds.first.alter_table_cmd.name
    }
  }
end

.alter_table_drop_column(statement) ⇒ Object



219
220
221
222
223
224
225
226
227
228
# File 'lib/pg_delta.rb', line 219

def self.alter_table_drop_column(statement)
  {
    mutation: :alter_table_drop_column,
    type: :column,
    details: {
      table_name: statement.relation.relname,
      column_name: statement.cmds.first.alter_table_cmd.name
    }
  }
end

.alter_table_drop_constraint(statement) ⇒ Object



610
611
612
613
614
615
616
617
618
619
# File 'lib/pg_delta.rb', line 610

def self.alter_table_drop_constraint(statement)
  {
    mutation: :alter_table_drop_constraint,
    type: :constraint,
    details: {
      table_name: statement.relation.relname,
      constraint_name: statement.cmds.first.alter_table_cmd.name
    }
  }
end

.alter_table_drop_not_null(statement) ⇒ Object



230
231
232
233
234
235
236
237
238
239
# File 'lib/pg_delta.rb', line 230

def self.alter_table_drop_not_null(statement)
  {
    mutation: :alter_table_drop_not_null,
    type: :column,
    details: {
      table_name: statement.relation.relname,
      column_name: statement.cmds.first.alter_table_cmd.name
    }
  }
end

.alter_table_enable_trigger(statement) ⇒ Object



440
441
442
443
444
445
446
447
448
449
# File 'lib/pg_delta.rb', line 440

def self.alter_table_enable_trigger(statement)
  {
    mutation: :enable_trigger,
    type: :trigger,
    details: {
      table_name: statement.relation.relname,
      trigger_name: statement.cmds.first.alter_table_cmd.name
    }
  }
end

.alter_table_set_not_null(statement) ⇒ Object



241
242
243
244
245
246
247
248
249
250
# File 'lib/pg_delta.rb', line 241

def self.alter_table_set_not_null(statement)
  {
    mutation: :alter_table_set_not_null,
    type: :column,
    details: {
      table_name: statement.relation.relname,
      column_name: statement.cmds.first.alter_table_cmd.name
    }
  }
end

.alter_table_validate_constraint(statement) ⇒ Object



621
622
623
624
625
626
627
628
629
630
# File 'lib/pg_delta.rb', line 621

def self.alter_table_validate_constraint(statement)
  {
    mutation: :alter_table_validate_constraint,
    type: :constraint,
    details: {
      table_name: statement.relation.relname,
      constraint_name: statement.cmds.first.alter_table_cmd.name
    }
  }
end

.create_domain(statement) ⇒ Object



276
277
278
279
280
281
282
283
284
# File 'lib/pg_delta.rb', line 276

def self.create_domain(statement)
  {
    mutation: :create_domain,
    type: :domain,
    details: {
      domain_name: statement.domainname.first.string.sval
    }
  }
end

.create_enum(statement) ⇒ Object



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

def self.create_enum(statement)
  {
    mutation: :create_enum,
    type: :enum,
    details: {
      enum_name: statement.type_name.first.string.sval
    }
  }
end

.create_function(statement) ⇒ Object

Function mutations



320
321
322
323
324
325
326
327
328
# File 'lib/pg_delta.rb', line 320

def self.create_function(statement)
  {
    mutation: :create_function,
    type: :function,
    details: {
      function_name: statement.funcname.first.string.sval
    }
  }
end

.create_index(statement) ⇒ Object

Index mutations



498
499
500
501
502
503
504
505
506
507
# File 'lib/pg_delta.rb', line 498

def self.create_index(statement)
  {
    mutation: :create_index,
    type: :index,
    details: {
      table_name: statement.relation.relname,
      column_names: statement.index_params.map(&:index_elem).map(&:name)
    }
  }
end

.create_table(statement) ⇒ Object

Table mutations



353
354
355
356
357
358
359
360
361
# File 'lib/pg_delta.rb', line 353

def self.create_table(statement)
  {
    mutation: :create_table,
    type: :table,
    details: {
      table_name: statement.relation.relname
    }
  }
end

.create_table_as(statement) ⇒ Object



363
364
365
366
367
368
369
370
371
# File 'lib/pg_delta.rb', line 363

def self.create_table_as(statement)
  {
    mutation: :create_table_as,
    type: :table,
    details: {
      table_name: statement.into.rel.relname
    }
  }
end

.create_trigger(statement) ⇒ Object

Trigger mutations



406
407
408
409
410
411
412
413
414
415
# File 'lib/pg_delta.rb', line 406

def self.create_trigger(statement)
  {
    mutation: :create_trigger,
    type: :trigger,
    details: {
      trigger_name: statement.trigname,
      table_name: statement.relation.relname
    }
  }
end

.delete(statement) ⇒ Object

Data mutations



465
466
467
468
469
470
471
472
473
# File 'lib/pg_delta.rb', line 465

def self.delete(statement)
  {
    mutation: :delete_data,
    type: :data,
    details: {
      table_name: statement.relation.relname
    }
  }
end

.drop_domain(statement) ⇒ Object



286
287
288
289
290
291
292
293
294
# File 'lib/pg_delta.rb', line 286

def self.drop_domain(statement)
  {
    mutation: :drop_domain,
    type: :domain,
    details: {
      domain_name: statement.objects.first.type_name.names.first.string.sval
    }
  }
end

.drop_function(statement) ⇒ Object



330
331
332
333
334
335
336
337
338
# File 'lib/pg_delta.rb', line 330

def self.drop_function(statement)
  {
    mutation: :drop_function,
    type: :function,
    details: {
      function_name: statement.objects.first.object_with_args.objname.first.string.sval
    }
  }
end

.drop_index(statement) ⇒ Object



509
510
511
512
513
514
515
516
517
# File 'lib/pg_delta.rb', line 509

def self.drop_index(statement)
  {
    mutation: :drop_index,
    type: :index,
    details: {
      index_name: statement.objects.first.list.items.first.string.sval
    }
  }
end

.drop_materialized_view(statement) ⇒ Object



577
578
579
580
581
582
583
584
585
# File 'lib/pg_delta.rb', line 577

def self.drop_materialized_view(statement)
  {
    mutation: :drop_materialized_view,
    type: :view,
    details: {
      view_name: statement.objects.first.list.items.first.string.sval
    }
  }
end

.drop_table(statement) ⇒ Object



373
374
375
376
377
378
379
380
381
# File 'lib/pg_delta.rb', line 373

def self.drop_table(statement)
  {
    mutation: :drop_table,
    type: :table,
    details: {
      table_name: statement.objects.first.list.items.first.string.sval
    }
  }
end

.drop_trigger(statement) ⇒ Object



417
418
419
420
421
422
423
424
425
426
427
# File 'lib/pg_delta.rb', line 417

def self.drop_trigger(statement)
  items = statement.objects.first.list.items
  {
    mutation: :drop_trigger,
    type: :trigger,
    details: {
      trigger_name: items.last.string.sval,
      table_name: items.first.string.sval
    }
  }
end

.drop_type(statement) ⇒ Object



544
545
546
547
548
549
550
551
552
# File 'lib/pg_delta.rb', line 544

def self.drop_type(statement)
  {
    mutation: :drop_type,
    type: :type,
    details: {
      type_name: statement.objects.first.type_name.names.first.string.sval
    }
  }
end

.drop_view(statement) ⇒ Object



587
588
589
590
591
592
593
594
595
# File 'lib/pg_delta.rb', line 587

def self.drop_view(statement)
  {
    mutation: :drop_view,
    type: :view,
    details: {
      view_name: statement.objects.first.list.items.first.string.sval
    }
  }
end

.insert(statement) ⇒ Object



475
476
477
478
479
480
481
482
483
# File 'lib/pg_delta.rb', line 475

def self.insert(statement)
  {
    mutation: :insert_data,
    type: :data,
    details: {
      table_name: statement.relation.relname
    }
  }
end

.parse_query(query) ⇒ Hash

Parses a single query and its corresponding PgQuery statement. Returns an empty hash if the query cannot be parsed.

Parameters:

  • query (String)

    The query

Returns:

  • (Hash)

    The info about the parsed query



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

def self.parse_query(query)
  parsed_query = PgQuery.parse query
  statement = parsed_query.tree.stmts.first.stmt
  parse_statement statement
rescue
  {}
end

.parse_statement(statement) ⇒ Hash

Returns information about the supplied statement.

Parameters:

  • statement (PgQuery::Node)

    The statement

Returns:

  • (Hash)

    The info about the statement



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
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
111
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
141
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
171
172
173
174
175
176
177
178
# File 'lib/pg_delta.rb', line 39

def self.parse_statement(statement)
  case statement.node
  when :alter_domain_stmt
    alter_domain statement.alter_domain_stmt
  when :alter_enum_stmt
    alter_enum statement.alter_enum_stmt
  when :alter_table_stmt
    case statement.alter_table_stmt.cmds[0].alter_table_cmd.subtype
    when :AT_SetNotNull
      alter_table_set_not_null statement.alter_table_stmt
    when :AT_AddColumn
      alter_table_add_column statement.alter_table_stmt
    when :AT_DropColumn
      alter_table_drop_column statement.alter_table_stmt
    when :AT_AddConstraint
      alter_table_add_constraint statement.alter_table_stmt
    when :AT_DropConstraint
      alter_table_drop_constraint statement.alter_table_stmt
    when :AT_ValidateConstraint
      alter_table_validate_constraint statement.alter_table_stmt
    when :AT_DropNotNull
      alter_table_drop_not_null statement.alter_table_stmt
    when :AT_ColumnDefault
      alter_table_column_default statement.alter_table_stmt
    when :AT_AlterColumnType
      alter_table_alter_column_type statement.alter_table_stmt
    when :AT_DisableTrig
      alter_table_disable_trigger statement.alter_table_stmt
    when :AT_SetRelOptions
      # Don't return anything for now.
      {}
    when :AT_EnableTrig
      alter_table_enable_trigger statement.alter_table_stmt
    else
      unknown statement
    end
  when :create_domain_stmt
    create_domain statement.create_domain_stmt
  when :create_enum_stmt
    create_enum statement.create_enum_stmt
  when :create_extension_stmt
    # Don't return anything for now.
    {}
  when :create_function_stmt
    create_function statement.create_function_stmt
  when :create_stmt
    create_table statement.create_stmt
  when :create_table_as_stmt
    create_table_as statement.create_table_as_stmt
  when :create_trig_stmt
    create_trigger statement.create_trig_stmt
  when :delete_stmt
    delete statement.delete_stmt
  when :drop_stmt
    case statement.drop_stmt.remove_type
    when :OBJECT_INDEX
      drop_index statement.drop_stmt
    when :OBJECT_TABLE
      drop_table statement.drop_stmt
    when :OBJECT_TYPE
      drop_type statement.drop_stmt
    when :OBJECT_TRIGGER
      drop_trigger statement.drop_stmt
    when :OBJECT_FUNCTION
      drop_function statement.drop_stmt
    when :OBJECT_VIEW
      drop_view statement.drop_stmt
    when :OBJECT_DOMAIN
      drop_domain statement.drop_stmt
    when :OBJECT_MATVIEW
      drop_materialized_view statement.drop_stmt
    else
      unknown statement
    end
  when :index_stmt
    create_index statement.index_stmt
  when :insert_stmt
    insert statement.insert_stmt
  when :rename_stmt
    case statement.rename_stmt.rename_type
    when :OBJECT_TABCONSTRAINT
      rename_constraint statement.rename_stmt
    when :OBJECT_TABLE
      rename_table statement.rename_stmt
    when :OBJECT_COLUMN
      rename_column statement.rename_stmt
    when :OBJECT_FUNCTION
      rename_function statement.rename_stmt
    when :OBJECT_TRIGGER
      rename_trigger statement.rename_stmt
    when :OBJECT_INDEX
      rename_index statement.rename_stmt
    when :OBJECT_TYPE
      rename_type statement.rename_stmt
    else
      unknown statement
    end
  when :truncate_stmt
    truncate statement.truncate_stmt
  when :update_stmt
    update statement.update_stmt
  when :view_stmt
    view statement.view_stmt
  when :select_stmt
    # TODO: This is tricky, because this could call a function which might
    # alter the schema.
    # Don't return anything for now.
    {}
  when :create_seq_stmt
    # Don't return anything for now.
    {}
  when :create_stats_stmt
    # Don't return anything for now.
    {}
  when :define_stmt
    # Don't return anything for now.
    {}
  when :alter_seq_stmt
    # Don't return anything for now.
    {}
  when :comment_stmt
    # Don't return anything for now.
    {}
  when :lock_stmt
    # Don't return anything for now.
    {}
  when :variable_set_stmt
    # Don't return anything for now.
    {}
  when :transaction_stmt
    # "begin;" and "commit;" are parsed as :transaction_stmt.
    # Don't return anything for now.
    {}
  when :do_stmt
    # Don't return anything for now.
    {}
  else
    unknown statement
  end
end

.rename_column(statement) ⇒ Object



252
253
254
255
256
257
258
259
260
261
262
# File 'lib/pg_delta.rb', line 252

def self.rename_column(statement)
  {
    mutation: :alter_table_rename_column,
    type: :column,
    details: {
      table_name: statement.relation.relname,
      old_column_name: statement.subname,
      new_column_name: statement.newname
    }
  }
end

.rename_constraint(statement) ⇒ Object



632
633
634
635
636
637
638
639
640
641
642
# File 'lib/pg_delta.rb', line 632

def self.rename_constraint(statement)
  {
    mutation: :alter_table_rename_constraint,
    type: :constraint,
    details: {
      table_name: statement.relation.relname,
      old_constraint_name: statement.subname,
      new_constraint_name: statement.newname
    }
  }
end

.rename_function(statement) ⇒ Object



340
341
342
343
344
345
346
347
348
349
# File 'lib/pg_delta.rb', line 340

def self.rename_function(statement)
  {
    mutation: :rename_function,
    type: :function,
    details: {
      old_function_name: statement.object.object_with_args.objname.first.string.sval,
      new_function_name: statement.newname
    }
  }
end

.rename_index(statement) ⇒ Object



519
520
521
522
523
524
525
526
527
528
# File 'lib/pg_delta.rb', line 519

def self.rename_index(statement)
  {
    mutation: :rename_index,
    type: :index,
    details: {
      old_index_name: statement.relation.relname,
      new_index_name: statement.newname
    }
  }
end

.rename_table(statement) ⇒ Object



383
384
385
386
387
388
389
390
391
392
# File 'lib/pg_delta.rb', line 383

def self.rename_table(statement)
  {
    mutation: :rename_table,
    type: :table,
    details: {
      old_table_name: statement.relation.relname,
      new_table_name: statement.newname
    }
  }
end

.rename_trigger(statement) ⇒ Object



451
452
453
454
455
456
457
458
459
460
461
# File 'lib/pg_delta.rb', line 451

def self.rename_trigger(statement)
  {
    mutation: :rename_trigger,
    type: :trigger,
    details: {
      table_name: statement.relation.relname,
      old_trigger_name: statement.subname,
      new_trigger_name: statement.newname
    }
  }
end

.rename_type(statement) ⇒ Object



554
555
556
557
558
559
560
561
562
563
# File 'lib/pg_delta.rb', line 554

def self.rename_type(statement)
  {
    mutation: :rename_type,
    type: :type,
    details: {
      old_type_name: statement.object.list.items.first.string.sval,
      new_type_name: statement.newname
    }
  }
end

.split_statements(queries) ⇒ Array<PgQuery::Node>

Parses the provided queries and returns a PgQuery statement for each query. Returns an empty list if the queries cannot be parsed.

Parameters:

  • queries (String)

    The queries

Returns:

  • (Array<PgQuery::Node>)

    The info about the parsed query



15
16
17
18
19
20
# File 'lib/pg_delta.rb', line 15

def self.split_statements(queries)
  parsed_query = PgQuery.parse queries
  parsed_query.tree.stmts.map(&:stmt)
rescue
  []
end

.truncate(statement) ⇒ Object



394
395
396
397
398
399
400
401
402
# File 'lib/pg_delta.rb', line 394

def self.truncate(statement)
  {
    mutation: :truncate_table,
    type: :table,
    details: {
      table_names: statement.relations.map(&:range_var).map(&:relname)
    }
  }
end

.unknown(statement) ⇒ Object

Unknown mutations



646
647
648
649
650
651
652
653
654
# File 'lib/pg_delta.rb', line 646

def self.unknown(statement)
  {
    mutation: :unknown,
    type: :unknown,
    details: {
      statement: statement.to_s
    }
  }
end

.update(statement) ⇒ Object



485
486
487
488
489
490
491
492
493
494
# File 'lib/pg_delta.rb', line 485

def self.update(statement)
  {
    mutation: :update_data,
    type: :data,
    details: {
      table_name: statement.relation.relname,
      column_names: statement.target_list.map(&:res_target).map(&:name)
    }
  }
end

.view(statement) ⇒ Object

View mutations



567
568
569
570
571
572
573
574
575
# File 'lib/pg_delta.rb', line 567

def self.view(statement)
  {
    mutation: :create_view,
    type: :view,
    details: {
      view_name: statement.view.relname
    }
  }
end