Skip to content

Self-hosted project management for devs and their clients: tickets, time tracking, docs and meetings, with an AI assistant that cites its sources. AGPL-3.0.

Run the view configuration migration in one transaction and refuse a rollback over overview rows

Guido Boyke1b5e063c8764
diff --git a/db/migrate/20261017120000_allow_view_configurations_without_project.rb b/db/migrate/20261017120000_allow_view_configurations_without_project.rb
index 67c5b4d..e4f239b 100644
--- a/db/migrate/20261017120000_allow_view_configurations_without_project.rb
+++ b/db/migrate/20261017120000_allow_view_configurations_without_project.rb
@@ -5,17 +5,28 @@
# over (user_id, project_id, view_type) does not hold that row (Postgres counts
# NULLs as distinct), so it gets a partial index of its own. No data changes.
#
-# Rolling back fails while a row without a project exists (the column cannot
-# be NOT NULL again over it). Those rows are personal column lists of the
-# overview; delete them first: ViewConfiguration.where(project_id: nil).delete_all
+# One transaction, not a concurrent index: the table holds a few rows per user,
+# and a rollback must leave nothing half done.
class AllowViewConfigurationsWithoutProject < ActiveRecord::Migration[8.1]
- disable_ddl_transaction!
-
INDEX = 'index_view_configs_on_user_organisation_type_without_project'
- def change
+ def up
change_column_null :view_configurations, :project_id, true
add_index :view_configurations, %i[user_id organisation_id view_type],
- unique: true, where: 'project_id IS NULL', name: INDEX, algorithm: :concurrently
+ unique: true, where: 'project_id IS NULL', name: INDEX
+ end
+
+ # Refuses while a row without a project exists: the column cannot be NOT NULL
+ # over it. Those rows are personal column lists of "Alle Meetings"; deleting
+ # them costs their owners that list and nothing else.
+ def down
+ if select_value('SELECT 1 FROM view_configurations WHERE project_id IS NULL LIMIT 1')
+ raise ActiveRecord::IrreversibleMigration,
+ 'view_configurations has rows without a project (base views of "Alle Meetings"). ' \
+ 'Delete them first: ViewConfiguration.where(project_id: nil).delete_all'
+ end
+
+ remove_index :view_configurations, name: INDEX
+ change_column_null :view_configurations, :project_id, false
end
end