521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
|
# File 'lib/webhookdb/replicator/base.rb', line 521
def ensure_all_columns_modification
existing_cols, existing_indices = nil
max_pk = 0
sint = self.service_integration
self.admin_dataset do |ds|
return self.create_table_modification unless ds.db.table_exists?(self.qualified_table_sequel_identifier)
existing_cols = ds.columns.to_set
existing_indices = ds.db[:pg_indexes].where(
schemaname: sint.organization.replication_schema,
tablename: sint.table_name,
).select_map(:indexname).to_set
max_pk = ds.max(:pk) || 0
end
adapter = Webhookdb::DBAdapter::PG.new
table = self.dbadapter_table
result = Webhookdb::Replicator::SchemaModification.new
missing_columns = self._denormalized_columns.delete_if { |c| existing_cols.include?(c.name) }
missing_columns.each do |whcol|
result.transaction_statements << adapter.add_column_sql(table, whcol.to_dbadapter)
end
if (enrich_col = self.enrichment_column) && !existing_cols.include?(enrich_col.name)
result.transaction_statements << adapter.add_column_sql(table, enrich_col)
end
if missing_columns.any?
result.nontransaction_statements.concat(missing_columns.filter_map(&:backfill_statement))
update_expr = missing_columns.to_h { |c| [c.name, c.backfill_expr || c.to_sql_expr] }
self.admin_dataset do |ds|
chunks = Webhookdb::Replicator::Base.chunked_row_update_bounds(max_pk)
chunks[...-1].each do |(lower, upper)|
update_query = ds.where { pk > lower }.where { pk <= upper }.update_sql(update_expr)
result.nontransaction_statements << update_query
end
final_update_query = ds.where { pk > chunks[-1][0] }.update_sql(update_expr)
result.nontransaction_statements << final_update_query
end
end
self.indices(table).map do |index|
next if existing_indices.include?(index.name.to_s)
result.nontransaction_statements << adapter.create_index_sql(index, concurrently: true)
end
result.application_database_statements << sint.ensure_sequence_sql if self.requires_sequence?
return result
end
|