From 6f4030802969e729448e21ddd71452caf3d3e36c Mon Sep 17 00:00:00 2001 From: Claude Date: Sat, 31 Oct 2026 11:52:32 -0400 Subject: [PATCH] Fix migration 050 on MySQL; add the estimate status column - 050 wrote the kept rows' times back with their zone offset ('2026-10-31 10:06:08 -0400'), which MySQL refuses; dates and times are now written with Rails' quoted_date. Each record's delete and re-insert run in one transaction, so a failure can't leave it deleted. - 051 adds estimates.txn_status for databases that ran migration 047 before that column was added to it. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01BiwYMjhoQvL7XsKKfRz4pM --- db/migrate/050_unique_qbo_ids.rb | 18 +++++++++++++++--- db/migrate/051_add_estimate_status.rb | 22 ++++++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 db/migrate/051_add_estimate_status.rb diff --git a/db/migrate/050_unique_qbo_ids.rb b/db/migrate/050_unique_qbo_ids.rb index c2877f3..9c5540b 100644 --- a/db/migrate/050_unique_qbo_ids.rb +++ b/db/migrate/050_unique_qbo_ids.rb @@ -43,10 +43,22 @@ class UniqueQboIds < ActiveRecord::Migration[7.0] rows = select_all("SELECT * FROM #{quoted} WHERE id = #{quote(id)}").to_a keep = rows.max_by { |row| [row['updated_at'].to_s, row.values.compact.size] } columns = keep.keys + values = columns.map { |c| sql_value(keep[c]) } - execute "DELETE FROM #{quoted} WHERE id = #{quote(id)}" - execute "INSERT INTO #{quoted} (#{columns.map { |c| quote_column_name(c) }.join(', ')}) " \ - "VALUES (#{columns.map { |c| quote(keep[c]) }.join(', ')})" + # Both or neither: a failed insert must not leave the record deleted + transaction do + execute "DELETE FROM #{quoted} WHERE id = #{quote(id)}" + execute "INSERT INTO #{quoted} (#{columns.map { |c| quote_column_name(c) }.join(', ')}) VALUES (#{values.join(', ')})" + end end end + + # A value read back from the table, quoted for writing it again. Dates and times are formatted the way + # Rails writes them (e.g. '2026-10-31 14:06:08'), not with a time zone offset, which MySQL refuses. + def sql_value(value) + return 'NULL' if value.nil? + return quote(connection.quoted_date(value)) if value.is_a?(Time) || value.is_a?(Date) || value.is_a?(DateTime) + + quote(value) + end end diff --git a/db/migrate/051_add_estimate_status.rb b/db/migrate/051_add_estimate_status.rb new file mode 100644 index 0000000..de443eb --- /dev/null +++ b/db/migrate/051_add_estimate_status.rb @@ -0,0 +1,22 @@ +#The MIT License (MIT) +# +#Copyright (c) 2026 rick barrette +# +#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: +# +#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. +# +#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + + +# Each estimate's QuickBooks status (Converted and Closed estimates are billed, so lists show View instead of +# Edit). Added to migration 047 after some databases had already run it, so it's added here for those. +class AddEstimateStatus < ActiveRecord::Migration[7.0] + def up + add_column :estimates, :txn_status, :string unless column_exists?(:estimates, :txn_status) + end + + def down + remove_column :estimates, :txn_status if column_exists?(:estimates, :txn_status) + end +end