Commit 3a27579c authored by Lin Jen-Shin's avatar Lin Jen-Shin

Align the ordering of partitioned tables

In JiHu repository, there's an additional code to register this
table to be synchronized like:

    Gitlab::Database::Partitioning.register_models([
      Phone::VerificationCode
    ])

This does not present in GitLab repository, but only in JiHu repository,
and this will cause the table to move to the top of db/structure.sql along
with other synchronized partitioned tables.

This will align both repositories to be more consistent.

The same does also happen for FOSS where
incident_management_pending_alert_escalations and
incident_management_pending_issue_escalations are not aligned.
parent 772e44a3
...@@ -10,6 +10,29 @@ if Gitlab.ee? ...@@ -10,6 +10,29 @@ if Gitlab.ee?
IncidentManagement::PendingEscalations::Alert, IncidentManagement::PendingEscalations::Alert,
IncidentManagement::PendingEscalations::Issue IncidentManagement::PendingEscalations::Issue
]) ])
else
Gitlab::Database::Partitioning.register_tables([
{
table_name: 'incident_management_pending_alert_escalations',
partitioned_column: :process_at, strategy: :monthly
},
{
table_name: 'incident_management_pending_issue_escalations',
partitioned_column: :process_at, strategy: :monthly
}
])
end
# The following tables are already defined as models
unless Gitlab.jh?
Gitlab::Database::Partitioning.register_tables([
# This should be synchronized with the following model:
# https://gitlab.com/gitlab-jh/gitlab/-/blob/main-jh/jh/app/models/phone/verification_code.rb
{
table_name: 'verification_codes',
partitioned_column: :created_at, strategy: :monthly
}
])
end end
begin begin
......
...@@ -135,6 +135,19 @@ CREATE TABLE incident_management_pending_issue_escalations ( ...@@ -135,6 +135,19 @@ CREATE TABLE incident_management_pending_issue_escalations (
) )
PARTITION BY RANGE (process_at); PARTITION BY RANGE (process_at);
CREATE TABLE verification_codes (
created_at timestamp with time zone DEFAULT now() NOT NULL,
visitor_id_code text NOT NULL,
code text NOT NULL,
phone text NOT NULL,
CONSTRAINT check_9b84e6aaff CHECK ((char_length(code) <= 8)),
CONSTRAINT check_ccc542256b CHECK ((char_length(visitor_id_code) <= 64)),
CONSTRAINT check_f5684c195b CHECK ((char_length(phone) <= 32))
)
PARTITION BY RANGE (created_at);
COMMENT ON TABLE verification_codes IS 'JiHu-specific table';
CREATE TABLE web_hook_logs ( CREATE TABLE web_hook_logs (
id bigint NOT NULL, id bigint NOT NULL,
web_hook_id integer NOT NULL, web_hook_id integer NOT NULL,
...@@ -20185,19 +20198,6 @@ CREATE SEQUENCE users_statistics_id_seq ...@@ -20185,19 +20198,6 @@ CREATE SEQUENCE users_statistics_id_seq
ALTER SEQUENCE users_statistics_id_seq OWNED BY users_statistics.id; ALTER SEQUENCE users_statistics_id_seq OWNED BY users_statistics.id;
CREATE TABLE verification_codes (
created_at timestamp with time zone DEFAULT now() NOT NULL,
visitor_id_code text NOT NULL,
code text NOT NULL,
phone text NOT NULL,
CONSTRAINT check_9b84e6aaff CHECK ((char_length(code) <= 8)),
CONSTRAINT check_ccc542256b CHECK ((char_length(visitor_id_code) <= 64)),
CONSTRAINT check_f5684c195b CHECK ((char_length(phone) <= 32))
)
PARTITION BY RANGE (created_at);
COMMENT ON TABLE verification_codes IS 'JiHu-specific table';
CREATE TABLE vulnerabilities ( CREATE TABLE vulnerabilities (
id bigint NOT NULL, id bigint NOT NULL,
milestone_id bigint, milestone_id bigint,
...@@ -3,15 +3,44 @@ ...@@ -3,15 +3,44 @@
module Gitlab module Gitlab
module Database module Database
module Partitioning module Partitioning
class TableWithoutModel
include PartitionedTable::ClassMethods
attr_reader :table_name
def initialize(table_name:, partitioned_column:, strategy:)
@table_name = table_name
partitioned_by(partitioned_column, strategy: strategy)
end
def connection
Gitlab::Database::SharedModel.connection
end
end
def self.register_models(models) def self.register_models(models)
registered_models.merge(models) registered_models.merge(models)
end end
def self.register_tables(tables)
registered_tables.merge(tables)
end
def self.registered_models def self.registered_models
@registered_models ||= Set.new @registered_models ||= Set.new
end end
def self.sync_partitions(models_to_sync = registered_models) def self.registered_tables
@registered_tables ||= Set.new
end
def self.registered_for_sync
registered_models + registered_tables.map do |table|
TableWithoutModel.new(**table)
end
end
def self.sync_partitions(models_to_sync = registered_for_sync)
Gitlab::AppLogger.info(message: 'Syncing dynamic postgres partitions') Gitlab::AppLogger.info(message: 'Syncing dynamic postgres partitions')
Gitlab::Database::EachDatabase.each_model_connection(models_to_sync) do |model| Gitlab::Database::EachDatabase.each_model_connection(models_to_sync) do |model|
......
...@@ -45,8 +45,13 @@ RSpec.describe Gitlab::Database::Partitioning do ...@@ -45,8 +45,13 @@ RSpec.describe Gitlab::Database::Partitioning do
let(:model) { double('model') } let(:model) { double('model') }
it 'manages partitions for each registered model' do it 'manages partitions for each registered model' do
registered_for_sync = described_class.registered_for_sync
allow(described_class).to receive(:registered_for_sync)
.and_return(registered_for_sync)
expect(Gitlab::Database::EachDatabase).to receive(:each_model_connection) expect(Gitlab::Database::EachDatabase).to receive(:each_model_connection)
.with(described_class.registered_models) .with(registered_for_sync)
.and_yield(model) .and_yield(model)
expect(partition_manager_class).to receive(:new).with(model).and_return(partition_manager) expect(partition_manager_class).to receive(:new).with(model).and_return(partition_manager)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment