Baseline replacing collection with board across code
This commit is contained in:
@@ -5,10 +5,10 @@ require_relative "../../config/environment"
|
||||
ApplicationRecord.with_each_tenant do |tenant|
|
||||
puts "Processing tenant: #{tenant}"
|
||||
|
||||
Collection.find_each do |collection|
|
||||
puts " Processing collection: #{collection.name} (ID: #{collection.id})"
|
||||
Board.find_each do |board|
|
||||
puts " Processing board: #{board.name} (ID: #{board.id})"
|
||||
|
||||
columns = collection.columns.order(:id)
|
||||
columns = board.columns.order(:id)
|
||||
|
||||
columns.each_with_index do |column, index|
|
||||
column.update_column(:position, index)
|
||||
|
||||
@@ -41,13 +41,13 @@ ApplicationRecord.with_each_tenant do |tenant|
|
||||
puts
|
||||
|
||||
Card.find_each do |card|
|
||||
puts "### Processing card #{card.id} in #{Rails.application.routes.url_helpers.collection_card_path(card.collection, card)}"
|
||||
puts "### Processing card #{card.id} in #{Rails.application.routes.url_helpers.board_card_path(card.board, card)}"
|
||||
fix_attachments(card.description)
|
||||
card.reload
|
||||
|
||||
old_body = card.description.body.to_s
|
||||
if match = regex.match(old_body)
|
||||
puts "URL found in card #{card.id} in #{Rails.application.routes.url_helpers.collection_card_path(card.collection, card)}"
|
||||
puts "URL found in card #{card.id} in #{Rails.application.routes.url_helpers.board_card_path(card.board, card)}"
|
||||
new_body = old_body.gsub(regex, "://#{domain}/#{account_id}/")
|
||||
|
||||
card.description.update(body: new_body) || raise("Failed to update card description for card #{card.id}")
|
||||
@@ -55,13 +55,13 @@ ApplicationRecord.with_each_tenant do |tenant|
|
||||
end
|
||||
|
||||
Comment.find_each do |comment|
|
||||
puts "### Processing comment #{comment.id} in #{Rails.application.routes.url_helpers.collection_card_path(comment.card.collection, comment.card)}"
|
||||
puts "### Processing comment #{comment.id} in #{Rails.application.routes.url_helpers.board_card_path(comment.card.board, comment.card)}"
|
||||
fix_attachments(comment.body)
|
||||
comment.reload
|
||||
|
||||
old_body = comment.body.body.to_s
|
||||
if match = regex.match(old_body)
|
||||
puts "URL found in comment #{comment.id} in #{Rails.application.routes.url_helpers.collection_card_path(comment.card.collection, comment.card)}"
|
||||
puts "URL found in comment #{comment.id} in #{Rails.application.routes.url_helpers.board_card_path(comment.card.board, comment.card)}"
|
||||
new_body = old_body.gsub(regex, "://#{domain}/#{account_id}/")
|
||||
|
||||
comment.body.update(body: new_body) || raise("Failed to update comment body for comment #{comment.id}")
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
require_relative "../config/environment"
|
||||
|
||||
def replace_url(string)
|
||||
string.gsub(%r{/collections/\d+/cards/(\d+)}) do
|
||||
string.gsub(%r{/boards/\d+/cards/(\d+)}) do
|
||||
"/cards/#{$1}"
|
||||
end
|
||||
end
|
||||
|
||||
@@ -4,7 +4,7 @@ require_relative "../config/environment"
|
||||
|
||||
def replace_url(string)
|
||||
string.gsub(%r{/buckets/(\d+)/bubbles/(\d+)}) do
|
||||
"/collections/#{$1}/cards/#{$2}"
|
||||
"/boards/#{$1}/cards/#{$2}"
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -23,28 +23,28 @@ ApplicationRecord.with_each_tenant do |tenant|
|
||||
|
||||
Column.destroy_all
|
||||
|
||||
Collection.find_each do |collection|
|
||||
next unless collection.workflow.present?
|
||||
Board.find_each do |board|
|
||||
next unless board.workflow.present?
|
||||
|
||||
# Map to track stage_id -> column
|
||||
columns_by_stage = {}
|
||||
|
||||
# Create columns from workflow stages
|
||||
collection.workflow.stages.find_each do |stage|
|
||||
column = collection.columns.create!(
|
||||
board.workflow.stages.find_each do |stage|
|
||||
column = board.columns.create!(
|
||||
name: stage.name,
|
||||
color: stage.color || Card::Colored::COLORS.first
|
||||
)
|
||||
columns_by_stage[stage] = column
|
||||
puts "Created column '#{column.name}' for collection '#{collection.name}'"
|
||||
puts "Created column '#{column.name}' for board '#{board.name}'"
|
||||
end
|
||||
|
||||
# Associate cards with their corresponding columns based on stages
|
||||
collection.cards.includes(:stage).find_each do |card|
|
||||
board.cards.includes(:stage).find_each do |card|
|
||||
next if !card.doing? || card.stage.blank?
|
||||
|
||||
unless card.stage.workflow.collections.include?(collection)
|
||||
puts "Corrupt data: the card with id #{card.id} has the stage #{card.stage.name} with id #{card.stage.id} that belongs to a workflow not asociated ot its collection"
|
||||
unless card.stage.workflow.boards.include?(board)
|
||||
puts "Corrupt data: the card with id #{card.id} has the stage #{card.stage.name} with id #{card.stage.id} that belongs to a workflow not asociated ot its board"
|
||||
next
|
||||
end
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ RENAME_RULES = {
|
||||
"closed" => "closed",
|
||||
"poppable" => "closeable",
|
||||
"pop" => "closure",
|
||||
"bucket" => "collection"
|
||||
"bucket" => "board"
|
||||
}
|
||||
|
||||
EXTENSIONS = %w[.rb .yml .html .js .css .erb]
|
||||
|
||||
@@ -11,7 +11,7 @@ RENAMES = {
|
||||
"poppable" => "closeable",
|
||||
"closed" => "closed",
|
||||
"pop" => "closure",
|
||||
"bucket" => "collection"
|
||||
"bucket" => "board"
|
||||
}.freeze
|
||||
|
||||
FILE_EXTENSIONS = %w[rb yml html css js jpg jpeg png gif svg erb].freeze
|
||||
|
||||
@@ -11,95 +11,95 @@ ApplicationRecord.with_each_tenant do |tenant|
|
||||
ApplicationRecord.connection.execute("PRAGMA foreign_keys = OFF;")
|
||||
|
||||
begin
|
||||
# Get all collections ordered by ID
|
||||
collections = Collection.order(:id).to_a
|
||||
# Get all boards ordered by ID
|
||||
boards = Board.order(:id).to_a
|
||||
|
||||
# Create mapping of old IDs to new IDs
|
||||
collections.each_with_index do |collection, index|
|
||||
id_mapping[collection.id] = index + 1
|
||||
boards.each_with_index do |board, index|
|
||||
id_mapping[board.id] = index + 1
|
||||
end
|
||||
|
||||
# Update foreign keys in related tables
|
||||
puts "Updating foreign keys in related tables..."
|
||||
|
||||
# Update accesses table
|
||||
Access.where.not(collection_id: nil).find_each do |access|
|
||||
if id_mapping[access.collection_id]
|
||||
access.update_column(:collection_id, id_mapping[access.collection_id])
|
||||
Access.where.not(board_id: nil).find_each do |access|
|
||||
if id_mapping[access.board_id]
|
||||
access.update_column(:board_id, id_mapping[access.board_id])
|
||||
end
|
||||
end
|
||||
|
||||
# Update cards table
|
||||
Card.where.not(collection_id: nil).find_each do |card|
|
||||
if id_mapping[card.collection_id]
|
||||
card.update_column(:collection_id, id_mapping[card.collection_id])
|
||||
Card.where.not(board_id: nil).find_each do |card|
|
||||
if id_mapping[card.board_id]
|
||||
card.update_column(:board_id, id_mapping[card.board_id])
|
||||
end
|
||||
end
|
||||
|
||||
# Update collections_filters table (join table)
|
||||
ApplicationRecord.connection.execute("SELECT collection_id FROM collections_filters").each do |row|
|
||||
# Update boards_filters table (join table)
|
||||
ApplicationRecord.connection.execute("SELECT board_id FROM boards_filters").each do |row|
|
||||
old_id = row[0]
|
||||
if id_mapping[old_id]
|
||||
ApplicationRecord.connection.execute("UPDATE collections_filters SET collection_id = #{id_mapping[old_id]} WHERE collection_id = #{old_id}")
|
||||
ApplicationRecord.connection.execute("UPDATE boards_filters SET board_id = #{id_mapping[old_id]} WHERE board_id = #{old_id}")
|
||||
end
|
||||
end
|
||||
|
||||
# Update events table
|
||||
Event.where.not(collection_id: nil).find_each do |event|
|
||||
if id_mapping[event.collection_id]
|
||||
event.update_column(:collection_id, id_mapping[event.collection_id])
|
||||
Event.where.not(board_id: nil).find_each do |event|
|
||||
if id_mapping[event.board_id]
|
||||
event.update_column(:board_id, id_mapping[event.board_id])
|
||||
end
|
||||
end
|
||||
|
||||
# Update events table (polymorphic relationship)
|
||||
Event.where(eventable_type: "Collection").find_each do |event|
|
||||
Event.where(eventable_type: "Board").find_each do |event|
|
||||
if id_mapping[event.eventable_id]
|
||||
event.update_column(:eventable_id, id_mapping[event.eventable_id])
|
||||
end
|
||||
end
|
||||
|
||||
# Update mentions table (polymorphic relationship)
|
||||
Mention.where(source_type: "Collection").find_each do |mention|
|
||||
Mention.where(source_type: "Board").find_each do |mention|
|
||||
if id_mapping[mention.source_id]
|
||||
mention.update_column(:source_id, id_mapping[mention.source_id])
|
||||
end
|
||||
end
|
||||
|
||||
# Update notifications table (polymorphic relationship)
|
||||
Notification.where(source_type: "Collection").find_each do |notification|
|
||||
Notification.where(source_type: "Board").find_each do |notification|
|
||||
if id_mapping[notification.source_id]
|
||||
notification.update_column(:source_id, id_mapping[notification.source_id])
|
||||
end
|
||||
end
|
||||
|
||||
# Update action_text_markdowns table (polymorphic relationship)
|
||||
ActionText::RichText.where(record_type: "Collection").find_each do |rich_text|
|
||||
ActionText::RichText.where(record_type: "Board").find_each do |rich_text|
|
||||
if id_mapping[rich_text.record_id]
|
||||
rich_text.update_column(:record_id, id_mapping[rich_text.record_id])
|
||||
end
|
||||
end
|
||||
|
||||
# Update active_storage_attachments table (polymorphic relationship)
|
||||
ActiveStorage::Attachment.where(record_type: "Collection").find_each do |attachment|
|
||||
ActiveStorage::Attachment.where(record_type: "Board").find_each do |attachment|
|
||||
if id_mapping[attachment.record_id]
|
||||
attachment.update_column(:record_id, id_mapping[attachment.record_id])
|
||||
end
|
||||
end
|
||||
|
||||
# Reset the collections table IDs
|
||||
puts "Resetting collection IDs..."
|
||||
collections.each do |collection|
|
||||
new_id = id_mapping[collection.id]
|
||||
# Reset the boards table IDs
|
||||
puts "Resetting board IDs..."
|
||||
boards.each do |board|
|
||||
new_id = id_mapping[board.id]
|
||||
# Use direct SQL to update the ID to avoid ActiveRecord validations
|
||||
ApplicationRecord.connection.execute("UPDATE collections SET id = #{new_id} WHERE id = #{collection.id}")
|
||||
ApplicationRecord.connection.execute("UPDATE boards SET id = #{new_id} WHERE id = #{board.id}")
|
||||
end
|
||||
|
||||
# Reset the SQLite sequence for the collections table
|
||||
ApplicationRecord.connection.execute("DELETE FROM sqlite_sequence WHERE name = 'collections'")
|
||||
max_id = Collection.maximum(:id) || 0
|
||||
ApplicationRecord.connection.execute("INSERT INTO sqlite_sequence (name, seq) VALUES ('collections', #{max_id})")
|
||||
# Reset the SQLite sequence for the boards table
|
||||
ApplicationRecord.connection.execute("DELETE FROM sqlite_sequence WHERE name = 'boards'")
|
||||
max_id = Board.maximum(:id) || 0
|
||||
ApplicationRecord.connection.execute("INSERT INTO sqlite_sequence (name, seq) VALUES ('boards', #{max_id})")
|
||||
|
||||
puts "Collection IDs have been reset successfully!"
|
||||
puts "Board IDs have been reset successfully!"
|
||||
rescue => e
|
||||
puts "Error: #{e.message}"
|
||||
puts e.backtrace
|
||||
@@ -112,11 +112,11 @@ ApplicationRecord.with_each_tenant do |tenant|
|
||||
Card.find_each do |card|
|
||||
description = card.description.content.dup
|
||||
|
||||
description.gsub!(/collections\/(\d+)\//) do |match|
|
||||
description.gsub!(/boards\/(\d+)\//) do |match|
|
||||
old_id = $1.to_i
|
||||
new_id = id_mapping[old_id]
|
||||
|
||||
new_id ? "collections/#{new_id}/" : match
|
||||
new_id ? "boards/#{new_id}/" : match
|
||||
end
|
||||
|
||||
if description != card.description.content
|
||||
@@ -128,10 +128,10 @@ ApplicationRecord.with_each_tenant do |tenant|
|
||||
Comment.find_each do |comment|
|
||||
body = comment.body.content.dup
|
||||
|
||||
body.gsub!(/collections\/(\d+)\//) do |match|
|
||||
body.gsub!(/boards\/(\d+)\//) do |match|
|
||||
old_id = $1.to_i
|
||||
new_id = id_mapping[old_id]
|
||||
new_id ? "collections/#{new_id}/" : match
|
||||
new_id ? "boards/#{new_id}/" : match
|
||||
end
|
||||
|
||||
if body != comment.body.content
|
||||
|
||||
Reference in New Issue
Block a user