Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# This migration comes from co_plan (originally 20260827000000)
class ExpandCoplanReferenceUrls < ActiveRecord::Migration[8.1]
def up
if connection.adapter_name == "PostgreSQL"
remove_url_index_and_expand_column
add_digest_column
add_digest_index
else
add_digest_column
add_digest_index
remove_url_index_and_expand_column
end
end

def down
raise ActiveRecord::IrreversibleMigration,
"reference URLs may exceed the former 255-character limit"
end

private

def add_digest_column
add_column :coplan_references, :url_digest, :virtual, type: :string, limit: 64,
as: digest_expression, stored: true
end

def add_digest_index
add_index :coplan_references, [ :plan_id, :url_digest ], unique: true,
name: "index_coplan_references_on_plan_id_and_url_digest"
end

def remove_url_index_and_expand_column
remove_index :coplan_references, column: [ :plan_id, :url ]
if connection.adapter_name == "Mysql2"
execute "ALTER TABLE coplan_references MODIFY url TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL"
else
change_column :coplan_references, :url, :text, null: false
end
end

def digest_expression
case connection.adapter_name
when "Mysql2"
"SHA2(url, 256)"
when "PostgreSQL"
"encode(sha256(url::bytea), 'hex')"
else
raise "Unsupported database adapter: #{connection.adapter_name}"
end
end
end
7 changes: 4 additions & 3 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
class ExpandCoplanReferenceUrls < ActiveRecord::Migration[8.1]
def up
if connection.adapter_name == "PostgreSQL"
remove_url_index_and_expand_column
add_digest_column
add_digest_index
else
add_digest_column
add_digest_index
remove_url_index_and_expand_column
end
end

def down
raise ActiveRecord::IrreversibleMigration,
"reference URLs may exceed the former 255-character limit"
end

private

def add_digest_column
add_column :coplan_references, :url_digest, :virtual, type: :string, limit: 64,
as: digest_expression, stored: true
end

def add_digest_index
add_index :coplan_references, [ :plan_id, :url_digest ], unique: true,
name: "index_coplan_references_on_plan_id_and_url_digest"
end

def remove_url_index_and_expand_column
remove_index :coplan_references, column: [ :plan_id, :url ]

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Install the digest index before dropping the URL index

On MySQL, each of these DDL statements commits independently, so dropping the original unique index here leaves writes unprotected throughout the subsequent column alteration and digest-index build. If concurrent writers insert the same plan/URL during that window, both model validations can pass and the final add_index ... unique: true can fail, leaving this nontransactional migration partially applied. The generated column addresses the previously reported NULL-digest problem, but the updated ordering provides fresh evidence of this separate gap; create the digest unique index immediately after adding the generated column, before removing the old index.

AGENTS.md reference: AGENTS.md:L30-L36

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Updated by Amp 🤖

The generated-digest unique index is now created before the old URL index is removed on MySQL, so every step of its nontransactional DDL sequence retains database-level uniqueness.

if connection.adapter_name == "Mysql2"
execute "ALTER TABLE coplan_references MODIFY url TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL"
else
change_column :coplan_references, :url, :text, null: false
end
end

def digest_expression
case connection.adapter_name
when "Mysql2"
"SHA2(url, 256)"
Comment on lines +42 to +43

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P2 Badge Preserve MySQL URL collation in the digest

On MySQL, url and its former unique index use the table's case-insensitive utf8mb4_0900_ai_ci collation, while SHA2 hashes the original bytes. Consequently, two concurrent creates for the same plan using URLs that differ only by host or path casing can both pass the model's collation-aware uniqueness query and then receive different digests, allowing rows that both the validator and previous database index considered duplicates. Normalize the digest consistently with the intended URL collation, or make URL comparisons explicitly binary so validation and database enforcement agree.

AGENTS.md reference: AGENTS.md:L30-L36

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Updated by Amp 🤖

MySQL now stores reference URLs with a binary collation, matching PostgreSQL’s case-sensitive equality and the byte-sensitive SHA-256 digest while preserving valid case-sensitive URL paths. Fresh migration and schema-load tests cover the contract on both adapters.

when "PostgreSQL"
"encode(sha256(url::bytea), 'hex')"
else
raise "Unsupported database adapter: #{connection.adapter_name}"
end
end
end
39 changes: 39 additions & 0 deletions spec/models/coplan/reference_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,51 @@
expect(ref).not_to be_valid
end

it "enforces database uniqueness when a writer omits the generated digest" do
create(:reference, plan: plan, url: "https://example.com")
attributes = {
id: SecureRandom.uuid,
plan_id: plan.id,
url: "https://example.com",
reference_type: "link",
source: "extracted",
created_at: Time.current,
updated_at: Time.current
}

expect { described_class.insert_all!([ attributes ]) }.to raise_error(ActiveRecord::RecordNotUnique)
end

it "stores URLs longer than a database string" do
url = "https://example.com/?query=#{"x" * 500}"
ref = create(:reference, plan: plan, url: url)

expect(ref.reload.url).to eq(url)
expect(ref.url_digest).to eq(Digest::SHA256.hexdigest(url))
end

it "updates the database-generated digest when the URL changes" do
ref = create(:reference, plan: plan, url: "https://example.com/old")

ref.update!(url: "https://example.com/new")

expect(ref.reload.url_digest).to eq(Digest::SHA256.hexdigest(ref.url))
end

it "allows same url on different plans" do
other_plan = create(:plan)
create(:reference, plan: plan, url: "https://example.com")
ref = build(:reference, plan: other_plan, url: "https://example.com")
expect(ref).to be_valid
end

it "treats case-sensitive URL paths as distinct" do
create(:reference, plan: plan, url: "https://example.com/Report")

expect {
create(:reference, plan: plan, url: "https://example.com/report")
}.to change(described_class, :count).by(1)
end
end

describe ".classify_url" do
Expand Down
18 changes: 18 additions & 0 deletions spec/requests/api/v1/operations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,24 @@
expect(body["revision"]).to eq(plan.current_revision + 1)
end

it "applies content containing a URL longer than a database string" do
url = "https://example.com/?query=#{"x" * 500}"

post api_v1_plan_operations_path(plan),
params: {
lease_token: lease_token,
base_revision: plan.current_revision,
operations: [
{ op: "replace_exact", old_text: "Some content here.", new_text: "Read [the report](#{url}).", count: 1 }
]
},
headers: headers,
as: :json

expect(response).to have_http_status(:created)
expect(plan.references.find_by!(url: url).source).to eq("extracted")
end

it "apply operations fails without lease" do
CoPlan::EditLease.find_by(plan_id: plan.id)&.destroy

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -116,5 +116,13 @@ def update_content(plan, content)
expect(plan.references.count).to eq(1)
expect(plan.references.first.url).to eq("https://example.com")
end

it "extracts URLs longer than a database string" do
url = "https://example.com/?query=#{"x" * 500}"
update_content(plan, "Visit [the report](#{url}) for more info.")

expect { described_class.call(plan: plan) }.to change(plan.references, :count).by(1)
expect(plan.references.first.url).to eq(url)
end
end
end
Loading