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

fix orphaned tokens migration for some MySQL imported schemas

Markus Kahldb7f2a7c4809
diff --git a/db/migrate/20221017073431_remove_orphaned_tokens.rb b/db/migrate/20221017073431_remove_orphaned_tokens.rb
index 61c804451ae..71c37cc42d6 100644
--- a/db/migrate/20221017073431_remove_orphaned_tokens.rb
+++ b/db/migrate/20221017073431_remove_orphaned_tokens.rb
@@ -1,10 +1,30 @@
class RemoveOrphanedTokens < ActiveRecord::Migration[7.0]
def up
Token::Base.where.not(user_id: User.select(:id)).delete_all
+
+ # Make sure we have bigint columns on both sides so the foreign key can be added.
+ # It could be that they are of type numeric if the data was migrated from MySQL once.
+ make_bigint! :users, :id
+ make_bigint! :tokens, :user_id
+
add_foreign_key :tokens, :users
end
def down
# Nothing to do
end
+
+ def make_bigint!(table, field)
+ return if bigint? table, field
+
+ change_column table, field, :bigint
+ end
+
+ def bigint?(table, field)
+ get_column_type(table, field).to_s == "bigint"
+ end
+
+ def get_column_type(table, field)
+ ActiveRecord::Base.connection.columns(table.to_s).find { |col| col.name == field.to_s }&.sql_type
+ end
end