Commit | Line | Data |
591b4fa9 |
1 | #!/usr/bin/env perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More; |
4d4d68c0 |
7 | use Test::Exception; |
591b4fa9 |
8 | use Test::SQL::Translator; |
9 | use SQL::Translator; |
10 | use SQL::Translator::Diff; |
e8ce6480 |
11 | use Digest; |
591b4fa9 |
12 | |
4d4d68c0 |
13 | maybe_plan(undef, 'DBD::Pg'); |
591b4fa9 |
14 | |
0b20d468 |
15 | my ( $pg_tst, $ddl, $ret, $dsn, $user, $pass ); |
4d4d68c0 |
16 | if ($ENV{DBICTEST_PG_DSN}) { |
17 | ($dsn, $user, $pass) = map { $ENV{"DBICTEST_PG_$_"} } qw(DSN USER PASS); |
18 | } |
19 | else { |
20 | no warnings 'once'; |
21 | maybe_plan(undef, 'Test::PostgreSQL'); |
e8ce6480 |
22 | open my $fh, '<:raw', $INC{"Test/PostgreSQL.pm"} or die "No Test::PostgreSQL: $!\n"; |
23 | my $d = Digest->new('MD5'); |
24 | $d->addfile($fh); |
25 | diag sprintf "Test::PostgreSQL %s found at %s (md5: %s)", |
26 | Test::PostgreSQL->VERSION, $INC{"Test/PostgreSQL.pm"}, $d->hexdigest; |
deb63b42 |
27 | $pg_tst = eval { Test::PostgreSQL->new } |
dbdb7508 |
28 | or plan skip_all => "Can't create test database: $Test::PostgreSQL::errstr"; |
0b20d468 |
29 | $dsn = $pg_tst->dsn; |
4d4d68c0 |
30 | }; |
591b4fa9 |
31 | |
4d4d68c0 |
32 | my $dbh = DBI->connect($dsn, $user, $pass, { RaiseError => 1, AutoCommit => 1 }); |
33 | $dbh->do('SET client_min_messages=warning'); |
591b4fa9 |
34 | |
35 | my $source_ddl = <<DDL; |
4d4d68c0 |
36 | CREATE TABLE sqlt_test_foo ( |
591b4fa9 |
37 | pk SERIAL PRIMARY KEY, |
38 | bar VARCHAR(10) |
39 | ); |
40 | DDL |
41 | |
42 | ok( $ret = $dbh->do($source_ddl), "create table" ); |
43 | |
4d4d68c0 |
44 | ok( $ret = $dbh->do(q| INSERT INTO sqlt_test_foo (bar) VALUES ('buzz') |), "insert data" ); |
591b4fa9 |
45 | |
46 | cmp_ok( $ret, '==', 1, "one row inserted" ); |
47 | |
48 | my $target_ddl = <<DDL; |
4d4d68c0 |
49 | CREATE TABLE sqlt_test_fluff ( |
591b4fa9 |
50 | pk SERIAL PRIMARY KEY, |
51 | biff VARCHAR(10) |
52 | ); |
53 | DDL |
54 | |
55 | my $source_sqlt = SQL::Translator->new( |
56 | no_comments => 1, |
57 | parser => 'SQL::Translator::Parser::PostgreSQL', |
58 | )->translate(\$source_ddl); |
59 | |
60 | my $target_sqlt = SQL::Translator->new( |
61 | no_comments => 1, |
62 | parser => 'SQL::Translator::Parser::PostgreSQL', |
63 | )->translate(\$target_ddl); |
64 | |
4d4d68c0 |
65 | my $table = $target_sqlt->get_table('sqlt_test_fluff'); |
66 | $table->extra( renamed_from => 'sqlt_test_foo' ); |
591b4fa9 |
67 | my $field = $table->get_field('biff'); |
68 | $field->extra( renamed_from => 'bar' ); |
69 | |
70 | my @diff = SQL::Translator::Diff->new({ |
71 | output_db => 'PostgreSQL', |
72 | source_schema => $source_sqlt, |
73 | target_schema => $target_sqlt, |
74 | })->compute_differences->produce_diff_sql; |
75 | |
76 | foreach my $line (@diff) { |
77 | $line =~ s/\n//g; |
78 | next if $line =~ /^--/; |
4d4d68c0 |
79 | lives_ok { $dbh->do($line) } "$line"; |
591b4fa9 |
80 | } |
81 | |
4d4d68c0 |
82 | ok ( $ret = $dbh->selectall_arrayref(q(SELECT biff FROM sqlt_test_fluff), { Slice => {} }), "query DB for data" ); |
591b4fa9 |
83 | |
84 | cmp_ok( scalar(@$ret), '==', 1, "Got 1 row"); |
85 | |
86 | cmp_ok( $ret->[0]->{biff}, 'eq', 'buzz', "col biff has value buzz" ); |
4d4d68c0 |
87 | |
88 | # Make sure Test::PostgreSQL can kill Pg |
0b20d468 |
89 | undef $dbh if $pg_tst; |
4d4d68c0 |
90 | |
91 | END { |
0b20d468 |
92 | if ($dbh && !$pg_tst) { |
4d4d68c0 |
93 | $dbh->do("drop table if exists sqlt_test_$_") foreach qw(foo fluff); |
94 | } |
0b20d468 |
95 | elsif( $pg_tst ) { |
96 | # do the teardown ourselves, work around RT#108460 |
97 | local $?; |
98 | $pg_tst->stop; |
99 | 1; |
100 | } |
4d4d68c0 |
101 | } |
102 | |
103 | done_testing; |