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

WIP: quoting of identifiers in diff, SQLite and test #96

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
5 changes: 3 additions & 2 deletions lib/SQL/Translator/Producer/SQLite.pm
Original file line number Diff line number Diff line change
Expand Up @@ -386,10 +386,11 @@ sub create_trigger {
sub alter_table { () } # Noop

sub add_field {
my ($field) = @_;
my ($field,$options) = @_;

return sprintf("ALTER TABLE %s ADD COLUMN %s",
_generator()->quote($field->table->name), create_field($field))
_generator($options)->quote($field->table->name),
create_field($field, $options))
}

sub alter_create_index {
Expand Down
63 changes: 63 additions & 0 deletions t/XX-quote-diff.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use v5.24.0;
use warnings;

use SQL::Translator;
use SQL::Translator::Diff;

use Test::More;

sub schema_pair {
my $y1 = <<'Y1';
---
schema:
tables:
foo:
name: foo
fields:
foo: { order: 1, name: foo, data_type: varchar, size: 36 }
Y1

my $y2 = <<'Y2';
---
schema:
tables:
foo:
name: foo
fields:
foo: { order: 1, name: foo, data_type: varchar, size: 36 }
bar: { order: 2, name: bar, data_type: varchar, size: 36 }
Y2

my $t1 = SQL::Translator->new(parser => "YAML", quote_identifiers => 1);
$t1->translate(\$y1);

my $t2 = SQL::Translator->new(parser => "YAML", quote_identifiers => 1);
$t2->translate(\$y2);

return ($t1->schema, $t2->schema);
}

for my $test (
[ MySQL => sub { "`$_[0]`" } ],
[ PostgreSQL => sub { qq{"$_[0]"} } ],
[ SQLite => sub { qq{"$_[0]"} } ],
) {
my ($producer, $q) = @$test;

my ($s1, $s2) = schema_pair;

my $sql = SQL::Translator::Diff::schema_diff(
$s1, $producer,
$s2, $producer,
{ producer_args => { quote_identifiers => 1 } },
);

my $quoted = $q->('bar');
like(
$sql,
qr{ADD COLUMN \Q$quoted\E},
"$producer: new column name is quoted",
);
}

done_testing;