summaryrefslogtreecommitdiff
path: root/lib/generators
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2023-03-20 15:19:03 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2023-03-20 15:19:03 +0000
commit14bd84b61276ef29b97d23642d698de769bacfd2 (patch)
treef9eba90140c1bd874211dea17750a0d422c04080 /lib/generators
parent891c388697b2db0d8ee0c8358a9bdbf6dc56d581 (diff)
downloadgitlab-ce-14bd84b61276ef29b97d23642d698de769bacfd2.tar.gz
Add latest changes from gitlab-org/gitlab@15-10-stable-eev15.10.0-rc42
Diffstat (limited to 'lib/generators')
-rw-r--r--lib/generators/batched_background_migration/USAGE12
-rw-r--r--lib/generators/batched_background_migration/batched_background_migration_generator.rb73
-rw-r--r--lib/generators/batched_background_migration/templates/batched_background_migration_dictionary.template6
-rw-r--r--lib/generators/batched_background_migration/templates/batched_background_migration_job.template22
-rw-r--r--lib/generators/batched_background_migration/templates/batched_background_migration_job_spec.template7
-rw-r--r--lib/generators/batched_background_migration/templates/queue_batched_background_migration.template28
-rw-r--r--lib/generators/batched_background_migration/templates/queue_batched_background_migration_spec.template26
-rw-r--r--lib/generators/gitlab/snowplow_event_definition_generator.rb13
8 files changed, 179 insertions, 8 deletions
diff --git a/lib/generators/batched_background_migration/USAGE b/lib/generators/batched_background_migration/USAGE
new file mode 100644
index 00000000000..2fc2b2f7b96
--- /dev/null
+++ b/lib/generators/batched_background_migration/USAGE
@@ -0,0 +1,12 @@
+Description:
+ Generates files required for batched background migration.
+
+Example:
+ rails g batched_background_migration my_batched_migration --table_name=users --column_name=id --feature_category=gitaly
+
+ This will create:
+ db/post_migrate/20230213215230_queue_my_batched_migration.rb
+ spec/migrations/20230213215230_queue_my_batched_migration_spec.rb
+ lib/gitlab/background_migration/my_batched_migration.rb
+ spec/lib/gitlab/background_migration/my_batched_migration_spec.rb
+ db/docs/batched_background_migrations/my_batched_migration.yml
diff --git a/lib/generators/batched_background_migration/batched_background_migration_generator.rb b/lib/generators/batched_background_migration/batched_background_migration_generator.rb
new file mode 100644
index 00000000000..c68ed52c1a0
--- /dev/null
+++ b/lib/generators/batched_background_migration/batched_background_migration_generator.rb
@@ -0,0 +1,73 @@
+# frozen_string_literal: true
+
+require 'rails/generators/active_record'
+
+module BatchedBackgroundMigration
+ class BatchedBackgroundMigrationGenerator < ActiveRecord::Generators::Base
+ source_root File.expand_path('templates', __dir__)
+
+ class_option :table_name
+ class_option :column_name
+ class_option :feature_category
+
+ def validate!
+ raise ArgumentError, "table_name is required" unless table_name.present?
+ raise ArgumentError, "column_name is required" unless column_name.present?
+ raise ArgumentError, "feature_category is required" unless feature_category.present?
+ end
+
+ def create_post_migration_and_specs
+ migration_template(
+ "queue_batched_background_migration.template",
+ File.join(db_migrate_path, "queue_#{file_name}.rb")
+ )
+
+ template(
+ "queue_batched_background_migration_spec.template",
+ File.join("spec/migrations/#{migration_number}_queue_#{file_name}_spec.rb")
+ )
+ end
+
+ def create_batched_background_migration_class_and_specs
+ template(
+ "batched_background_migration_job.template",
+ File.join("lib/gitlab/background_migration/#{file_name}.rb")
+ )
+
+ template(
+ "batched_background_migration_job_spec.template",
+ File.join("spec/lib/gitlab/background_migration/#{file_name}_spec.rb")
+ )
+ end
+
+ def create_dictionary_file
+ template(
+ "batched_background_migration_dictionary.template",
+ File.join("db/docs/batched_background_migrations/#{file_name}.yml")
+ )
+ end
+
+ def db_migrate_path
+ super.sub("migrate", "post_migrate")
+ end
+
+ private
+
+ def table_name
+ options[:table_name]
+ end
+
+ def column_name
+ options[:column_name]
+ end
+
+ def feature_category
+ options[:feature_category]
+ end
+
+ def current_milestone
+ version = Gem::Version.new(File.read('VERSION'))
+ version.release.segments.first(2).join('.')
+ end
+ end
+end
diff --git a/lib/generators/batched_background_migration/templates/batched_background_migration_dictionary.template b/lib/generators/batched_background_migration/templates/batched_background_migration_dictionary.template
new file mode 100644
index 00000000000..8aa08e15f48
--- /dev/null
+++ b/lib/generators/batched_background_migration/templates/batched_background_migration_dictionary.template
@@ -0,0 +1,6 @@
+---
+migration_job_name: <%= class_name %>
+description: # Please capture what <%= class_name %> does
+feature_category: <%= feature_category %>
+introduced_by_url: # URL of the MR (or issue/commit) that introduced the migration
+milestone: <%= current_milestone %>
diff --git a/lib/generators/batched_background_migration/templates/batched_background_migration_job.template b/lib/generators/batched_background_migration/templates/batched_background_migration_job.template
new file mode 100644
index 00000000000..c57ac637cb8
--- /dev/null
+++ b/lib/generators/batched_background_migration/templates/batched_background_migration_job.template
@@ -0,0 +1,22 @@
+# frozen_string_literal: true
+
+# See https://docs.gitlab.com/ee/development/database/batched_background_migrations.html
+# for more information on how to use batched background migrations
+
+# Update below commented lines with appropriate values.
+
+module Gitlab
+ module BackgroundMigration
+ class <%= class_name %> < BatchedMigrationJob
+ # operation_name :my_operation
+ # scope_to ->(relation) { relation.where(column: "value") }
+ feature_category :<%= feature_category %>
+
+ def perform
+ each_sub_batch do |sub_batch|
+ # Your action on each sub_batch
+ end
+ end
+ end
+ end
+end
diff --git a/lib/generators/batched_background_migration/templates/batched_background_migration_job_spec.template b/lib/generators/batched_background_migration/templates/batched_background_migration_job_spec.template
new file mode 100644
index 00000000000..c41b8107c95
--- /dev/null
+++ b/lib/generators/batched_background_migration/templates/batched_background_migration_job_spec.template
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+
+RSpec.describe Gitlab::BackgroundMigration::<%= class_name %>, schema: <%= migration_number %>, feature_category: :<%= feature_category %> do # rubocop:disable Layout/LineLength
+ # Tests go here
+end
diff --git a/lib/generators/batched_background_migration/templates/queue_batched_background_migration.template b/lib/generators/batched_background_migration/templates/queue_batched_background_migration.template
new file mode 100644
index 00000000000..502edf2c1d7
--- /dev/null
+++ b/lib/generators/batched_background_migration/templates/queue_batched_background_migration.template
@@ -0,0 +1,28 @@
+# frozen_string_literal: true
+
+# See https://docs.gitlab.com/ee/development/database/batched_background_migrations.html
+# for more information on when/how to queue batched background migrations
+
+# Update below commented lines with appropriate values.
+
+class <%= migration_class_name %> < Gitlab::Database::Migration[<%= Gitlab::Database::Migration.current_version %>]
+ MIGRATION = "<%= class_name %>"
+ # DELAY_INTERVAL = 2.minutes
+ # BATCH_SIZE = <%= Gitlab::Database::Migrations::BatchedBackgroundMigrationHelpers::BATCH_SIZE %>
+ # SUB_BATCH_SIZE = <%= Gitlab::Database::Migrations::BatchedBackgroundMigrationHelpers::SUB_BATCH_SIZE %>
+
+ def up
+ queue_batched_background_migration(
+ MIGRATION,
+ :<%= table_name %>,
+ :<%= column_name %>,
+ job_interval: DELAY_INTERVAL,
+ batch_size: BATCH_SIZE,
+ sub_batch_size: SUB_BATCH_SIZE
+ )
+ end
+
+ def down
+ delete_batched_background_migration(MIGRATION, :<%= table_name.to_sym %>, :<%= column_name.to_sym %>, [])
+ end
+end
diff --git a/lib/generators/batched_background_migration/templates/queue_batched_background_migration_spec.template b/lib/generators/batched_background_migration/templates/queue_batched_background_migration_spec.template
new file mode 100644
index 00000000000..e0a3078114e
--- /dev/null
+++ b/lib/generators/batched_background_migration/templates/queue_batched_background_migration_spec.template
@@ -0,0 +1,26 @@
+# frozen_string_literal: true
+
+require 'spec_helper'
+require_migration!
+
+RSpec.describe <%= migration_class_name %>, feature_category: :<%= feature_category.to_sym %> do
+ # let!(:batched_migration) { described_class::MIGRATION }
+
+ # it 'schedules a new batched migration' do
+ # reversible_migration do |migration|
+ # migration.before -> {
+ # expect(batched_migration).not_to have_scheduled_batched_migration
+ # }
+
+ # migration.after -> {
+ # expect(batched_migration).to have_scheduled_batched_migration(
+ # table_name: :<%= table_name %>,
+ # column_name: :<%= column_name %>,
+ # interval: described_class::DELAY_INTERVAL,
+ # batch_size: described_class::BATCH_SIZE,
+ # sub_batch_size: described_class::SUB_BATCH_SIZE
+ # )
+ # }
+ # end
+ # end
+end
diff --git a/lib/generators/gitlab/snowplow_event_definition_generator.rb b/lib/generators/gitlab/snowplow_event_definition_generator.rb
index 827e87dc313..b1a31541350 100644
--- a/lib/generators/gitlab/snowplow_event_definition_generator.rb
+++ b/lib/generators/gitlab/snowplow_event_definition_generator.rb
@@ -14,12 +14,11 @@ module Gitlab
class_option :ee, type: :boolean, optional: true, default: false, desc: 'Indicates if event is for ee'
class_option :category, type: :string, optional: false, desc: 'Category of the event'
class_option :action, type: :string, optional: false, desc: 'Action of the event'
- class_option :force, type: :boolean, optional: true, default: false, desc: 'Overwrite existing definition'
def create_event_file
- raise "Event definition already exists at #{file_path}" if definition_exists? && !force_definition_override?
+ raise "Event definition already exists at #{file_path}" if definition_exists?
- template "event_definition.yml", file_path, force: force_definition_override?
+ template "event_definition.yml", file_path, force: false
end
def distributions
@@ -42,10 +41,6 @@ module Gitlab
options[:ee]
end
- def force_definition_override?
- options[:force]
- end
-
private
def definition_exists?
@@ -64,8 +59,10 @@ module Gitlab
File.join(EE_DIR, file_name)
end
+ # Example of file name
+ # 20230227000018_project_management_issue_title_changed.yml
def file_name
- name = remove_special_chars("#{Time.current.to_i}_#{event_category}_#{event_action}")
+ name = remove_special_chars("#{Time.now.utc.strftime('%Y%m%d%H%M%S')}_#{event_category}_#{event_action}")
"#{name[0..95]}.yml" # max 100 chars, see https://gitlab.com/gitlab-com/gl-infra/delivery/-/issues/2030#note_679501200
end