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

Add index length option for MySQL #68

Merged
merged 27 commits into from
Nov 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
28a7ca4
Add index length option for MySQL
Sep 2, 2015
065748e
Merge branch 'master' into index-length
Sep 29, 2015
9a738f7
Name index field "size" to "prefix_length", hide plain/refy fieldness
castaway Nov 6, 2015
0d987f2
Merge remote-tracking branch 'upstream/master' into index-length
Feb 5, 2019
cf6c180
Merge remote-tracking branch 'upstream/master' into index-length
abeverley Sep 15, 2022
1213f8e
Merge remote-tracking branch 'castaway/index-length' into index-length
abeverley Sep 15, 2022
b56de88
Fix failing tests
abeverley Sep 15, 2022
71ac26d
Fix failing test
abeverley Sep 15, 2022
e48176e
Fix incorrect index names in YAML producer
abeverley Sep 15, 2022
373a011
Change missed key to new prefix_length name
abeverley Sep 16, 2022
2e52bd6
Fix index lengths not being produced in YAML
abeverley Sep 16, 2022
f3a8e27
Merge remote-tracking branch 'upstream/master' into index-length
abeverley Jun 18, 2023
1c413ca
Fix failing tests
abeverley Jun 18, 2023
a10be29
Fix warnings when running tests
abeverley Jun 18, 2023
d064562
Enable other options in index field names
abeverley Jun 19, 2023
aff5e8d
chore: rollback the fields -> field_names changes in the producers
rabbiveesh Jun 19, 2023
cbd2d4f
feat(IndexField): upgrade all index fields to use a new IndexField ob…
rabbiveesh Jun 21, 2023
f08bf6a
feat(MySQL): upgrade producer to support prefix_length w/ new objects
rabbiveesh Jun 21, 2023
e267a95
fix: oops, actually put in the sort routine :face_palm:
rabbiveesh Jun 21, 2023
1a0a314
fix(parse_list_arg): don't stringify the tings!
rabbiveesh Jun 21, 2023
a26cff0
fix: handle weird YAML double objecting
rabbiveesh Jun 21, 2023
bfc7565
Write index extra properties in YAML producer
abeverley Jul 22, 2023
905fa34
Fix failing test
abeverley Jul 22, 2023
79bfc70
Fix failing lint test
abeverley Jul 22, 2023
28d3d2f
chore: finish up here
rabbiveesh Nov 25, 2023
a2d2e6c
Merge branch 'master' into index-length
rabbiveesh Nov 25, 2023
3aa5cd8
Merge pull request #2 from dbsrgits/index-length
abeverley Nov 26, 2023
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
4 changes: 1 addition & 3 deletions lib/SQL/Translator/Generator/DDL/SQLServer.pm
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,7 @@ sub index {
'CREATE INDEX ' .
$_[0]->quote($_[1]->name || $_[1]->table->name . '_idx') .
' ON ' . $_[0]->quote($_[1]->table->name) .
' (' . join( ', ', map {
ref $_ ? $_[0]->quote($_->{name}) : $_[0]->quote($_)
} $_[1]->fields ) . ');'
' (' . join( ', ', map $_[0]->quote($_), $_[1]->field_names ) . ');'
}

sub unique_constraint_single {
Expand Down
4 changes: 1 addition & 3 deletions lib/SQL/Translator/Producer/DB2.pm
Original file line number Diff line number Diff line change
Expand Up @@ -310,9 +310,7 @@ sub create_index
$index->type() =~ /^UNIQUE$/i ? 'UNIQUE' : '',
$index->name,
$index->table->name,
join(', ',
map { ref $_ ? $_->{name} : $_ } $index->fields
) );
join(', ', $index->field_names) );

return $out;
}
Expand Down
4 changes: 1 addition & 3 deletions lib/SQL/Translator/Producer/GraphViz.pm
Original file line number Diff line number Diff line change
Expand Up @@ -465,9 +465,7 @@ sub produce {
? $index->name . ':'
: ()
,
join (', ',
map { ref $_ ? "$_->{name}($_->{size})" : $_ } $index->fields
),
join (', ', $index->field_names_with_lengths),
($index->type eq 'UNIQUE') ? '[U]' : (),
);
}
Expand Down
4 changes: 1 addition & 3 deletions lib/SQL/Translator/Producer/HTML.pm
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,7 @@ sub produce {

for my $index ( @indices ) {
my $name = $index->name || '';
my $fields = join( ', ',
map { ref $_ ? "$_->{name}($_->{size})" : $_ } $index->fields
) || '';
my $fields = join( ', ', $index->field_names_with_lengths ) || '';

push @html,
$q->Tr({ -class => 'IndexCell' },
Expand Down
4 changes: 2 additions & 2 deletions lib/SQL/Translator/Producer/MySQL.pm
Original file line number Diff line number Diff line change
Expand Up @@ -684,8 +684,8 @@ sub create_index
$options->{max_id_length} || $DEFAULT_MAX_ID_LENGTH
))
: '',
'(' . join( ', ', map {
ref $_ ? $generator->quote($_->{name}) . "($_->{size})" : $generator->quote($_)
'(' . join( ', ', map {
ref $_ ? $generator->quote($_->{name}) . "($_->{prefix_length})" : $generator->quote($_)
} $index->fields ) . ')'
);
}
Expand Down
4 changes: 1 addition & 3 deletions lib/SQL/Translator/Producer/Oracle.pm
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,7 @@ sub create_table {
for my $index ( $table->get_indices ) {
my $index_name = $index->name || '';
my $index_type = $index->type || NORMAL;
my @fields = map {
ref $_ ? quote($_->{name}, $qf) : quote($_, $qf)
} $index->fields;
my @fields = map { quote($_, $qf) } $index->field_names;
next unless @fields;

my @index_options;
Expand Down
4 changes: 1 addition & 3 deletions lib/SQL/Translator/Producer/POD.pm
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,7 @@ sub produce {
for my $index ( @indices ) {
$pod .= "=head4 " . $index->type . "\n\n=over 4\n\n";
$pod .= "=item * Fields = " .
join(', ',
map { ref $_ ? "$_->{name}($_->{size})" : $_ } $index->fields
) . "\n\n";
join(', ', $index->field_names_with_lengths ) . "\n\n";
$pod .= "=back\n\n";
}
}
Expand Down
7 changes: 2 additions & 5 deletions lib/SQL/Translator/Producer/PostgreSQL.pm
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ sub create_geometry_constraints {
|| join('_', $table_name, 'idx', ++$index_name{ $table_name });

my $type = $index->type || NORMAL;
my @fields = $index->fields;
my @fields = $index->field_names;
return unless @fields;

my %index_extras;
Expand All @@ -645,10 +645,7 @@ sub create_geometry_constraints {
}

my $def_start = 'CONSTRAINT ' . $generator->quote($name) . ' ';
my $field_names = '(' . join(", ", (map {
my $name = ref $_ ? $_->{name} : $_;
$name =~ /\(.*\)/ ? $name : ( $generator->quote($name) );
} @fields)) . ')';
my $field_names = '(' . join(", ", (map { $_ =~ /\(.*\)/ ? $_ : ( $generator->quote($_) ) } @fields)) . ')';
if ( $type eq PRIMARY_KEY ) {
push @constraint_defs, "${def_start}PRIMARY KEY ".$field_names;
}
Expand Down
6 changes: 1 addition & 5 deletions lib/SQL/Translator/Producer/SQLite.pm
Original file line number Diff line number Diff line change
Expand Up @@ -299,11 +299,7 @@ sub create_index
my $type = $index->type eq 'UNIQUE' ? "UNIQUE " : '';

# strip any field size qualifiers as SQLite doesn't like these
my @fields = map {
$_ = $_->{name} if ref $_; # Remove any index lengths
s/\(\d+\)$//;
_generator()->quote($_)
} $index->fields;
my @fields = map { s/\(\d+\)$//; _generator()->quote($_) } $index->field_names;
$index_table_name = _generator()->quote($index_table_name);
warn "removing schema name from '" . $index->table->name . "' to make '$index_table_name'\n" if $WARN;
my $index_def =
Expand Down
4 changes: 1 addition & 3 deletions lib/SQL/Translator/Producer/Sybase.pm
Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,7 @@ sub produce {
push @index_defs,
'CREATE INDEX ' . $index->name .
" ON $table_name (".
join( ', ',
map { ref $_ ? $_->{name} : $_ } $index->fields
) . ")";
join( ', ',$index->field_names ) . ")";
}

my $drop_statement = $add_drop_table
Expand Down
31 changes: 29 additions & 2 deletions lib/SQL/Translator/Schema/Index.pm
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ names and keep them in order by the first occurrence of a field name.
The length of an index can be specified as follows (only has any effect
with a MySQL database):

$index->fields( 'id', { name => 'firstname', size => 15 } );
$index->fields( 'id', { name => 'firstname', prefix_length => 15 } );

=cut

Expand Down Expand Up @@ -112,6 +112,33 @@ has name => (
default => quote_sub(q{ '' }),
);

=head2 field_names

Return just the index field names for the case when we don't care whether
the "prefix_length" is specified or not.

=cut

sub field_names {
my ($self) = @_;

return ( map { ref $_ ? $_->{name} : $_ } ($self->fields) );
}

=head2 fields_with_lengths

Return the index field names with the prefix_length appended if set.

=cut

sub fields_with_lengths {
my ($self) = @_;

print STDERR Data::Dumper::Dumper($self->fields);
return ( map { ref $_ ? "$_->{name}($_->{prefix_length})" : $_ }
($self->fields) );
}

=head2 options

Get or set the index's options (e.g., "using" or "where" for PG). Returns
Expand Down Expand Up @@ -197,7 +224,7 @@ around equals => sub {
$otherFields{$name} = ref $otherField ? $otherField->{size} : -1; # -1 == no length. Easier comparison.
}
foreach my $selfField ($self->fields) { # check for self fields in hash
my ($name, $size) = ref $selfField ? ($selfField->{name}, $selfField->{size}) : ($selfField, -1);
my ($name, $size) = ref $selfField ? ($selfField->{name}, $selfField->{prefix_length}) : ($selfField, -1);
$name = uc($name) if $case_insensitive;
return 0 unless exists $otherFields{$name} && $otherFields{$name} == $size;
delete $otherFields{$name};
Expand Down
2 changes: 1 addition & 1 deletion t/13schema.t
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ require_ok( 'SQL::Translator::Schema' );
isa_ok( $index2, 'SQL::Translator::Schema::Index', 'Index' );
is( $index2->name, 'bar', 'Index name is "bar"' );

my $index3 = $person_table->add_index( name => "sized", fields => [ { name => 'forename', size => 15} ] )
my $index3 = $person_table->add_index( name => "sized", fields => [ { name => 'forename', prefix_length => 15} ] )
or warn $person_table->error;
isa_ok( $index3, 'SQL::Translator::Schema::Index', 'Index' );
is( $index3->name, 'sized', 'Index name is "sized"' );
Expand Down
6 changes: 3 additions & 3 deletions t/38-mysql-producer.t
Original file line number Diff line number Diff line change
Expand Up @@ -798,16 +798,16 @@ EOV
}

{
my $index = $table->add_index(fields => [ { name => 'foo', size => 25 } ], type => 'unique');
my $index = $table->add_index(fields => [ { name => 'foo', prefix_length => 25 } ], type => 'unique');
my ($def) = SQL::Translator::Producer::MySQL::create_index($index);
is($def, 'UNIQUE INDEX (foo(25))', 'unique index created');
}

{
my $index = $table->add_index(name => 'sized', fields => [
'foobar',
{ name => 'foo', size => 10 },
{ name => 'bar', size => 15 },
{ name => 'foo', prefix_length => 10 },
{ name => 'bar', prefix_length => 15 },
]);
my ($def) = SQL::Translator::Producer::MySQL::create_index($index);
is($def, 'INDEX sized (foobar, foo(10), bar(15))', 'index created');
Expand Down
2 changes: 1 addition & 1 deletion t/55-oracle-producer.t
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ use SQL::Translator::Producer::Oracle;
);

my $index1 = $table1->add_index(name => 'myfooindex', fields => ['foo']);
my $index2 = $table1->add_index(name => 'mybarindex', fields => [ { name => 'bar', size => 10 } ]);
my $index2 = $table1->add_index(name => 'mybarindex', fields => [ { name => 'bar', prefix_length => 10 } ]);

my ($table1_def, $fk1_def, $trigger1_def,
$index1_def, $constraint1_def
Expand Down
2 changes: 1 addition & 1 deletion t/56-sqlite-producer.t
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ $SQL::Translator::Producer::SQLite::NO_QUOTES = 0;
}

{
my $index = $table->add_index(name => 'myindex2', fields => [ { name => 'foo', size => 15 } ]);
my $index = $table->add_index(name => 'myindex2', fields => [ { name => 'foo', prefix_length => 15 } ]);
my ($def) = SQL::Translator::Producer::SQLite::create_index($index);
is($def, 'CREATE INDEX "myindex2" ON "foobar" ("foo")', 'index created');
}
Expand Down
2 changes: 1 addition & 1 deletion t/75-sqlserver-producer.t
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ schema:
- type: NORMAL
fields:
- name: id
size: 10
prefix_length: 10
name: index_1
- type: NORMAL
fields:
Expand Down