]> gitweb.mndrdr.org Git - arelpe.git/commitdiff
Tweaks for older MySQL
authorAidan Cornelius-Bell <[email protected]>
Sun, 8 Dec 2024 07:05:17 +0000 (17:35 +1030)
committerAidan Cornelius-Bell <[email protected]>
Sun, 8 Dec 2024 07:05:17 +0000 (17:35 +1030)
app/controllers/posts_controller.rb
db/migrate/20241208070031_fix_api_response_column.rb [new file with mode: 0644]
db/schema.rb

index 0d038683374f1bcafd583a3d4f093f7b31a3602b..fe80e30def5f296281d2fc10a38636fd9d0c7001 100644 (file)
@@ -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 (file)
index 0000000..bb583f1
--- /dev/null
@@ -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
index e5e4662220bbf5f54b3f5979187b9bff00ae81da..6d79b3f1fa82d8affd5f15aeee6e1df97192e160 100644 (file)
@@ -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|