Skip to content

Comments

feat: add json support#1

Merged
sncalvo merged 1 commit intomainfrom
feat/add-json-support
Feb 3, 2026
Merged

feat: add json support#1
sncalvo merged 1 commit intomainfrom
feat/add-json-support

Conversation

@sncalvo
Copy link
Member

@sncalvo sncalvo commented Feb 3, 2026

json support was added to psql and mysql

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR adds JSON column support for PostgreSQL (using JSONB) and MySQL (using JSON) databases. The implementation automatically serializes Ruby Arrays and Hashes to JSON strings during bulk insertion, enabling seamless storage and retrieval of structured data in JSON columns.

Changes:

  • Modified the BulkInserter#quote method to detect and serialize Array/Hash values to JSON
  • Updated test database schemas to include tags (array) and metadata (hash) JSON columns
  • Added comprehensive tests for JSON column handling in PostgreSQL and MySQL contexts

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 3 comments.

File Description
lib/staging_table/bulk_inserter.rb Enhanced the quote method to automatically serialize Array and Hash values to JSON strings before database insertion
spec/support/database_helper.rb Added JSON column definitions to test tables, using JSONB for PostgreSQL, JSON for MySQL, and TEXT for SQLite
spec/staging_table/bulk_inserter_spec.rb Added test coverage for JSON array and hash handling, correctly scoped to PostgreSQL and MySQL (SQLite excluded due to TEXT column limitations)

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

connection.quote(value)
case value
when Array, Hash
connection.quote(value.to_json)
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The to_json method is being used without explicitly requiring the JSON library. While ActiveSupport provides to_json for Array and Hash, it would be more robust to add require "json" at the top of this file to ensure the method is available, especially if this library is used in contexts where ActiveSupport might not load all core extensions.

Copilot uses AI. Check for mistakes.
DROP TABLE IF EXISTS test_users
SQL

json_type = ActiveRecord::Base.connection.adapter_name.downcase.include?("postgresql") ? "JSONB" : "JSON"
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The adapter detection uses include?("postgresql") which is inconsistent with the pattern matching approach used in lib/staging_table/adapters/base.rb:24 where regex patterns like /postgresql/ are used. Consider using the same pattern matching approach for consistency: adapter_name.downcase =~ /postgresql/ instead of include?("postgresql").

Copilot uses AI. Check for mistakes.
Comment on lines +94 to +115
it "handles array values for JSON/JSONB columns" do
records = [
{name: "John", email: "john@example.com", tags: %w[admin user]}
]

inserter.insert(records)

user = staging_model.first
expect(user.tags).to eq(%w[admin user])
end

it "handles hash values for JSON/JSONB columns" do
records = [
{name: "John", email: "john@example.com", metadata: {role: "admin", level: 5}}
]

inserter.insert(records)

user = staging_model.first
expect(user.metadata).to eq({"role" => "admin", "level" => 5})
end
end
Copy link

Copilot AI Feb 3, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding test cases for edge cases with JSON columns, such as: empty arrays ([]), empty hashes ({}), nested structures, and null values for JSON columns. These would help ensure the JSON serialization handles all scenarios correctly.

Copilot uses AI. Check for mistakes.
json support was added to psql and mysql
@sncalvo sncalvo force-pushed the feat/add-json-support branch from 1d97d0f to 1fafa32 Compare February 3, 2026 21:27
@sncalvo sncalvo merged commit af4da59 into main Feb 3, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant