Work round MySQL/InnoDB bug http://bugs.mysql.com/bug.php?id=13741
[dbsrgits/SQL-Translator.git] / t / 30sqlt-new-diff-sqlite.t
CommitLineData
4d438549 1#!/usr/bin/perl
2# vim: set ft=perl:
3
4use strict;
5use warnings;
6use SQL::Translator;
7
8use File::Spec::Functions qw(catfile updir tmpdir);
9use FindBin qw($Bin);
10use Test::More;
11use Test::Differences;
12use Test::SQL::Translator qw(maybe_plan);
13
14plan tests => 4;
15
16use_ok('SQL::Translator::Diff') or die "Cannot continue\n";
17
18my $tr = SQL::Translator->new;
19
20my ( $source_schema, $target_schema ) = map {
21 my $t = SQL::Translator->new;
22 $t->parser( 'YAML' )
23 or die $tr->error;
24 my $out = $t->translate( catfile($Bin, qw/data diff/, $_ ) )
25 or die $tr->error;
26
27 my $schema = $t->schema;
28 unless ( $schema->name ) {
29 $schema->name( $_ );
30 }
31 ($schema);
32} (qw/create1.yml create2.yml/);
33
34# Test for differences
35my $out = SQL::Translator::Diff::schema_diff( $source_schema, 'SQLite', $target_schema, 'SQLite',
36 { no_batch_alters => 1,
37 ignore_missing_methods => 1,
38 output_db => 'SQLite',
39 }
40);
41eq_or_diff($out, <<'## END OF DIFF', "Diff as expected");
42-- Convert schema 'create1.yml' to 'create2.yml':
43
44BEGIN TRANSACTION;
45
46CREATE TABLE added (
47 id int(11)
48);
49
50
46bf5655 51ALTER TABLE old_name RENAME TO new_name;
4d438549 52DROP INDEX FK5302D47D93FE702E ON employee;
53DROP INDEX UC_age_name ON person;
54DROP INDEX u_name ON person;
55-- SQL::Translator::Producer::SQLite cant drop_field
46bf5655 56ALTER TABLE new_name ADD COLUMN new_field int;
4d438549 57ALTER TABLE person ADD COLUMN is_rock_star tinyint(4) DEFAULT '1';
58-- SQL::Translator::Producer::SQLite cant alter_field
59-- SQL::Translator::Producer::SQLite cant rename_field
60CREATE UNIQUE INDEX unique_name_person ON person (name);
61CREATE UNIQUE INDEX UC_person_id_person ON person (person_id);
62CREATE UNIQUE INDEX UC_age_name_person ON person (age, name);
63DROP TABLE deleted;
64
65COMMIT;
66## END OF DIFF
67
68
69$out = SQL::Translator::Diff::schema_diff($source_schema, 'SQLite', $target_schema, 'SQLite',
70 { ignore_index_names => 1,
71 ignore_constraint_names => 1,
72 output_db => 'SQLite',
73 });
74
75eq_or_diff($out, <<'## END OF DIFF', "Diff as expected");
76-- Convert schema 'create1.yml' to 'create2.yml':
77
78BEGIN TRANSACTION;
79
80CREATE TABLE added (
81 id int(11)
82);
83
84
85
86CREATE TEMPORARY TABLE employee_temp_alter (
87 position varchar(50) NOT NULL,
88 employee_id int(11) NOT NULL,
89 PRIMARY KEY (position, employee_id)
90);
91INSERT INTO employee_temp_alter SELECT position, employee_id FROM employee;
92DROP TABLE employee;
93CREATE TABLE employee (
94 position varchar(50) NOT NULL,
95 employee_id int(11) NOT NULL,
96 PRIMARY KEY (position, employee_id)
97);
98INSERT INTO employee SELECT position, employee_id FROM employee_temp_alter;
99DROP TABLE employee_temp_alter;
100
46bf5655 101ALTER TABLE old_name RENAME TO new_name;
102ALTER TABLE new_name ADD COLUMN new_field int;
4d438549 103CREATE TEMPORARY TABLE person_temp_alter (
104 person_id INTEGER PRIMARY KEY NOT NULL,
105 name varchar(20) NOT NULL,
106 age int(11) DEFAULT '18',
107 weight double(11,2),
108 iq int(11) DEFAULT '0',
109 is_rock_star tinyint(4) DEFAULT '1',
110 physical_description text
111);
112INSERT INTO person_temp_alter SELECT person_id, name, age, weight, iq, is_rock_star, physical_description FROM person;
113DROP TABLE person;
114CREATE TABLE person (
115 person_id INTEGER PRIMARY KEY NOT NULL,
116 name varchar(20) NOT NULL,
117 age int(11) DEFAULT '18',
118 weight double(11,2),
119 iq int(11) DEFAULT '0',
120 is_rock_star tinyint(4) DEFAULT '1',
121 physical_description text
122);
123CREATE UNIQUE INDEX unique_name_person02 ON person (name);
124CREATE UNIQUE INDEX UC_person_id_person02 ON person (person_id);
125CREATE UNIQUE INDEX UC_age_name_person02 ON person (age, name);
126INSERT INTO person SELECT person_id, name, age, weight, iq, is_rock_star, physical_description FROM person_temp_alter;
127DROP TABLE person_temp_alter;
128
129DROP TABLE deleted;
130
131COMMIT;
132## END OF DIFF
133
134
135# Test for sameness
136$out = SQL::Translator::Diff::schema_diff($source_schema, 'MySQL', $source_schema, 'MySQL' );
137
138eq_or_diff($out, <<'## END OF DIFF', "No differences found");
139-- Convert schema 'create1.yml' to 'create1.yml':
140
141-- No differences found
142
143## END OF DIFF
144