Commit | Line | Data |
819fe9ef |
1 | #!/usr/bin/perl -w |
1ded8513 |
2 | # vim:filetype=perl |
3 | |
4 | # |
5 | # Note that the bulk of the testing for the mysql producer is in |
6 | # 08postgres-to-mysql.t. This test is for additional stuff that can't be tested |
7 | # using an Oracle schema as source e.g. extra attributes. |
8 | # |
9 | |
10 | use strict; |
11 | use Test::More; |
12 | use Test::Exception; |
13 | use Test::SQL::Translator qw(maybe_plan); |
14 | |
15 | use Data::Dumper; |
16 | use FindBin qw/$Bin/; |
17 | |
18 | # Testing 1,2,3,4... |
19 | #============================================================================= |
20 | |
21 | BEGIN { |
fe0f47d0 |
22 | maybe_plan(7, |
1ded8513 |
23 | 'YAML', |
24 | 'SQL::Translator::Producer::MySQL', |
25 | 'Test::Differences', |
26 | ) |
27 | } |
28 | use Test::Differences; |
29 | use SQL::Translator; |
30 | |
31 | # Main test. |
32 | { |
33 | my $yaml_in = <<EOSCHEMA; |
34 | --- |
35 | schema: |
36 | tables: |
37 | thing: |
38 | name: thing |
39 | extra: |
40 | mysql_charset: latin1 |
41 | mysql_collate: latin1_danish_ci |
42 | order: 1 |
43 | fields: |
8c4efd11 |
44 | id: |
45 | name: id |
46 | data_type: unsigned int |
47 | is_primary_key: 1 |
48 | is_auto_increment: 1 |
49 | order: 0 |
1ded8513 |
50 | name: |
51 | name: name |
52 | data_type: varchar |
53 | size: |
54 | - 32 |
55 | order: 1 |
56 | swedish_name: |
57 | name: swedish_name |
58 | data_type: varchar |
59 | size: 32 |
60 | extra: |
61 | mysql_charset: swe7 |
62 | order: 2 |
63 | description: |
64 | name: description |
65 | data_type: text |
66 | extra: |
67 | mysql_charset: utf8 |
68 | mysql_collate: utf8_general_ci |
69 | order: 3 |
fe0f47d0 |
70 | constraints: |
71 | - type: UNIQUE |
72 | fields: |
73 | - name |
74 | name: idx_unique_name |
8c4efd11 |
75 | thing2: |
76 | name: thing2 |
77 | extra: |
78 | order: 2 |
79 | fields: |
80 | id: |
81 | name: id |
82 | data_type: int |
83 | is_primary_key: 0 |
84 | order: 0 |
85 | is_foreign_key: 1 |
86 | foo: |
87 | name: foo |
88 | data_type: int |
89 | order: 1 |
90 | is_not_null: 1 |
91 | constraints: |
92 | - type: PRIMARY_KEY |
93 | fields: |
94 | - id |
95 | - foo |
96 | - reference_table: thing |
97 | type: FOREIGN_KEY |
98 | fields: foo |
99 | name: fk_thing |
100 | |
1ded8513 |
101 | EOSCHEMA |
102 | |
103 | my $mysql_out = <<EOSQL; |
104 | SET foreign_key_checks=0; |
105 | |
fe0f47d0 |
106 | CREATE TABLE `thing` ( |
107 | `id` unsigned int auto_increment, |
108 | `name` varchar(32), |
109 | `swedish_name` varchar(32) CHARACTER SET swe7, |
110 | `description` text CHARACTER SET utf8 COLLATE utf8_general_ci, |
111 | INDEX (`id`), |
112 | INDEX (`name`), |
113 | PRIMARY KEY (`id`), |
114 | UNIQUE `idx_unique_name` (`name`) |
819fe9ef |
115 | ) Type=InnoDB DEFAULT CHARACTER SET latin1 COLLATE latin1_danish_ci; |
1ded8513 |
116 | |
fe0f47d0 |
117 | CREATE TABLE `thing2` ( |
118 | `id` integer, |
119 | `foo` integer, |
120 | INDEX (`id`), |
121 | INDEX (`foo`), |
122 | PRIMARY KEY (`id`, `foo`), |
123 | CONSTRAINT `thing2_fk_thing` FOREIGN KEY (`foo`) REFERENCES `thing` (`id`) |
8c4efd11 |
124 | ) Type=InnoDB; |
125 | |
126 | SET foreign_key_checks=1; |
127 | |
1ded8513 |
128 | EOSQL |
129 | |
130 | my $sqlt; |
131 | $sqlt = SQL::Translator->new( |
132 | show_warnings => 1, |
133 | no_comments => 1, |
8c4efd11 |
134 | # debug => 1, |
1ded8513 |
135 | from => "YAML", |
136 | to => "MySQL", |
fe0f47d0 |
137 | quote_table_names => 1, |
138 | quote_field_names => 1 |
1ded8513 |
139 | ); |
140 | |
819fe9ef |
141 | my $out = $sqlt->translate(\$yaml_in) |
142 | or die "Translate error:".$sqlt->error; |
1ded8513 |
143 | ok $out ne "" ,"Produced something!"; |
fe0f47d0 |
144 | eq_or_diff $out, $mysql_out ,"Output looks right with quoting"; |
145 | |
146 | @{$sqlt}{qw/quote_table_names quote_field_names/} = (0,0); |
147 | |
148 | $out = $sqlt->translate(\$yaml_in) |
149 | or die "Translat eerror:".$sqlt->error; |
150 | $mysql_out =~ s/`//g; |
151 | eq_or_diff $out, $mysql_out, "Output looks right without quoting"; |
1ded8513 |
152 | } |
8db4bd9d |
153 | |
154 | ############################################################################### |
155 | # New alter/add subs |
156 | |
157 | my $table = SQL::Translator::Schema::Table->new( name => 'mytable'); |
158 | |
159 | my $field1 = SQL::Translator::Schema::Field->new( name => 'myfield', |
160 | table => $table, |
161 | data_type => 'VARCHAR', |
162 | size => 10, |
163 | default_value => undef, |
164 | is_auto_increment => 0, |
165 | is_nullable => 1, |
166 | is_foreign_key => 0, |
167 | is_unique => 0 ); |
168 | |
169 | my $field1_sql = SQL::Translator::Producer::MySQL::create_field($field1); |
170 | |
171 | is($field1_sql, 'myfield VARCHAR(10)', 'Create field works'); |
172 | |
173 | my $field2 = SQL::Translator::Schema::Field->new( name => 'myfield', |
174 | table => $table, |
175 | data_type => 'VARCHAR', |
176 | size => 25, |
177 | default_value => undef, |
178 | is_auto_increment => 0, |
179 | is_nullable => 0, |
180 | is_foreign_key => 0, |
181 | is_unique => 0 ); |
182 | |
183 | my $alter_field = SQL::Translator::Producer::MySQL::alter_field($field1, |
184 | $field2); |
185 | is($alter_field, 'ALTER TABLE mytable CHANGE COLUMN myfield myfield VARCHAR(25) NOT NULL', 'Alter field works'); |
186 | |
187 | my $add_field = SQL::Translator::Producer::MySQL::add_field($field1); |
188 | |
189 | is($add_field, 'ALTER TABLE mytable ADD COLUMN myfield VARCHAR(10)', 'Add field works'); |
190 | |
191 | my $drop_field = SQL::Translator::Producer::MySQL::drop_field($field2); |
192 | is($drop_field, 'ALTER TABLE mytable DROP COLUMN myfield', 'Drop field works'); |