Skip to content

Remove size on floating point data types during MySQL field normalization #110

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 6 additions & 1 deletion lib/SQL/Translator/Parser/MySQL.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,7 @@ sub normalize_field {
$changed = $size != 20;
$size = 20;
}
elsif ( lc $type =~ /(float|double|decimal|numeric|real|fixed|dec)/ ) {
elsif ( lc $type =~ /(decimal|numeric|fixed|dec)/ ) {
my $old_size = (ref $size || '') eq 'ARRAY' ? $size : [];
$changed = @$old_size != 2
|| $old_size->[0] != 8
Expand All @@ -1113,6 +1113,11 @@ sub normalize_field {
}
}

if( $type =~ /^(float|double|real)$/i ) {
$changed = $size;
$size = undef;
}

if ( $type =~ /^tiny(text|blob)$/i ) {
$changed = $size != 255;
$size = 255;
Expand Down
14 changes: 6 additions & 8 deletions t/30sqlt-new-diff-mysql.t
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ plan tests => 9;

use_ok('SQL::Translator::Diff') or die "Cannot continue\n";

my $tr = SQL::Translator->new;

my ( $source_schema, $target_schema, $parsed_sql_schema ) = map {
my ( $source_schema, $target_schema ) = map {
my $t = SQL::Translator->new;
$t->parser( 'YAML' )
or die $tr->error;
or die $t->error;
my $out = $t->translate( catfile($Bin, qw/data diff/, $_ ) )
or die $tr->error;
or die $t->error;

my $schema = $t->schema;
unless ( $schema->name ) {
Expand Down Expand Up @@ -161,9 +159,9 @@ eq_or_diff($out, <<'## END OF DIFF', "No differences found");
{
my $t = SQL::Translator->new;
$t->parser( 'MySQL' )
or die $tr->error;
or die $t->error;
my $out = $t->translate( catfile($Bin, qw/data mysql create.sql/ ) )
or die $tr->error;
or die $t->error;

# Lets remove the renamed table so we dont have to change the SQL or other tests
$target_schema->drop_table('new_name');
Expand Down Expand Up @@ -199,7 +197,7 @@ ALTER TABLE employee DROP FOREIGN KEY FK5302D47D93FE702E,
ALTER TABLE person DROP INDEX UC_age_name,
DROP INDEX u_name,
ADD COLUMN is_rock_star tinyint(4) NULL DEFAULT 1,
ADD COLUMN value double(8, 2) NULL DEFAULT 0.00,
ADD COLUMN value double NULL DEFAULT 0.00,
CHANGE COLUMN person_id person_id integer(11) NOT NULL auto_increment,
CHANGE COLUMN name name varchar(20) NOT NULL,
CHANGE COLUMN age age integer(11) NULL DEFAULT 18,
Expand Down
4 changes: 2 additions & 2 deletions t/30sqlt-new-diff-sqlite.t
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ CREATE TEMPORARY TABLE person_temp_alter (
weight double(11,2),
iq int(11) DEFAULT 0,
is_rock_star tinyint(4) DEFAULT 1,
value double(8,2) DEFAULT 0.00,
value double DEFAULT 0.00,
physical_description text
);

Expand All @@ -143,7 +143,7 @@ CREATE TABLE person (
weight double(11,2),
iq int(11) DEFAULT 0,
is_rock_star tinyint(4) DEFAULT 1,
value double(8,2) DEFAULT 0.00,
value double DEFAULT 0.00,
physical_description text
);

Expand Down
3 changes: 1 addition & 2 deletions t/data/diff/create1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -215,8 +215,7 @@ schema:
name: value
order: 7
size:
- 8
- 2
- 0
indices:
- fields:
- name
Expand Down
3 changes: 1 addition & 2 deletions t/data/diff/create2.yml
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,7 @@ schema:
name: value
order: 7
size:
- 8
- 2
- 0
indices:
- fields:
- name
Expand Down