Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/db
diff options
context:
space:
mode:
Diffstat (limited to 'db')
-rw-r--r--db/post_migrate/20210525075724_clean_up_pending_builds_table.rb41
-rw-r--r--db/schema_migrations/202105250757241
2 files changed, 42 insertions, 0 deletions
diff --git a/db/post_migrate/20210525075724_clean_up_pending_builds_table.rb b/db/post_migrate/20210525075724_clean_up_pending_builds_table.rb
new file mode 100644
index 00000000000..58b9b5b3ca1
--- /dev/null
+++ b/db/post_migrate/20210525075724_clean_up_pending_builds_table.rb
@@ -0,0 +1,41 @@
+# frozen_string_literal: true
+
+class CleanUpPendingBuildsTable < ActiveRecord::Migration[6.0]
+ BATCH_SIZE = 1000
+
+ disable_ddl_transaction!
+
+ def up
+ return unless Gitlab.dev_or_test_env? || Gitlab.com?
+
+ each_batch('ci_pending_builds', of: BATCH_SIZE) do |min, max|
+ execute <<~SQL
+ DELETE FROM ci_pending_builds
+ USING ci_builds
+ WHERE ci_builds.id = ci_pending_builds.build_id
+ AND ci_builds.status != 'pending'
+ AND ci_builds.type = 'Ci::Build'
+ AND ci_pending_builds.id BETWEEN #{min} AND #{max}
+ SQL
+ end
+ end
+
+ def down
+ # noop
+ end
+
+ private
+
+ def each_batch(table_name, scope: ->(table) { table.all }, of: 1000)
+ table = Class.new(ActiveRecord::Base) do
+ include EachBatch
+
+ self.table_name = table_name
+ self.inheritance_column = :_type_disabled
+ end
+
+ scope.call(table).each_batch(of: of) do |batch|
+ yield batch.pluck('MIN(id), MAX(id)').first
+ end
+ end
+end
diff --git a/db/schema_migrations/20210525075724 b/db/schema_migrations/20210525075724
new file mode 100644
index 00000000000..539138e18a9
--- /dev/null
+++ b/db/schema_migrations/20210525075724
@@ -0,0 +1 @@
+5dc1119c5efe28225bb7ac8a9ed2c4c5cfaeaff202194ed4419cfd54eaf7483d \ No newline at end of file