Skip to content

Conversation

@BuonOmo
Copy link
Collaborator

@BuonOmo BuonOmo commented Oct 4, 2024

This fixes regional related parts, hence fixes #344

@BuonOmo BuonOmo force-pushed the fix-regional-index branch 2 times, most recently from c36330f to 93526f5 Compare October 4, 2024 09:59
@BuonOmo BuonOmo force-pushed the fix-regional-index branch 4 times, most recently from 4c9aa0a to af51719 Compare October 5, 2024 13:12
@BuonOmo BuonOmo changed the title fix: handle indexes with a regional compound fix: handle indexes with a hidden compound Oct 5, 2024
@BuonOmo BuonOmo force-pushed the fix-regional-index branch from af51719 to 9f56778 Compare October 5, 2024 20:28
This fixes regional related parts, hence fixes #344
@BuonOmo BuonOmo force-pushed the fix-regional-index branch from 9f56778 to 9204637 Compare October 6, 2024 11:19
@BuonOmo BuonOmo marked this pull request as ready for review October 7, 2024 10:06
@BuonOmo BuonOmo requested a review from rafiss October 7, 2024 10:07
Comment on lines +78 to +86
return super unless database_version >= 24_02_02

Hash[query(<<~SQL, "SCHEMA")].values_at(*column_numbers).compact
SELECT a.attnum, a.attname
FROM pg_attribute a
WHERE a.attrelid = #{table_oid}
AND a.attnum IN (#{column_numbers.join(", ")})
AND NOT a.attishidden
SQL

Choose a reason for hiding this comment

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

This is not specific to CRDB >= 24.0.2. On 24.1.2 unique constraints on a region by row table will combine the crdb_region and column on the dumped schema. Since attishidden is not available in CRDB 24.1.2 this seems to do the trick:

def column_names_from_column_numbers(table_oid, column_numbers)
  Hash[query(<<~SQL, "SCHEMA")].values_at(*column_numbers).compact
    SELECT a.attnum, a.attname
    FROM pg_attribute a
    WHERE a.attrelid = #{table_oid}
    AND a.attnum IN (#{column_numbers.join(", ")})
    AND a.attisdropped <> 't'
    AND a.attname <> 'crdb_region'
  SQL
end

Without attisdropped <> t, I see weird ....pg.dropped.18... strings in the schema dump. All this is happening after upgrade to Rails 7.1.x and Cockroach AR Adapter 7.1.x. I can't reproduce it on Rails 7.0.x.

When it comes to unique_constraints, I also see a problem with indexes with expressions. For example:

    t.index "lower(name::STRING) ASC", name: "index_table_name_redacted_on_name_unique", unique: true

is becoming:

    t.unique_constraint [], name: "index_table_name_redacted_on_name_unique"

Choose a reason for hiding this comment

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

I created an issue with a workaround for unique_constraint here.

Copy link
Collaborator Author

@BuonOmo BuonOmo Oct 8, 2024

Choose a reason for hiding this comment

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

@mmalek-sa I'm not targetting 24.0.2 but 24.2.2, which is the version where the related issue started raising. We do not need this patch before (hence the conditionnal!)

Also crdb_region is not the only issue, see the tests in this PR they are targetting hash sharded indexes!


EDIT: I understand now there is a second issue. I'll answer in the issue, and I'd rather solve it in a different PR so that we can ship this one quicker as it is blocking migrations for some users of 24.2

Copy link
Contributor

@rafiss rafiss left a comment

Choose a reason for hiding this comment

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

nice, thanks for identifying the fix!

@rafiss rafiss merged commit 356e7b4 into master Oct 10, 2024
4 checks passed
@rafiss rafiss deleted the fix-regional-index branch October 10, 2024 14:16
BuonOmo added a commit that referenced this pull request Nov 22, 2024
BuonOmo added a commit that referenced this pull request Jan 22, 2025
BuonOmo added a commit that referenced this pull request Jan 22, 2025
BuonOmo added a commit that referenced this pull request Jan 22, 2025
BuonOmo added a commit that referenced this pull request Jan 22, 2025
BuonOmo added a commit that referenced this pull request Jan 22, 2025
BuonOmo added a commit that referenced this pull request Jan 22, 2025
BuonOmo added a commit that referenced this pull request Jan 22, 2025
BuonOmo added a commit that referenced this pull request Jan 22, 2025
BuonOmo added a commit that referenced this pull request Jan 22, 2025
BuonOmo added a commit that referenced this pull request Jan 22, 2025
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.

Rails migration fails after upgrading from 24.1 to 24.2 - no indexes found & no support for composite primary keys

3 participants