Skip to content
代码片段 群组 项目
未验证 提交 0325ce64 编辑于 作者: Alper Akgun's avatar Alper Akgun 提交者: GitLab
浏览文件

Merge branch '442385-specs-for-next-desired-sharding-key' into 'master'

Add specs for changes surrounding "next" desired sharding key

See merge request https://gitlab.com/gitlab-org/gitlab/-/merge_requests/145223



Merged-by: default avatarAlper Akgun <aakgun@gitlab.com>
Approved-by: default avatarAlper Akgun <aakgun@gitlab.com>
Reviewed-by: default avatarAlper Akgun <aakgun@gitlab.com>
Co-authored-by: default avatarManoj M J <mmj@gitlab.com>
No related branches found
No related tags found
无相关合并请求
...@@ -15,43 +15,67 @@ ...@@ -15,43 +15,67 @@
end end
end end
it 'must be possible to backfill it via backfill_via' do context 'for tables that already have a backfilled, non-nullable sharding key on their parent' do
desired_sharding_key_entries.each do |entry| it 'must be possible to backfill it via backfill_via' do
entry.desired_sharding_key.each do |desired_column, details| desired_sharding_key_entries_not_awaiting_backfill_on_parent.each do |entry|
table = entry.table_name entry.desired_sharding_key.each do |desired_column, details|
connection = Gitlab::Database.schemas_to_base_models[entry.gitlab_schema].first.connection table = entry.table_name
sharding_key = desired_column connection = Gitlab::Database.schemas_to_base_models[entry.gitlab_schema].first.connection
parent = details['backfill_via']['parent'] sharding_key = desired_column
foreign_key = parent['foreign_key'] parent = details['backfill_via']['parent']
parent_table = parent['table'] foreign_key = parent['foreign_key']
parent_sharding_key = parent['sharding_key'] parent_table = parent['table']
parent_sharding_key = parent['sharding_key']
connection.execute("ALTER TABLE #{table} ADD COLUMN IF NOT EXISTS #{sharding_key} bigint")
connection.execute("ALTER TABLE #{table} ADD COLUMN IF NOT EXISTS #{sharding_key} bigint")
# Confirming it at least produces a valid query
connection.execute <<~SQL # Confirming it at least produces a valid query
UPDATE #{table} connection.execute <<~SQL
SET #{sharding_key} = #{parent_table}.#{parent_sharding_key} EXPLAIN UPDATE #{table}
FROM #{parent_table} SET #{sharding_key} = #{parent_table}.#{parent_sharding_key}
WHERE #{table}.#{foreign_key} = #{parent_table}.id FROM #{parent_table}
SQL WHERE #{table}.#{foreign_key} = #{parent_table}.id
SQL
end
end
end
it 'the parent.belongs_to must be a model with the parent.sharding_key column' do
desired_sharding_key_entries_not_awaiting_backfill_on_parent.each do |entry|
model = entry.classes.first.constantize
entry.desired_sharding_key.each do |_column, details|
parent = details['backfill_via']['parent']
parent_sharding_key = parent['sharding_key']
belongs_to = parent['belongs_to']
parent_association = model.reflect_on_association(belongs_to)
expect(parent_association).not_to be_nil,
"Invalid backfill_via.parent.belongs_to: #{belongs_to} in db/docs for #{entry.table_name}"
parent_columns = parent_association.klass.columns.map(&:name)
expect(parent_columns).to include(parent_sharding_key)
end
end end
end end
end end
it 'the parent.belongs_to must be a model with the parent.sharding_key column' do context 'for tables that do not already have a backfilled, non-nullable sharding key on their parent' \
desired_sharding_key_entries.each do |entry| 'but only has a desired sharding key on their parent' do
model = entry.classes.first.constantize it 'the parent.belongs_to must be a model with a desired_sharding_key' do
entry.desired_sharding_key.each do |_column, details| desired_sharding_key_entries_awaiting_backfill_on_parent.each do |entry|
parent = details['backfill_via']['parent'] model = entry.classes.first.constantize
parent_sharding_key = parent['sharding_key'] entry.desired_sharding_key.each do |column, details|
belongs_to = parent['belongs_to'] parent = details['backfill_via']['parent']
parent_association = model.reflect_on_association(belongs_to) parent_table = parent['table']
expect(parent_association).not_to be_nil, belongs_to = parent['belongs_to']
"Invalid backfil_via.parent.belongs_to: #{belongs_to} in db/docs for #{entry.table_name}" sharding_key_of_parent = parent['sharding_key']
parent_columns = parent_association.klass.columns.map(&:name)
parent_association = model.reflect_on_association(belongs_to)
expect(parent_columns).to include(parent_sharding_key) expect(parent_association).not_to be_nil,
"Invalid backfill_via.parent.belongs_to: #{belongs_to} in db/docs for #{entry.table_name}"
expect(desired_sharding_keys_of(parent_table).keys).to include(column)
expect(desired_sharding_keys_of(parent_table).keys).to include(sharding_key_of_parent)
end
end end
end end
end end
...@@ -72,4 +96,22 @@ def desired_sharding_key_entries ...@@ -72,4 +96,22 @@ def desired_sharding_key_entries
entry.desired_sharding_key.present? entry.desired_sharding_key.present?
end end
end end
def desired_sharding_key_entries_not_awaiting_backfill_on_parent
::Gitlab::Database::Dictionary.entries.select do |entry|
entry.desired_sharding_key.present? &&
entry.desired_sharding_key.all? { |_, details| details['awaiting_backfill_on_parent'].blank? }
end
end
def desired_sharding_key_entries_awaiting_backfill_on_parent
::Gitlab::Database::Dictionary.entries.select do |entry|
entry.desired_sharding_key.present? &&
entry.desired_sharding_key.all? { |_, details| details['awaiting_backfill_on_parent'] }
end
end
def desired_sharding_keys_of(table_name)
Gitlab::Database::Dictionary.entries.find_by_table_name(table_name).desired_sharding_key
end
end end
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册