Skip to content

Commit

Permalink
Proof of concept for index lengths
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrew Beverley committed Aug 11, 2015
1 parent d5103d9 commit e31c185
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
4 changes: 3 additions & 1 deletion lib/SQL/Translator/Producer/MySQL.pm
Original file line number Diff line number Diff line change
Expand Up @@ -684,7 +684,9 @@ sub create_index
$options->{max_id_length} || $DEFAULT_MAX_ID_LENGTH
))
: '',
'(' . join( ', ', map { $generator->quote($_) } $index->fields ) . ')'
'(' . join( ', ', map {
ref $_ ? $generator->quote($_->{name}) . "($_->{size})" : $generator->quote($_)
} $index->fields ) . ')'
);
}

Expand Down
5 changes: 4 additions & 1 deletion lib/SQL/Translator/Role/ListAttr.pm
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ attributes.
=cut

use SQL::Translator::Utils qw(parse_list_arg ex2err uniq);
use SQL::Translator::Utils qw(parse_list_arg ex2err uniq uniq_keys);
use Sub::Quote qw(quote_sub);

use Package::Variant (
Expand Down Expand Up @@ -83,8 +83,11 @@ sub make_variant {
my $may_throw = delete $arguments{may_throw};
my $undef_if_empty = delete $arguments{undef_if_empty};
my $append = delete $arguments{append};
my $uniq_keys = delete $arguments{uniq_keys};
my $coerce = delete $arguments{uniq}
? sub { [ uniq @{parse_list_arg($_[0])} ] }
: $uniq_keys
? sub { [ uniq_keys $uniq_keys, @{parse_list_arg($_[0])} ] }
: \&parse_list_arg;

has($name => (
Expand Down
15 changes: 9 additions & 6 deletions lib/SQL/Translator/Schema/Index.pm
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ names and keep them in order by the first occurrence of a field name.
=cut

with ListAttr fields => ( uniq => 1 );
with ListAttr fields => ( uniq_keys => 'name' );

sub is_valid {

Expand Down Expand Up @@ -173,6 +173,7 @@ around equals => sub {

return 0 unless $self->$orig($other);

# XXX Probably needs changing too
unless ($ignore_index_names) {
unless ((!$self->name && ($other->name eq $other->fields->[0])) ||
(!$other->name && ($self->name eq $self->fields->[0]))) {
Expand All @@ -185,13 +186,15 @@ around equals => sub {
# Check fields, regardless of order
my %otherFields = (); # create a hash of the other fields
foreach my $otherField ($other->fields) {
$otherField = uc($otherField) if $case_insensitive;
$otherFields{$otherField} = 1;
my $name = ref $otherField ? $otherField->{name} : $otherField;
$name = uc($name) if $case_insensitive;
$otherFields{$name} = ref $otherField ? $otherField->{size} : -1;
}
foreach my $selfField ($self->fields) { # check for self fields in hash
$selfField = uc($selfField) if $case_insensitive;
return 0 unless $otherFields{$selfField};
delete $otherFields{$selfField};
my ($name, $size) = ref $selfField ? ($selfField->{name}, $selfField->{size}) : ($selfField, -1);
$name = uc($name) if $case_insensitive;
return 0 unless exists $otherFields{$name} && $otherFields{$name} == $size;
delete $otherFields{$name};
}
# Check all other fields were accounted for
return 0 unless keys %otherFields == 0;
Expand Down
11 changes: 10 additions & 1 deletion lib/SQL/Translator/Utils.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ our @EXPORT_OK = qw(
debug normalize_name header_comment parse_list_arg truncate_id_uniquely
$DEFAULT_COMMENT parse_mysql_version parse_dbms_version
ddl_parser_instance batch_alter_table_statements
uniq throw ex2err carp_ro
uniq uniq_keys throw ex2err carp_ro
normalize_quote_options
);
use constant COLLISION_TAG_LENGTH => 8;
Expand Down Expand Up @@ -375,6 +375,15 @@ sub uniq {
) } @_;
}

sub uniq_keys {
my $key = shift;
my %seen;
grep {
my $name = ref $_ ? $_->{$key} : $_;
not ( $seen{$name}++ );
} @_;
}

sub throw {
die SQL::Translator::Utils::Error->new($_[0]);
}
Expand Down

8 comments on commit e31c185

@ribasushi
Copy link

Choose a reason for hiding this comment

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

It's hard to judge the implementation from a cursory glance without a couple of tests showing "input => output".
Also the uniq_keys() function seems to be misplaced - are you sure a map {} .. won't suffice?

@abeverley
Copy link
Owner

@abeverley abeverley commented on e31c185 Aug 11, 2015 via email

Choose a reason for hiding this comment

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

@ribasushi
Copy link

@ribasushi ribasushi commented on e31c185 Aug 11, 2015 via email

Choose a reason for hiding this comment

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

@ribasushi
Copy link

@ribasushi ribasushi commented on e31c185 Aug 11, 2015 via email

Choose a reason for hiding this comment

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

@abeverley
Copy link
Owner

Choose a reason for hiding this comment

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

I've added another couple of commits, primarily to add tests (I found one bug as a result). The overall diff can be seen here: dbsrgits/sql-translator@master...abeverley:master

I can't see any way of removing the uniq_keys() function, without breaking the compatibility of the uniq() function, unless you can?

Is there enough there to comment on? Docs are still missing, as is support for other databases.

Thanks,

Andy

@ribasushi
Copy link

Choose a reason for hiding this comment

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

@abeverley This looks like the right direction. I am not sure about the actual name of the key size, I need to sleep on it for couple days, see if I can figure out why I dislike it.

But all in all this looks like the right direction. I will look more closely at the constraint thing when I am about to merge this, which should happen in about a week or so.

Thank you!

@abeverley
Copy link
Owner

Choose a reason for hiding this comment

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

Thanks for the feedback. I'll aim to tidy it up and add in some of the missing stuff over the next couple of days.

@abeverley
Copy link
Owner

Choose a reason for hiding this comment

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

Okay, I've tidied it all up, finished it off, and created a PR: dbsrgits#68

If you'd rather the size parameter changed to something else then just let me know.

Thanks, Andy

Please sign in to comment.