From ee3c3c573a2b1dbfc1d7d6bca81d8e637c3f5f1b Mon Sep 17 00:00:00 2001 From: Aidan Cornelius-Bell Date: Sun, 8 Dec 2024 17:35:17 +1030 Subject: [PATCH] Tweaks for older MySQL --- app/controllers/posts_controller.rb | 46 +++++++++++++++---- .../20241208070031_fix_api_response_column.rb | 23 ++++++++++ db/schema.rb | 5 +- 3 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 db/migrate/20241208070031_fix_api_response_column.rb diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 0d03868..fe80e30 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -144,15 +144,43 @@ class PostsController < ApplicationController Rails.logger.debug "Full API result: #{result.inspect}" - # Always store the API response and timestamp - @post.update( - summarizer_api_response: { - response_body: result[:raw_response], - status_code: result[:status_code], - timestamp: Time.current - }, - last_summarized_at: Time.current - ) + # Prepare the API response for storage + begin + # Construct a basic hash with stringified values + api_response = { + response_body: result[:raw_response].to_s, # Ensure string + status_code: result[:status_code].to_s, # Convert to string + timestamp: Time.current.iso8601 + } + + # Convert to JSON string explicitly + json_response = api_response.to_json + + Rails.logger.debug "Attempting to store JSON response: #{json_response}" + + # Store the JSON string + success = @post.update( + summarizer_api_response: json_response, + last_summarized_at: Time.current + ) + + if !success + Rails.logger.error "Failed to update post with API response: #{@post.errors.full_messages}" + end + rescue StandardError => e + Rails.logger.error "Error storing API response: #{e.message}" + Rails.logger.error e.backtrace.join("\n") + # Store a simplified error response + error_json = { + error: e.message, + timestamp: Time.current.iso8601 + }.to_json + + @post.update( + summarizer_api_response: error_json, + last_summarized_at: Time.current + ) + end if result[:success] && result[:parsed_response] summary_text = result[:parsed_response]['output'] || diff --git a/db/migrate/20241208070031_fix_api_response_column.rb b/db/migrate/20241208070031_fix_api_response_column.rb new file mode 100644 index 0000000..bb583f1 --- /dev/null +++ b/db/migrate/20241208070031_fix_api_response_column.rb @@ -0,0 +1,23 @@ +class FixApiResponseColumn < ActiveRecord::Migration[7.2] + def up + # First, remove any existing column to ensure clean state + remove_column :posts, :summarizer_api_response if column_exists?(:posts, :summarizer_api_response) + + # Then add it back with proper JSON type for MySQL + add_column :posts, :summarizer_api_response, :text + + # Add a check constraint to ensure valid JSON (MySQL 8.0.17+) + execute <<-SQL + ALTER TABLE posts + ADD CONSTRAINT check_valid_json + CHECK ( + summarizer_api_response IS NULL OR + JSON_VALID(summarizer_api_response) + ) + SQL + end + + def down + remove_column :posts, :summarizer_api_response + end +end diff --git a/db/schema.rb b/db/schema.rb index e5e4662..6d79b3f 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.2].define(version: 2024_12_08_065057) do +ActiveRecord::Schema[7.2].define(version: 2024_12_08_070031) do create_table "api_keys", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.string "key" t.datetime "created_at", null: false @@ -39,11 +39,12 @@ ActiveRecord::Schema[7.2].define(version: 2024_12_08_065057) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.text "summary" - t.json "summarizer_api_response" t.datetime "last_summarized_at" + t.text "summarizer_api_response" t.index ["post_type"], name: "index_posts_on_post_type" t.index ["published_at"], name: "index_posts_on_published_at" t.index ["slug"], name: "index_posts_on_slug", unique: true + t.check_constraint "(`summarizer_api_response` is null) or json_valid(`summarizer_api_response`)", name: "check_valid_json" end create_table "users", charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| -- 2.39.5