Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

re-run postgres index fix migration if necessary during budgets migration (#8802)

Markus Kahl7b2baebc31e4
diff --git a/modules/budgets/db/migrate/20200810152654_rename_cost_object_to_budget.rb b/modules/budgets/db/migrate/20200810152654_rename_cost_object_to_budget.rb
index 4a7df9e8e7e..5e4d2408396 100644
--- a/modules/budgets/db/migrate/20200810152654_rename_cost_object_to_budget.rb
+++ b/modules/budgets/db/migrate/20200810152654_rename_cost_object_to_budget.rb
@@ -1,5 +1,13 @@
class RenameCostObjectToBudget < ActiveRecord::Migration[6.0]
def up
+ if primary_key_index_name(:cost_objects) != 'cost_objects_pkey'
+ warn "Found unexpected primary key name. Fixing primary key names..."
+
+ require "./db/migrate/20190502102512_ensure_postgres_index_names.rb"
+
+ EnsurePostgresIndexNames.new.up
+ end
+
remove_column :cost_objects, :type
rename_table :cost_objects, :budgets
@@ -147,4 +155,12 @@ class RenameCostObjectToBudget < ActiveRecord::Migration[6.0]
WHERE serialized LIKE '%#{old}%'
SQL
end
+
+ def primary_key_index_name(table_name)
+ connection = ActiveRecord::Base.connection
+ table = connection.quote table_name
+ sql = "SELECT indexname FROM pg_indexes WHERE tablename = #{table} AND indexdef LIKE '%(id)';"
+
+ connection.execute(sql).map { |row| row['indexname'] }.compact.first
+ end
end