39
39
* @param newSchema new schema
41
41
public static void diffClusters(
42
final PrintWriter writer,
43
final PgDiffArguments arguments,
44
final PgSchema oldSchema,
45
final PgSchema newSchema) {
42
final PrintWriter writer,
43
final PgDiffArguments arguments,
44
final PgSchema oldSchema,
45
final PgSchema newSchema) {
46
46
for (PgTable newTable : newSchema.getTables()) {
47
47
final PgTable oldTable;
63
63
final String newCluster = newTable.getClusterIndexName();
66
((oldCluster == null) && (newCluster != null))
67
|| ((oldCluster != null) && (newCluster != null)
68
&& (newCluster.compareTo(oldCluster) != 0))) {
65
if (((oldCluster == null) && (newCluster != null)) ||
66
((oldCluster != null) && (newCluster != null) &&
67
(newCluster.compareTo(oldCluster) != 0))) {
70
69
writer.print("ALTER TABLE ");
72
71
PgDiffUtils.getQuotedName(
74
arguments.isQuoteNames()));
73
arguments.isQuoteNames()));
75
74
writer.print(" CLUSTER ON ");
77
76
PgDiffUtils.getQuotedName(
79
arguments.isQuoteNames()));
78
arguments.isQuoteNames()));
80
79
writer.println(';');
83
&& (newCluster == null)
84
&& newTable.containsIndex(oldCluster)) {
80
} else if ((oldCluster != null) && (newCluster == null) && newTable.
81
containsIndex(oldCluster)) {
86
83
writer.print("ALTER TABLE ");
88
85
PgDiffUtils.getQuotedName(
90
arguments.isQuoteNames()));
87
arguments.isQuoteNames()));
91
88
writer.println(" SET WITHOUT CLUSTER;");
102
99
* @param newSchema new schema
104
101
public static void diffTables(
105
final PrintWriter writer,
106
final PgDiffArguments arguments,
107
final PgSchema oldSchema,
108
final PgSchema newSchema) {
102
final PrintWriter writer,
103
final PgDiffArguments arguments,
104
final PgSchema oldSchema,
105
final PgSchema newSchema) {
109
106
dropTables(writer, arguments, oldSchema, newSchema);
110
107
createTables(writer, arguments, oldSchema, newSchema);
112
109
for (PgTable newTable : newSchema.getTables()) {
115
|| !oldSchema.containsTable(newTable.getName())) {
110
if ((oldSchema == null) || !oldSchema.containsTable(
111
newTable.getName())) {
133
129
* @param newTable new table
135
131
private static void addAlterStatistics(
136
final PrintWriter writer,
137
final PgDiffArguments arguments,
138
final PgTable oldTable,
139
final PgTable newTable) {
132
final PrintWriter writer,
133
final PgDiffArguments arguments,
134
final PgTable oldTable,
135
final PgTable newTable) {
140
136
final Map<String, Integer> stats = new HashMap<String, Integer>();
142
138
for (PgColumn newColumn : newTable.getColumns()) {
147
143
final Integer newStat = newColumn.getStatistics();
148
144
Integer newStatValue = null;
152
&& ((oldStat == null) || !newStat.equals(oldStat))) {
146
if ((newStat != null) && ((oldStat == null) || !newStat.equals(
153
148
newStatValue = newStat;
154
149
} else if ((oldStat != null) && (newStat == null)) {
155
150
newStatValue = Integer.valueOf(-1);
166
161
writer.print("ALTER TABLE ONLY ");
168
163
PgDiffUtils.getQuotedName(
170
arguments.isQuoteNames()));
165
arguments.isQuoteNames()));
171
166
writer.print(" ALTER COLUMN ");
173
168
PgDiffUtils.getQuotedName(
175
arguments.isQuoteNames()));
170
arguments.isQuoteNames()));
176
171
writer.print(" SET STATISTICS ");
177
172
writer.print(entry.getValue());
178
173
writer.println(';');
191
186
* value should be dropped
193
188
private static void addCreateTableColumns(
194
final List<String> commands,
195
final PgDiffArguments arguments,
196
final PgTable oldTable,
197
final PgTable newTable,
198
final List<PgColumn> dropDefaultsColumns) {
189
final List<String> commands,
190
final PgDiffArguments arguments,
191
final PgTable oldTable,
192
final PgTable newTable,
193
final List<PgColumn> dropDefaultsColumns) {
199
194
for (PgColumn column : newTable.getColumns()) {
200
195
if (!oldTable.containsColumn(column.getName())) {
203
+ column.getFullDefinition(
204
arguments.isQuoteNames(),
205
arguments.isAddDefaults()));
197
"\tADD COLUMN " + column.getFullDefinition(
198
arguments.isQuoteNames(),
199
arguments.isAddDefaults()));
207
201
if (arguments.isAddDefaults() && !column.getNullValue()) {
208
202
dropDefaultsColumns.add(column);
220
214
* @param newTable new table
222
216
private static void addDropTableColumns(
223
final List<String> commands,
224
final PgDiffArguments arguments,
225
final PgTable oldTable,
226
final PgTable newTable) {
217
final List<String> commands,
218
final PgDiffArguments arguments,
219
final PgTable oldTable,
220
final PgTable newTable) {
227
221
for (PgColumn column : oldTable.getColumns()) {
228
222
if (!newTable.containsColumn(column.getName())) {
231
+ PgDiffUtils.getQuotedName(
233
arguments.isQuoteNames()));
224
"\tDROP COLUMN " + PgDiffUtils.getQuotedName(
226
arguments.isQuoteNames()));
247
240
* value should be dropped
249
242
private static void addModifyTableColumns(
250
final List<String> commands,
251
final PgDiffArguments arguments,
252
final PgTable oldTable,
253
final PgTable newTable,
254
final List<PgColumn> dropDefaultsColumns) {
243
final List<String> commands,
244
final PgDiffArguments arguments,
245
final PgTable oldTable,
246
final PgTable newTable,
247
final List<PgColumn> dropDefaultsColumns) {
255
248
for (PgColumn newColumn : newTable.getColumns()) {
256
249
if (!oldTable.containsColumn(newColumn.getName())) {
260
253
final PgColumn oldColumn = oldTable.getColumn(newColumn.getName());
261
254
final String newColumnName =
262
PgDiffUtils.getQuotedName(
264
arguments.isQuoteNames());
255
PgDiffUtils.getQuotedName(
257
arguments.isQuoteNames());
266
259
if (!oldColumn.getType().equals(newColumn.getType())) {
268
"\tALTER COLUMN " + newColumnName + " TYPE "
269
+ newColumn.getType());
261
"\tALTER COLUMN " + newColumnName + " TYPE " +
262
newColumn.getType() + " /* TYPE change - table: " +
263
newTable.getName() + " original: " +
264
oldColumn.getType() + " new: " + newColumn.getType() +
272
268
final String oldDefault =
273
(oldColumn.getDefaultValue() == null) ? ""
269
(oldColumn.getDefaultValue() == null) ? ""
270
: oldColumn.getDefaultValue();
276
271
final String newDefault =
277
(newColumn.getDefaultValue() == null) ? ""
272
(newColumn.getDefaultValue() == null) ? ""
273
: newColumn.getDefaultValue();
281
275
if (!oldDefault.equals(newDefault)) {
282
276
if (newDefault.length() == 0) {
284
278
"\tALTER COLUMN " + newColumnName + " DROP DEFAULT");
287
"\tALTER COLUMN " + newColumnName + " SET DEFAULT "
281
"\tALTER COLUMN " + newColumnName + " SET DEFAULT " +
292
286
if (oldColumn.getNullValue() != newColumn.getNullValue()) {
293
287
if (newColumn.getNullValue()) {
295
"\tALTER COLUMN " + newColumnName
289
"\tALTER COLUMN " + newColumnName + " DROP NOT NULL");
298
291
if (arguments.isAddDefaults()) {
299
292
final String defaultValue =
300
PgColumnUtils.getDefaultValue(newColumn.getType());
293
PgColumnUtils.getDefaultValue(
294
newColumn.getType());
302
296
if (defaultValue != null) {
304
"\tALTER COLUMN " + newColumnName
305
+ " SET DEFAULT " + defaultValue);
298
"\tALTER COLUMN " + newColumnName +
299
" SET DEFAULT " + defaultValue);
306
300
dropDefaultsColumns.add(newColumn);
324
318
* @param newTable new table
326
320
private static void checkInherits(
327
final PrintWriter writer,
328
final PgDiffArguments arguments,
329
final PgTable oldTable,
330
final PgTable newTable) {
321
final PrintWriter writer,
322
final PgDiffArguments arguments,
323
final PgTable oldTable,
324
final PgTable newTable) {
331
325
final String oldInherits = oldTable.getInherits();
332
326
final String newInherits = newTable.getInherits();
334
328
if ((oldInherits == null) && (newInherits != null)) {
335
329
writer.println();
337
"Modified INHERITS on TABLE "
338
+ PgDiffUtils.getQuotedName(
340
arguments.isQuoteNames())
341
+ ": original table doesn't use INHERITS but new table "
342
+ "uses INHERITS " + newTable.getInherits());
331
"Modified INHERITS on TABLE " + PgDiffUtils.getQuotedName(
333
arguments.isQuoteNames()) +
334
": original table doesn't use INHERITS but new table " +
335
"uses INHERITS " + newTable.getInherits());
343
336
} else if ((oldInherits != null) && (newInherits == null)) {
344
337
writer.println();
346
"Modified INHERITS on TABLE "
347
+ PgDiffUtils.getQuotedName(
349
arguments.isQuoteNames())
350
+ ": original table uses INHERITS "
351
+ oldTable.getInherits()
352
+ " but new table doesn't use INHERITS");
354
(oldInherits != null)
355
&& (newInherits != null)
356
&& !oldInherits.equals(newInherits)) {
339
"Modified INHERITS on TABLE " + PgDiffUtils.getQuotedName(
341
arguments.isQuoteNames()) +
342
": original table uses INHERITS " + oldTable.getInherits() +
343
" but new table doesn't use INHERITS");
344
} else if ((oldInherits != null) && (newInherits != null) &&
345
!oldInherits.equals(newInherits)) {
357
346
writer.println();
359
"Modified INHERITS on TABLE "
360
+ PgDiffUtils.getQuotedName(
362
arguments.isQuoteNames())
363
+ ": original table uses INHERITS "
364
+ oldTable.getInherits() + " but new table uses INHERITS "
365
+ newTable.getInherits());
348
"Modified INHERITS on TABLE " + PgDiffUtils.getQuotedName(
350
arguments.isQuoteNames()) +
351
": original table uses INHERITS " + oldTable.getInherits() +
352
" but new table uses INHERITS " + newTable.getInherits());
377
364
* @param newTable new table
379
366
private static void checkWithOIDS(
380
final PrintWriter writer,
381
final PgDiffArguments arguments,
382
final PgTable oldTable,
383
final PgTable newTable) {
367
final PrintWriter writer,
368
final PgDiffArguments arguments,
369
final PgTable oldTable,
370
final PgTable newTable) {
384
371
if (oldTable.isWithOIDS() && !newTable.isWithOIDS()) {
385
372
writer.println();
388
+ PgDiffUtils.getQuotedName(
390
arguments.isQuoteNames()));
374
"ALTER TABLE " + PgDiffUtils.getQuotedName(
376
arguments.isQuoteNames()));
391
377
writer.println("\tSET WITHOUT OIDS;");
392
378
} else if (!oldTable.isWithOIDS() && newTable.isWithOIDS()) {
393
379
writer.println();
396
+ PgDiffUtils.getQuotedName(
398
arguments.isQuoteNames())
399
+ " adds WITH OIDS but there is no equivalent command "
400
+ "for adding of OIDS in PostgreSQL");
381
"WARNING: Table " + PgDiffUtils.getQuotedName(
383
arguments.isQuoteNames()) +
384
" adds WITH OIDS but there is no equivalent command " +
385
"for adding of OIDS in PostgreSQL");
410
395
* @param newSchema new schema
412
397
private static void createTables(
413
final PrintWriter writer,
414
final PgDiffArguments arguments,
415
final PgSchema oldSchema,
416
final PgSchema newSchema) {
398
final PrintWriter writer,
399
final PgDiffArguments arguments,
400
final PgSchema oldSchema,
401
final PgSchema newSchema) {
417
402
for (PgTable table : newSchema.getTables()) {
420
|| !oldSchema.containsTable(table.getName())) {
403
if ((oldSchema == null) || !oldSchema.containsTable(table.getName())) {
421
404
writer.println();
422
405
writer.println(table.getCreationSQL(arguments.isQuoteNames()));
433
416
* @param newSchema new schema
435
418
private static void dropTables(
436
final PrintWriter writer,
437
final PgDiffArguments arguments,
438
final PgSchema oldSchema,
439
final PgSchema newSchema) {
419
final PrintWriter writer,
420
final PgDiffArguments arguments,
421
final PgSchema oldSchema,
422
final PgSchema newSchema) {
440
423
if (oldSchema != null) {
441
424
for (PgTable table : oldSchema.getTables()) {
442
425
if (!newSchema.containsTable(table.getName())) {
457
440
* @param newTable new table
459
442
private static void updateTableColumns(
460
final PrintWriter writer,
461
final PgDiffArguments arguments,
462
final PgTable oldTable,
463
final PgTable newTable) {
443
final PrintWriter writer,
444
final PgDiffArguments arguments,
445
final PgTable oldTable,
446
final PgTable newTable) {
464
447
final List<String> commands = new ArrayList<String>();
465
448
final List<PgColumn> dropDefaultsColumns = new ArrayList<PgColumn>();
466
449
addDropTableColumns(commands, arguments, oldTable, newTable);
480
463
if (commands.size() > 0) {
481
464
final String quotedTableName =
482
PgDiffUtils.getQuotedName(
484
arguments.isQuoteNames());
465
PgDiffUtils.getQuotedName(
467
arguments.isQuoteNames());
485
468
writer.println();
486
469
writer.println("ALTER TABLE " + quotedTableName);
498
481
writer.print("\tALTER COLUMN ");
500
483
PgDiffUtils.getQuotedName(
501
dropDefaultsColumns.get(i).getName(),
502
arguments.isQuoteNames()));
484
dropDefaultsColumns.get(i).getName(),
485
arguments.isQuoteNames()));
503
486
writer.print(" DROP DEFAULT");
505
488
((i + 1) < dropDefaultsColumns.size()) ? "," : ";");