Class: Gitlab::Database::PartitioningMigrationHelpers::BackfillPartitionedTable::BulkCopy

Inherits:
Object
  • Object
show all
Defined in:
lib/gitlab/database/partitioning_migration_helpers/backfill_partitioned_table.rb

Overview

Helper class to copy data between two tables via upserts

Constant Summary collapse

DELIMITER =
', '

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source_table, destination_table, source_column, connection:) ⇒ BulkCopy

Returns a new instance of BulkCopy.


66
67
68
69
70
71
# File 'lib/gitlab/database/partitioning_migration_helpers/backfill_partitioned_table.rb', line 66

def initialize(source_table, destination_table, source_column, connection:)
  @source_table = source_table
  @destination_table = destination_table
  @source_column = source_column
  @connection = connection
end

Instance Attribute Details

#connectionObject (readonly)

Returns the value of attribute connection.


64
65
66
# File 'lib/gitlab/database/partitioning_migration_helpers/backfill_partitioned_table.rb', line 64

def connection
  @connection
end

#destination_tableObject (readonly)

Returns the value of attribute destination_table.


64
65
66
# File 'lib/gitlab/database/partitioning_migration_helpers/backfill_partitioned_table.rb', line 64

def destination_table
  @destination_table
end

#source_columnObject (readonly)

Returns the value of attribute source_column.


64
65
66
# File 'lib/gitlab/database/partitioning_migration_helpers/backfill_partitioned_table.rb', line 64

def source_column
  @source_column
end

#source_tableObject (readonly)

Returns the value of attribute source_table.


64
65
66
# File 'lib/gitlab/database/partitioning_migration_helpers/backfill_partitioned_table.rb', line 64

def source_table
  @source_table
end

Instance Method Details

#copy_between(start_id, stop_id) ⇒ Object


73
74
75
76
77
78
79
80
81
82
# File 'lib/gitlab/database/partitioning_migration_helpers/backfill_partitioned_table.rb', line 73

def copy_between(start_id, stop_id)
  connection.execute(<<~SQL)
    INSERT INTO #{destination_table} (#{column_listing})
    SELECT #{column_listing}
    FROM #{source_table}
    WHERE #{source_column} BETWEEN #{start_id} AND #{stop_id}
    FOR UPDATE
    ON CONFLICT (#{conflict_targets}) DO NOTHING
  SQL
end