Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate index names less than 63 bytes #94

Merged
merged 3 commits into from
Jan 11, 2024
Merged

Conversation

rkrage
Copy link
Contributor

@rkrage rkrage commented Nov 20, 2023

This was previously in #86, but that only solved the problem for creating partitioned indexes. This more holistically addresses #89

Essentially backports this feature into older versions of Rails

@@ -80,6 +80,16 @@ def unsafe_add_index(table, column_names, options = {})
raise PgHaMigrations::InvalidMigrationError, "ActiveRecord drops the :opclass option when supplying a string containing an expression or list of columns; instead either supply an array of columns or include the opclass in the string for each column"
end

validated_table = PgHaMigrations::Table.from_table_name(table)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Subtle, but maybe relevant behavior change here. This will raise a PgHaMigrations::UndefinedTableError instead of bubbling up an error from postgres when the table doesn't exist. I think that's okay...

Oh and I think I missed a test for this. Doing that now

Copy link
Contributor

Choose a reason for hiding this comment

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

That seems fine.

@@ -96,9 +96,23 @@ def partitions(include_sub_partitions: false, include_self: false)
end

class Index < Relation
# TODO: implement shortening to ensure < 63 bytes
MAX_NAME_SIZE = 63 # bytes

def self.from_table_and_columns(table, columns)
Copy link
Contributor

Choose a reason for hiding this comment

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

This function is always used for constructing a new index name (i.e., as opposed to getting existing indexes)?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Currently yes, although the class itself has a valid? method which can be used to check for both the existence and validity of an index. This brings up a really good point about the consistency of all of these structs. Like for tables, from_table_name expects the table to exist (and will raise an error if it doesn't). And the optional lock mode on a relation can be both an existing lock and a future target lock. It might be worthwhile to refactor all of this to be more consistent, although I don't have good sense of how to do that (and it most definitely should be in a different PR)

@rkrage rkrage merged commit f26d869 into master Jan 11, 2024
@rkrage rkrage deleted the generate-index-names branch January 11, 2024 16:11
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.

2 participants