forked from cloudspannerecosystem/spanner-schema-diff-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDdlDiffTest.java
More file actions
688 lines (625 loc) · 26.8 KB
/
Copy pathDdlDiffTest.java
File metadata and controls
688 lines (625 loc) · 26.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
/*
* Copyright 2019 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.cloud.solutions.spannerddl.diff;
import static com.google.cloud.solutions.spannerddl.diff.DdlDiff.ALLOW_DROP_STATEMENTS_OPT;
import static com.google.cloud.solutions.spannerddl.diff.DdlDiff.ALLOW_RECREATE_CONSTRAINTS_OPT;
import static com.google.cloud.solutions.spannerddl.diff.DdlDiff.ALLOW_RECREATE_INDEXES_OPT;
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;
import com.google.cloud.solutions.spannerddl.parser.ASTddl_statement;
import com.google.common.collect.ImmutableMap;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import org.junit.Test;
public class DdlDiffTest {
private static final Map<String, Boolean> DEFAULT_OPTIONS =
ImmutableMap.of(
ALLOW_RECREATE_CONSTRAINTS_OPT,
true,
ALLOW_DROP_STATEMENTS_OPT,
true,
ALLOW_RECREATE_INDEXES_OPT,
true,
DdlDiff.IGNORE_PROTO_BUNDLES_OPT,
false);
@Test
public void parseMultiDdlStatements() throws DdlDiffException {
String DDL =
"create table test1 (col1 int64) primary key (col1);\n\n"
+ "-- comment ; with semicolon.\n"
+ "create table test2 (col2 float64) primary key (col2);\n\n"
+ "create table test3 (col3 STRING(max) -- inline comment\n"
+ ") primary key (col3), interleave in parent testparent \n"
+ "on delete no action;\n\n"
+ "create index index1 on table1 (col1);\n"
+ "-- more comment\n"
+ "; -- stray semicolon";
List<ASTddl_statement> result = DdlDiff.parseDdl(DDL);
assertThat(result).hasSize(4);
assertThat(result.get(0).toString())
.isEqualTo("CREATE TABLE test1 ( col1 INT64 ) PRIMARY KEY (col1)");
assertThat(result.get(1).toString())
.isEqualTo("CREATE TABLE test2 ( col2 FLOAT64 ) PRIMARY KEY (col2)");
assertThat(result.get(2).toString())
.isEqualTo(
"CREATE TABLE test3 ( col3 STRING(MAX) ) PRIMARY KEY (col3), "
+ "INTERLEAVE IN PARENT testparent ON DELETE NO ACTION");
assertThat(result.get(3).toString()).isEqualTo("CREATE INDEX index1 ON table1 ( col1 )");
}
@Test
public void parseCreateTable_anonForeignKey() {
try {
DdlDiff.parseDdl(
"create table test ("
+ "intcol int64 not null, "
+ "FOREIGN KEY(col1, col2) REFERENCES other_table(other_col1, other_col2)"
+ ")"
+ "primary key (intcol ASC)");
fail("Expected exception not thrown");
} catch (DdlDiffException | IllegalArgumentException e) {
assertThat(e.getMessage())
.containsMatch("Can not create diffs when anonymous constraints are used.");
}
}
@Test
public void parseCreateTable_anonCheckConstraint() {
try {
DdlDiff.parseDdl(
"create table test ("
+ "intcol int64 not null, "
+ "check (intcol>1)"
+ ")"
+ "primary key (intcol ASC)");
fail("Expected exception not thrown");
} catch (DdlDiffException | IllegalArgumentException e) {
assertThat(e.getMessage())
.containsMatch("Can not create diffs when anonymous constraints are used.");
}
}
@Test(expected = IllegalArgumentException.class)
public void parseDDLNoAlterTableAlterColumn() throws DdlDiffException {
DdlDiff.parseDdl("alter table test1 alter column col1 int64 not null");
}
@Test(expected = IllegalArgumentException.class)
public void parseDDLNoAlterTableAddColumn() throws DdlDiffException {
DdlDiff.parseDdl("alter table test1 add column col1 int64 not null");
}
@Test(expected = IllegalArgumentException.class)
public void parseDDLNoAlterTableDropColumn() throws DdlDiffException {
DdlDiff.parseDdl("alter table test1 drop column col1");
}
@Test(expected = IllegalArgumentException.class)
public void parseDDLNoAlterTableDropConstraint() throws DdlDiffException {
DdlDiff.parseDdl("alter table test1 drop constraint xxx");
}
@Test(expected = IllegalArgumentException.class)
public void parseDDLNoAlterTableSetOnDelete() throws DdlDiffException {
DdlDiff.parseDdl("alter table test1 set on delete cascade");
}
@Test
public void parseDDLAlterTableAddConstraint() throws DdlDiffException {
DdlDiff.parseDdl("alter table test1 add constraint XXX FOREIGN KEY (yyy) references zzz(xxx)");
}
@Test(expected = IllegalArgumentException.class)
public void parseDDLNoAlterTableAddAnonConstraint() throws DdlDiffException {
DdlDiff.parseDdl("alter table test1 add FOREIGN KEY (yyy) references zzz(xxx)");
}
@Test
public void generateAlterTable_AddColumn() throws DdlDiffException {
// Add single row.
assertThat(
getDiff(
"create table test1 (col1 int64) primary key (col1);",
"create table test1 (col1 int64, col2 int64 not null) primary key (col1);",
true))
.containsExactly("ALTER TABLE test1 ADD COLUMN col2 INT64 NOT NULL");
// Add multiple rows.
assertThat(
getDiff(
"create table test1 (col1 int64) primary key (col1);",
"create table test1 (col1 int64, col2 String(MAX), col3 Array<Bytes(100)> not null)"
+ " primary key (col1);",
true))
.containsExactly(
"ALTER TABLE test1 ADD COLUMN col2 STRING(MAX)",
"ALTER TABLE test1 ADD COLUMN col3 ARRAY<BYTES(100)> NOT NULL");
}
@Test
public void generateAlterTable_DropColumn() throws DdlDiffException {
// Add single row.
assertThat(
getDiff(
"create table test1 (col1 int64, col2 int64 not null) primary key (col1);",
"create table test1 (col1 int64) primary key (col1);",
true))
.containsExactly("ALTER TABLE test1 DROP COLUMN col2");
assertThat(
getDiff(
"create table test1 (col1 int64, col2 int64 not null) primary key (col1);",
"create table test1 (col1 int64) primary key (col1);",
false))
.isEmpty();
// Add multiple rows.
assertThat(
getDiff(
"create table test1 (col1 int64, col2 String(MAX), col3 Array<Bytes(100)> not null)"
+ " primary key (col1);",
"create table test1 (col1 int64) primary key (col1);",
true))
.containsExactly(
"ALTER TABLE test1 DROP COLUMN col2", "ALTER TABLE test1 DROP COLUMN col3");
assertThat(
getDiff(
"create table test1 (col1 int64, col2 String(MAX), col3 Array<Bytes(100)> not null)"
+ " primary key (col1);",
"create table test1 (col1 int64) primary key (col1);",
false))
.isEmpty();
}
@Test
public void generateAlterTable_AlterColumnAttrs() throws DdlDiffException {
// Not null added and removed
assertThat(
getDiff(
"create table test1 (col1 int64, col2 int64 not null) primary key (col1);",
"create table test1 (col1 int64 not null, col2 int64) primary key (col1);",
true))
.containsExactly(
"ALTER TABLE test1 ALTER COLUMN col1 INT64 NOT NULL",
"ALTER TABLE test1 ALTER COLUMN col2 INT64");
// Options added and removed
assertThat(
getDiff(
"create table test1 (col1 timestamp options(allow_commit_timestamp=true), col2"
+ " timestamp) primary key (col1);",
"create table test1 (col1 timestamp, col2 timestamp"
+ " options(allow_commit_timestamp=true)) primary key (col1);",
true))
.containsExactly(
"ALTER TABLE test1 ALTER COLUMN col1 SET OPTIONS (allow_commit_timestamp=NULL)",
"ALTER TABLE test1 ALTER COLUMN col2 SET OPTIONS (allow_commit_timestamp=TRUE)");
}
@Test
public void generateAlterTable_AlterColumnSize() throws DdlDiffException {
// changes string() size and array<bytes()> size
assertThat(
getDiff(
"create table test1 (col1 String(100), col2 ARRAY<BYTES(100)>) primary key (col1);",
"create table test1 (col1 String(200), col2 ARRAY<BYTES(MAX)>) primary key (col1);",
true))
.containsExactly(
"ALTER TABLE test1 ALTER COLUMN col1 STRING(200)",
"ALTER TABLE test1 ALTER COLUMN col2 ARRAY<BYTES(MAX)>");
}
@Test
public void generateAlterTable_incompatibleTypeChange() {
// Change array depth
getDiffCheckDdlDiffException(
"create table test1 (col1 ARRAY<BYTES(100)>) primary key (col1);",
"create table test1 (col1 ARRAY<ARRAY<BYTES(100)>>) primary key (col1);",
true,
"Cannot change type of table test1 column col1");
// change Array sized type
getDiffCheckDdlDiffException(
"create table test1 (col1 ARRAY<BYTES(100)>) primary key (col1);",
"create table test1 (col1 ARRAY<STRING(100)>) primary key (col1);",
true,
"Cannot change type of table test1 column col1");
// change Array type
getDiffCheckDdlDiffException(
"create table test1 (col1 ARRAY<int64>) primary key (col1);",
"create table test1 (col1 ARRAY<float64>) primary key (col1);",
true,
"Cannot change type of table test1 column col1");
// change sized type
getDiffCheckDdlDiffException(
"create table test1 (col1 BYTES(100)) primary key (col1);",
"create table test1 (col1 STRING(100)) primary key (col1);",
true,
"Cannot change type of table test1 column col1");
// change type
getDiffCheckDdlDiffException(
"create table test1 (col1 int64) primary key (col1);",
"create table test1 (col1 float64) primary key (col1);",
true,
"Cannot change type of table test1 column col1");
// change type to UUID
getDiffCheckDdlDiffException(
"create table test1 (col1 int64, col2 int64) primary key (col1);",
"create table test1 (col1 int64, col2 uuid) primary key (col1);",
true,
"Cannot change type of table test1 column col2");
// change STRING(36) to UUID
getDiffCheckDdlDiffException(
"create table test1 (col1 int64, col2 string(36)) primary key (col1);",
"create table test1 (col1 int64, col2 uuid) primary key (col1);",
true,
"Cannot change type of table test1 column col2");
}
@Test
public void generateAlterTable_changeKey() {
// change key col
getDiffCheckDdlDiffException(
"create table test1 (col1 int64, col2 int64) primary key (col1);",
"create table test1 (col1 int64, col2 int64) primary key (col2);",
true,
"Cannot change primary key of table test1");
// add key col
getDiffCheckDdlDiffException(
"create table test1 (col1 int64, col2 int64) primary key (col1);",
"create table test1 (col1 int64, col2 int64) primary key (col1, col2);",
true,
"Cannot change primary key of table test1");
// remove key col
getDiffCheckDdlDiffException(
"create table test1 (col1 int64, col2 int64) primary key (col1, col2);",
"create table test1 (col1 int64, col2 int64) primary key (col1);",
true,
"Cannot change primary key of table test1");
// change order
getDiffCheckDdlDiffException(
"create table test1 (col1 int64, col2 int64) primary key (col1);",
"create table test1 (col1 int64, col2 int64) primary key (col1 DESC);",
true,
"Cannot change primary key of table test1");
}
@Test
public void generateAlterTable_changeInterleaving() throws DdlDiffException {
// remove interleave
getDiffCheckDdlDiffException(
"create table test1 (col1 int64, col2 int64) "
+ "primary key (col1), interleave in parent testparent;",
"create table test1 (col1 int64, col2 int64) primary key (col1)",
true,
"Cannot change interleaving on table test1");
// remove different parent
getDiffCheckDdlDiffException(
"create table test1 (col1 int64, col2 int64) "
+ "primary key (col1), interleave in parent testparent;",
"create table test1 (col1 int64, col2 int64) "
+ "primary key (col1), interleave in parent otherparent",
true,
"Cannot change interleaved parent of table test1");
// add parent constraint
getDiffCheckDdlDiffException(
"create table test1 (col1 int64, col2 int64) "
+ "primary key (col1), interleave in testparent;",
"create table test1 (col1 int64, col2 int64) "
+ "primary key (col1), interleave in parent testparent",
true,
"Cannot change interleaved parent of table test1");
// change on delete
assertThat(
getDiff(
"create table test1 (col1 int64, col2 int64) "
+ "primary key (col1), interleave in parent testparent on delete cascade;",
"create table test1 (col1 int64, col2 int64) "
+ "primary key (col1), interleave in parent testparent",
true))
.containsExactly("ALTER TABLE test1 SET ON DELETE NO ACTION");
// change on delete
assertThat(
getDiff(
"create table test1 (col1 int64, col2 int64) "
+ "primary key (col1), interleave in parent testparent on delete NO ACTION;",
"create table test1 (col1 int64, col2 int64) "
+ "primary key (col1), interleave in parent testparent on delete cascade",
true))
.containsExactly("ALTER TABLE test1 SET ON DELETE CASCADE");
}
@Test
public void generateAlterTable_changeGenerationClause() {
// remove interleave
getDiffCheckDdlDiffException(
"create table test1 (col1 int64, col2 int64, col3 int64 as ( col1*col2 ) stored) primary"
+ " key (col1)",
"create table test1 (col1 int64, col2 int64, col3 int64 as ( col1/col2 ) stored) primary"
+ " key (col1)",
true,
"Cannot change generation clause of table test1 column col3 from AS ");
// add generation
getDiffCheckDdlDiffException(
"create table test1 (col1 int64, col2 int64, col3 int64) primary key (col1)",
"create table test1 (col1 int64, col2 int64, col3 int64 as ( col1*col2 ) stored) primary"
+ " key (col1)",
true,
"Cannot change generation clause of table test1 column col3 from null ");
// remove generation
getDiffCheckDdlDiffException(
"create table test1 (col1 int64, col2 int64, col3 int64 as ( col1*col2 ) stored) primary"
+ " key (col1)",
"create table test1 (col1 int64, col2 int64, col3 int64) primary key (col1)",
true,
"Cannot change generation clause of table test1 column col3 from AS");
}
@Test
public void generateAlterTable_alterStatementOrdering() throws DdlDiffException {
assertThat(
getDiff(
"create table test1 ("
+ "col1 int64, "
+ "col2 int64, "
+ "col3 int64 NOT NULL, "
+ "col4 int64, "
+ "col5 timestamp OPTIONS (allow_commit_timestamp=true), "
+ "col6 timestamp, "
+ "col7 STRING(100) ) "
+ "primary key (col1), interleave in parent testparent",
"create table test1 ("
+ "col1 int64, "
// remove col2
+ "col3 int64, " // remove not null
+ "col4 int64 NOT NULL, " // add not null
+ "col5 timestamp, " // remove options
+ "col6 timestamp OPTIONS (allow_commit_timestamp=true)," // add options
+ "col7 STRING(200), " // change size
+ "col8 float64 not null " // add column
+ " ) primary key (col1), "
+ "interleave in parent testparent on delete cascade", // on delete rule.
true)) // allow drop
.containsExactly(
// change table options.
"ALTER TABLE test1 SET ON DELETE CASCADE",
// then drop cols
"ALTER TABLE test1 DROP COLUMN col2",
// then add cols
"ALTER TABLE test1 ADD COLUMN col8 FLOAT64 NOT NULL",
// then alter cols.
"ALTER TABLE test1 ALTER COLUMN col3 INT64",
"ALTER TABLE test1 ALTER COLUMN col4 INT64 NOT NULL",
"ALTER TABLE test1 ALTER COLUMN col5 SET OPTIONS (allow_commit_timestamp=NULL)",
"ALTER TABLE test1 ALTER COLUMN col6 SET OPTIONS (allow_commit_timestamp=TRUE)",
"ALTER TABLE test1 ALTER COLUMN col7 STRING(200)");
}
@Test
public void parseAlterDatabaseDifferentDbNames() {
getDiffCheckDdlDiffException(
"ALTER DATABASE dbname SET OPTIONS(hello='world');",
"ALTER DATABASE otherdbname SET OPTIONS(hello='world');",
true,
"Database IDs differ");
getDiffCheckDdlDiffException(
"ALTER DATABASE dbname SET OPTIONS(hello='world');"
+ "ALTER DATABASE otherdbname SET OPTIONS(goodbye='world');",
"",
true,
"Multiple database IDs defined");
getDiffCheckDdlDiffException(
"",
"ALTER DATABASE dbname SET OPTIONS(hello='world');"
+ "ALTER DATABASE otherdbname SET OPTIONS(hello='world');",
true,
"Multiple database IDs defined");
}
@Test
public void diffCreateTableDifferentExists() throws DdlDiffException {
assertThat(
getDiff(
"create table test1 ( col1 int64 ) primary key (col1);",
"create table if not exists test1 ( col1 int64 ) primary key (col1);",
true))
.isEmpty();
}
@Test
public void diffCreateIndexDifferentExists() throws DdlDiffException {
assertThat(
getDiff(
"CREATE INDEX myindex ON mytable ( col1 );",
"CREATE INDEX IF NOT EXISTS myindex ON mytable ( col1 );",
true))
.isEmpty();
}
@Test
public void diffCreateIndexOnlyStoring() throws DdlDiffException {
assertThat(
DdlDiff.build(
"CREATE INDEX myindex ON mytable ( col1 ) STORING (col2, col3);",
"CREATE INDEX myindex ON mytable ( col1 ) STORING (col3, col4);",
DEFAULT_OPTIONS)
.generateDifferenceStatements(
ImmutableMap.of(
ALLOW_RECREATE_INDEXES_OPT,
false,
ALLOW_DROP_STATEMENTS_OPT,
false,
ALLOW_RECREATE_CONSTRAINTS_OPT,
false,
DdlDiff.IGNORE_PROTO_BUNDLES_OPT,
false)))
.containsExactly(
"ALTER INDEX myindex DROP STORED COLUMN col2",
"ALTER INDEX myindex ADD STORED COLUMN col4");
}
@Test
public void diffCreateIndexNotOnlyStoringRecreates() throws DdlDiffException {
assertThat(
DdlDiff.build(
"CREATE UNIQUE INDEX myindex ON mytable ( col1 ) STORING (col2, col3);",
"CREATE INDEX myindex ON mytable ( col1 ) STORING (col3, col4);",
DEFAULT_OPTIONS)
.generateDifferenceStatements(
ImmutableMap.of(
ALLOW_RECREATE_INDEXES_OPT,
true,
ALLOW_DROP_STATEMENTS_OPT,
false,
ALLOW_RECREATE_CONSTRAINTS_OPT,
false,
DdlDiff.IGNORE_PROTO_BUNDLES_OPT,
false)))
.containsExactly(
"DROP INDEX myindex",
"CREATE INDEX myindex ON mytable ( col1 ) STORING ( col3, col4 )");
}
@Test
public void diffCreateIndexNotOnlyStoringThrows() {
getDiffCheckDdlDiffException(
"CREATE UNIQUE INDEX myindex ON mytable ( col1 ) STORING (col2, col3);",
"CREATE INDEX myindex ON mytable ( col1 ) STORING (col3, col4);",
false,
"At least one Index differs, and allowRecreateIndexes is not set");
}
private static void getDiffCheckDdlDiffException(
String originalDdl, String newDdl, boolean allowDropStatements, String exceptionContains) {
try {
getDiff(originalDdl, newDdl, allowDropStatements);
fail("Expected DdlDiffException not thrown.");
} catch (DdlDiffException e) {
assertThat(e.getMessage()).contains(exceptionContains);
}
}
private static List<String> getDiff(
String originalDdl, String newDdl, boolean allowDropStatements) throws DdlDiffException {
return getDiff(originalDdl, newDdl, allowDropStatements, false);
}
private static List<String> getDiff(
String originalDdl, String newDdl, boolean allowDropStatements, boolean ignoreProtoBundles)
throws DdlDiffException {
Map<String, Boolean> options =
ImmutableMap.of(
ALLOW_RECREATE_CONSTRAINTS_OPT,
true,
ALLOW_DROP_STATEMENTS_OPT,
allowDropStatements,
ALLOW_RECREATE_INDEXES_OPT,
false,
DdlDiff.IGNORE_PROTO_BUNDLES_OPT,
ignoreProtoBundles);
return DdlDiff.build(originalDdl, newDdl, options).generateDifferenceStatements(options);
}
@Test
public void generateDifferences_dropTables() throws DdlDiffException {
DdlDiff diff =
DdlDiff.build(
"Create table table1 (col1 int64) primary key (col1);"
+ "Create table table2 (col2 int64) primary key (col2);",
"Create table table1 (col1 int64) primary key (col1);",
DEFAULT_OPTIONS);
assertThat(diff.generateDifferenceStatements(ImmutableMap.of(ALLOW_DROP_STATEMENTS_OPT, true)))
.containsExactly("DROP TABLE table2");
assertThat(diff.generateDifferenceStatements(ImmutableMap.of(ALLOW_DROP_STATEMENTS_OPT, false)))
.isEmpty();
}
@Test
public void generateDifferences_dropIndexes() throws DdlDiffException {
DdlDiff diff =
DdlDiff.build(
"Create index index1 on table1 (col1 desc);"
+ "Create index index2 on table2 (col2 desc);",
"Create index index1 on table1 (col1 desc);",
DEFAULT_OPTIONS);
assertThat(diff.generateDifferenceStatements(ImmutableMap.of(ALLOW_DROP_STATEMENTS_OPT, true)))
.containsExactly("DROP INDEX index2");
assertThat(diff.generateDifferenceStatements(ImmutableMap.of(ALLOW_DROP_STATEMENTS_OPT, false)))
.isEmpty();
}
@Test
public void differentIndexesWithNoRecreate() {
Map<String, Boolean> options =
ImmutableMap.of(
ALLOW_DROP_STATEMENTS_OPT,
false,
ALLOW_RECREATE_INDEXES_OPT,
false,
DdlDiff.IGNORE_PROTO_BUNDLES_OPT,
false);
try {
DdlDiff.build(
"Create unique null_filtered index index1 on table1 (col1 desc)",
"Create unique null_filtered index index1 on table1 (col1 asc)",
options)
.generateDifferenceStatements(options);
fail("Expected exception not thrown");
} catch (DdlDiffException e) {
assertThat(e.getMessage()).containsMatch("one Index differs");
}
try {
DdlDiff.build(
"Create unique null_filtered index index1 on table1 (col1 desc)",
"Create unique null_filtered index index1 on table1 (col2 desc)",
options)
.generateDifferenceStatements(options);
fail("Expected exception not thrown");
} catch (DdlDiffException e) {
assertThat(e.getMessage()).containsMatch("one Index differs");
}
try {
DdlDiff.build(
"Create unique null_filtered index index1 on table1 (col1 desc)",
"Create index index1 on table1 (col1 desc)",
options)
.generateDifferenceStatements(options);
fail("Expected exception not thrown");
} catch (DdlDiffException e) {
assertThat(e.getMessage()).containsMatch("one Index differs");
}
}
@Test
public void differentIndexesWithRecreate() throws DdlDiffException {
Map<String, Boolean> options =
ImmutableMap.of(
ALLOW_DROP_STATEMENTS_OPT,
true,
ALLOW_RECREATE_INDEXES_OPT,
true,
DdlDiff.IGNORE_PROTO_BUNDLES_OPT,
false);
assertThat(
DdlDiff.build(
"Create unique null_filtered index index1 on table1 (col1 desc)",
"Create unique null_filtered index index1 on table1 (col1 asc)",
options)
.generateDifferenceStatements(options))
.isEqualTo(
Arrays.asList(
"DROP INDEX index1",
"CREATE UNIQUE NULL_FILTERED INDEX index1 ON table1 ( col1 ASC )"));
assertThat(
DdlDiff.build(
"Create unique null_filtered index index1 on table1 ( col1 desc )",
"Create unique null_filtered index index1 on table1 ( col2 desc )",
options)
.generateDifferenceStatements(options))
.isEqualTo(
Arrays.asList(
"DROP INDEX index1",
"CREATE UNIQUE NULL_FILTERED INDEX index1 ON table1 ( col2 DESC )"));
assertThat(
DdlDiff.build(
"Create unique null_filtered index index1 on table1 ( col1 desc )",
"Create index index1 on table1 ( col1 desc )",
options)
.generateDifferenceStatements(options))
.isEqualTo(
Arrays.asList("DROP INDEX index1", "CREATE INDEX index1 ON table1 ( col1 DESC )"));
}
@Test
public void ignoreProtoBundles_throwsExceptionWhenNotIgnoring() {
try {
getDiff("CREATE PROTO BUNDLE (a,b,c)", "", false, false);
fail("Expected exception not thrown");
} catch (UnsupportedOperationException e) {
assertThat(e.getMessage()).isEqualTo("Not Implemented");
} catch (DdlDiffException e) {
fail("Unexpected DdlDiffException: " + e);
}
}
@Test
public void ignoreProtoBundles_ignoresWhenFlagIsSet() throws DdlDiffException {
assertThat(getDiff("", "CREATE PROTO BUNDLE (a,b,c)", false, true)).isEmpty();
}
}