~ubuntu-branches/ubuntu/precise/apgdiff/precise

« back to all changes in this revision

Viewing changes to src/main/java/cz/startnet/utils/pgdiff/PgDiffTables.java

  • Committer: Bazaar Package Importer
  • Author(s): Andres Rodriguez
  • Date: 2009-06-22 01:09:18 UTC
  • mfrom: (2.1.1 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090622010918-y73rbzpqpe4gdc0z
Tags: 1.3-1ubuntu1
* Merge from debian unstable (LP: #390146), remaining changes:
  - Add default-jdk to Build-Depends.
  - Change the runtime dependency from gij to default-jre-headless.
* debian/control: Added Build-Depends-Indep on
  - asciidoc, contains a2x, required for the build process.
  - libxml2-utils, eliminates a2x message 'xmllint not found' during build
    process.
* debian/apgdiff.1.txt: Changed deprecated glossary list syntax.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
 
 * $Id: PgDiffTables.java 80 2007-09-01 20:25:45Z fordfrog $
 
2
 * $Id: PgDiffTables.java 94 2008-09-30 11:07:28Z fordfrog $
3
3
 */
4
4
package cz.startnet.utils.pgdiff;
5
5
 
15
15
import java.util.List;
16
16
import java.util.Map;
17
17
 
18
 
 
19
18
/**
20
19
 * Diffs tables.
21
20
 *
22
21
 * @author fordfrog
23
 
 * @version $Id: PgDiffTables.java 80 2007-09-01 20:25:45Z fordfrog $
 
22
 * @version $Id: PgDiffTables.java 94 2008-09-30 11:07:28Z fordfrog $
24
23
 */
25
24
public class PgDiffTables {
 
25
 
26
26
    /**
27
27
     * Creates a new instance of PgDiffTables.
28
28
     */
39
39
     * @param newSchema new schema
40
40
     */
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;
48
48
 
62
62
 
63
63
            final String newCluster = newTable.getClusterIndexName();
64
64
 
65
 
            if (
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))) {
69
68
                writer.println();
70
69
                writer.print("ALTER TABLE ");
71
70
                writer.print(
72
71
                        PgDiffUtils.getQuotedName(
73
 
                                newTable.getName(),
74
 
                                arguments.isQuoteNames()));
 
72
                        newTable.getName(),
 
73
                        arguments.isQuoteNames()));
75
74
                writer.print(" CLUSTER ON ");
76
75
                writer.print(
77
76
                        PgDiffUtils.getQuotedName(
78
 
                                newCluster,
79
 
                                arguments.isQuoteNames()));
 
77
                        newCluster,
 
78
                        arguments.isQuoteNames()));
80
79
                writer.println(';');
81
 
            } else if (
82
 
                (oldCluster != null)
83
 
                    && (newCluster == null)
84
 
                    && newTable.containsIndex(oldCluster)) {
 
80
            } else if ((oldCluster != null) && (newCluster == null) && newTable.
 
81
                    containsIndex(oldCluster)) {
85
82
                writer.println();
86
83
                writer.print("ALTER TABLE ");
87
84
                writer.print(
88
85
                        PgDiffUtils.getQuotedName(
89
 
                                newTable.getName(),
90
 
                                arguments.isQuoteNames()));
 
86
                        newTable.getName(),
 
87
                        arguments.isQuoteNames()));
91
88
                writer.println(" SET WITHOUT CLUSTER;");
92
89
            }
93
90
        }
102
99
     * @param newSchema new schema
103
100
     */
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);
111
108
 
112
109
        for (PgTable newTable : newSchema.getTables()) {
113
 
            if (
114
 
                (oldSchema == null)
115
 
                    || !oldSchema.containsTable(newTable.getName())) {
 
110
            if ((oldSchema == null) || !oldSchema.containsTable(
 
111
                    newTable.getName())) {
116
112
                continue;
117
113
            }
118
114
 
133
129
     * @param newTable new table
134
130
     */
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>();
141
137
 
142
138
        for (PgColumn newColumn : newTable.getColumns()) {
147
143
                final Integer newStat = newColumn.getStatistics();
148
144
                Integer newStatValue = null;
149
145
 
150
 
                if (
151
 
                    (newStat != null)
152
 
                        && ((oldStat == null) || !newStat.equals(oldStat))) {
 
146
                if ((newStat != null) && ((oldStat == null) || !newStat.equals(
 
147
                        oldStat))) {
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 ");
167
162
            writer.print(
168
163
                    PgDiffUtils.getQuotedName(
169
 
                            newTable.getName(),
170
 
                            arguments.isQuoteNames()));
 
164
                    newTable.getName(),
 
165
                    arguments.isQuoteNames()));
171
166
            writer.print(" ALTER COLUMN ");
172
167
            writer.print(
173
168
                    PgDiffUtils.getQuotedName(
174
 
                            entry.getKey(),
175
 
                            arguments.isQuoteNames()));
 
169
                    entry.getKey(),
 
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
192
187
     */
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())) {
201
196
                commands.add(
202
 
                        "\tADD COLUMN "
203
 
                        + column.getFullDefinition(
204
 
                                arguments.isQuoteNames(),
205
 
                                arguments.isAddDefaults()));
 
197
                        "\tADD COLUMN " + column.getFullDefinition(
 
198
                        arguments.isQuoteNames(),
 
199
                        arguments.isAddDefaults()));
206
200
 
207
201
                if (arguments.isAddDefaults() && !column.getNullValue()) {
208
202
                    dropDefaultsColumns.add(column);
220
214
     * @param newTable new table
221
215
     */
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())) {
229
223
                commands.add(
230
 
                        "\tDROP COLUMN "
231
 
                        + PgDiffUtils.getQuotedName(
232
 
                                column.getName(),
233
 
                                arguments.isQuoteNames()));
 
224
                        "\tDROP COLUMN " + PgDiffUtils.getQuotedName(
 
225
                        column.getName(),
 
226
                        arguments.isQuoteNames()));
234
227
            }
235
228
        }
236
229
    }
247
240
     *        value should be dropped
248
241
     */
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())) {
257
250
                continue;
259
252
 
260
253
            final PgColumn oldColumn = oldTable.getColumn(newColumn.getName());
261
254
            final String newColumnName =
262
 
                PgDiffUtils.getQuotedName(
263
 
                        newColumn.getName(),
264
 
                        arguments.isQuoteNames());
 
255
                    PgDiffUtils.getQuotedName(
 
256
                    newColumn.getName(),
 
257
                    arguments.isQuoteNames());
265
258
 
266
259
            if (!oldColumn.getType().equals(newColumn.getType())) {
267
260
                commands.add(
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() +
 
265
                        " */");
270
266
            }
271
267
 
272
268
            final String oldDefault =
273
 
                (oldColumn.getDefaultValue() == null) ? ""
274
 
                                                      : oldColumn
275
 
                .getDefaultValue();
 
269
                    (oldColumn.getDefaultValue() == null) ? ""
 
270
                    : oldColumn.getDefaultValue();
276
271
            final String newDefault =
277
 
                (newColumn.getDefaultValue() == null) ? ""
278
 
                                                      : newColumn
279
 
                .getDefaultValue();
 
272
                    (newColumn.getDefaultValue() == null) ? ""
 
273
                    : newColumn.getDefaultValue();
280
274
 
281
275
            if (!oldDefault.equals(newDefault)) {
282
276
                if (newDefault.length() == 0) {
284
278
                            "\tALTER COLUMN " + newColumnName + " DROP DEFAULT");
285
279
                } else {
286
280
                    commands.add(
287
 
                            "\tALTER COLUMN " + newColumnName + " SET DEFAULT "
288
 
                            + newDefault);
 
281
                            "\tALTER COLUMN " + newColumnName + " SET DEFAULT " +
 
282
                            newDefault);
289
283
                }
290
284
            }
291
285
 
292
286
            if (oldColumn.getNullValue() != newColumn.getNullValue()) {
293
287
                if (newColumn.getNullValue()) {
294
288
                    commands.add(
295
 
                            "\tALTER COLUMN " + newColumnName
296
 
                            + " DROP NOT NULL");
 
289
                            "\tALTER COLUMN " + newColumnName + " DROP NOT NULL");
297
290
                } else {
298
291
                    if (arguments.isAddDefaults()) {
299
292
                        final String defaultValue =
300
 
                            PgColumnUtils.getDefaultValue(newColumn.getType());
 
293
                                PgColumnUtils.getDefaultValue(
 
294
                                newColumn.getType());
301
295
 
302
296
                        if (defaultValue != null) {
303
297
                            commands.add(
304
 
                                    "\tALTER COLUMN " + newColumnName
305
 
                                    + " SET DEFAULT " + defaultValue);
 
298
                                    "\tALTER COLUMN " + newColumnName +
 
299
                                    " SET DEFAULT " + defaultValue);
306
300
                            dropDefaultsColumns.add(newColumn);
307
301
                        }
308
302
                    }
324
318
     * @param newTable new table
325
319
     */
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();
333
327
 
334
328
        if ((oldInherits == null) && (newInherits != null)) {
335
329
            writer.println();
336
330
            writer.println(
337
 
                    "Modified INHERITS on TABLE "
338
 
                    + PgDiffUtils.getQuotedName(
339
 
                            newTable.getName(),
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(
 
332
                    newTable.getName(),
 
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();
345
338
            writer.println(
346
 
                    "Modified INHERITS on TABLE "
347
 
                    + PgDiffUtils.getQuotedName(
348
 
                            newTable.getName(),
349
 
                            arguments.isQuoteNames())
350
 
                    + ": original table uses INHERITS "
351
 
                    + oldTable.getInherits()
352
 
                    + " but new table doesn't use INHERITS");
353
 
        } else if (
354
 
            (oldInherits != null)
355
 
                && (newInherits != null)
356
 
                && !oldInherits.equals(newInherits)) {
 
339
                    "Modified INHERITS on TABLE " + PgDiffUtils.getQuotedName(
 
340
                    newTable.getName(),
 
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();
358
347
            writer.println(
359
 
                    "Modified INHERITS on TABLE "
360
 
                    + PgDiffUtils.getQuotedName(
361
 
                            newTable.getName(),
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(
 
349
                    newTable.getName(),
 
350
                    arguments.isQuoteNames()) +
 
351
                    ": original table uses INHERITS " + oldTable.getInherits() +
 
352
                    " but new table uses INHERITS " + newTable.getInherits());
366
353
        }
367
354
    }
368
355
 
377
364
     * @param newTable new table
378
365
     */
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();
386
373
            writer.println(
387
 
                    "ALTER TABLE "
388
 
                    + PgDiffUtils.getQuotedName(
389
 
                            newTable.getName(),
390
 
                            arguments.isQuoteNames()));
 
374
                    "ALTER TABLE " + PgDiffUtils.getQuotedName(
 
375
                    newTable.getName(),
 
376
                    arguments.isQuoteNames()));
391
377
            writer.println("\tSET WITHOUT OIDS;");
392
378
        } else if (!oldTable.isWithOIDS() && newTable.isWithOIDS()) {
393
379
            writer.println();
394
380
            writer.println(
395
 
                    "WARNING: Table "
396
 
                    + PgDiffUtils.getQuotedName(
397
 
                            newTable.getName(),
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(
 
382
                    newTable.getName(),
 
383
                    arguments.isQuoteNames()) +
 
384
                    " adds WITH OIDS but there is no equivalent command " +
 
385
                    "for adding of OIDS in PostgreSQL");
401
386
        }
402
387
    }
403
388
 
410
395
     * @param newSchema new schema
411
396
     */
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()) {
418
 
            if (
419
 
                (oldSchema == null)
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()));
423
406
            }
433
416
     * @param newSchema new schema
434
417
     */
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
458
441
     */
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);
479
462
 
480
463
        if (commands.size() > 0) {
481
464
            final String quotedTableName =
482
 
                PgDiffUtils.getQuotedName(
483
 
                        newTable.getName(),
484
 
                        arguments.isQuoteNames());
 
465
                    PgDiffUtils.getQuotedName(
 
466
                    newTable.getName(),
 
467
                    arguments.isQuoteNames());
485
468
            writer.println();
486
469
            writer.println("ALTER TABLE " + quotedTableName);
487
470
 
498
481
                    writer.print("\tALTER COLUMN ");
499
482
                    writer.print(
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");
504
487
                    writer.println(
505
488
                            ((i + 1) < dropDefaultsColumns.size()) ? "," : ";");