0
# Core Database Operations
1
2
This document covers the primary database migration and management operations provided by the main Liquibase class.
3
4
## Imports
5
6
```java { .api }
7
import liquibase.Liquibase;
8
import liquibase.database.Database;
9
import liquibase.database.DatabaseConnection;
10
import liquibase.resource.ResourceAccessor;
11
import liquibase.changelog.DatabaseChangeLog;
12
import liquibase.changelog.ChangeSet;
13
import liquibase.Contexts;
14
import liquibase.LabelExpression;
15
import liquibase.exception.LiquibaseException;
16
import liquibase.exception.DatabaseException;
17
18
import java.util.List;
19
import java.util.Date;
20
import java.util.Collection;
21
import java.io.Writer;
22
import java.io.PrintStream;
23
24
// Additional types for complete API coverage
25
import liquibase.changelog.ChangeSetStatus;
26
import liquibase.changelog.RanChangeSet;
27
import liquibase.change.CheckSum;
28
import liquibase.lockservice.DatabaseChangeLogLock;
29
import liquibase.structure.core.CatalogAndSchema;
30
import liquibase.changelog.visitor.DefaultChangeExecListener;
31
import liquibase.logging.Logger;
32
import liquibase.diff.DiffResult;
33
import liquibase.diff.compare.CompareControl;
34
import liquibase.diff.output.changelog.DiffToChangeLog;
35
import liquibase.serializer.ChangeLogSerializer;
36
import liquibase.structure.DatabaseObject;
37
import liquibase.command.CommandExecutionException;
38
```
39
40
## Liquibase Class Construction
41
42
The main Liquibase class serves as the primary facade for all database operations.
43
44
```java { .api }
45
/**
46
* Create Liquibase instance with changelog file
47
* @param changeLogFile Path to changelog file
48
* @param resourceAccessor Resource accessor for file access
49
* @param conn Database connection
50
*/
51
public Liquibase(String changeLogFile, ResourceAccessor resourceAccessor, DatabaseConnection conn) throws LiquibaseException
52
53
/**
54
* Create Liquibase instance with database object
55
* @param changeLogFile Path to changelog file
56
* @param resourceAccessor Resource accessor for file access
57
* @param database Database instance
58
*/
59
public Liquibase(String changeLogFile, ResourceAccessor resourceAccessor, Database database)
60
61
/**
62
* Create Liquibase instance with changelog object
63
* @param changeLog Parsed changelog object
64
* @param resourceAccessor Resource accessor for file access
65
* @param database Database instance
66
*/
67
public Liquibase(DatabaseChangeLog changeLog, ResourceAccessor resourceAccessor, Database database)
68
```
69
70
## Update Operations
71
72
### Basic Updates
73
74
Execute database updates to apply pending changesets:
75
76
```java { .api }
77
/**
78
* Update database with all pending changesets
79
*/
80
public void update() throws LiquibaseException
81
82
/**
83
* Update database with context filtering
84
* @param contexts Comma-separated list of contexts
85
*/
86
public void update(String contexts) throws LiquibaseException
87
88
/**
89
* Update database with contexts object
90
* @param contexts Contexts for filtering changesets
91
*/
92
public void update(Contexts contexts) throws LiquibaseException
93
94
/**
95
* Update database with contexts and labels
96
* @param contexts Contexts for filtering changesets
97
* @param labelExpression Label expression for filtering
98
*/
99
public void update(Contexts contexts, LabelExpression labelExpression) throws LiquibaseException
100
101
/**
102
* Update database with full control
103
* @param contexts Contexts for filtering changesets
104
* @param labelExpression Label expression for filtering
105
* @param checkLiquibaseTables Whether to check Liquibase tracking tables
106
*/
107
public void update(Contexts contexts, LabelExpression labelExpression, boolean checkLiquibaseTables) throws LiquibaseException
108
```
109
110
### Limited Updates
111
112
Execute a specific number of changesets or update to a specific tag:
113
114
```java { .api }
115
/**
116
* Update database with limited number of changesets
117
* @param changesToApply Number of changesets to apply
118
* @param contexts Comma-separated list of contexts
119
*/
120
public void update(int changesToApply, String contexts) throws LiquibaseException
121
122
/**
123
* Update database with limited number of changesets (full control)
124
* @param changesToApply Number of changesets to apply
125
* @param contexts Contexts for filtering changesets
126
* @param labelExpression Label expression for filtering
127
*/
128
public void update(int changesToApply, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException
129
130
/**
131
* Update database to specific tag
132
* @param tag Target tag to update to
133
* @param contexts Comma-separated list of contexts
134
*/
135
public void update(String tag, String contexts) throws LiquibaseException
136
137
/**
138
* Update database to specific tag (full control)
139
* @param tag Target tag to update to
140
* @param contexts Contexts for filtering changesets
141
*/
142
public void update(String tag, Contexts contexts) throws LiquibaseException
143
144
/**
145
* Update database to specific tag (full control with labels)
146
* @param tag Target tag to update to
147
* @param contexts Contexts for filtering changesets
148
* @param labelExpression Label expression for filtering
149
*/
150
public void update(String tag, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException
151
```
152
153
### Update SQL Generation
154
155
Generate SQL for update operations without executing them:
156
157
```java { .api }
158
/**
159
* Generate update SQL
160
* @param contexts Contexts for filtering changesets
161
* @param labelExpression Label expression for filtering
162
* @param output Writer for SQL output
163
*/
164
public void updateSql(Contexts contexts, LabelExpression labelExpression, Writer output) throws LiquibaseException
165
166
/**
167
* Generate SQL for limited number of changesets
168
* @param count Number of changesets to generate SQL for
169
* @param contexts Contexts for filtering changesets
170
* @param labelExpression Label expression for filtering
171
* @param output Writer for SQL output
172
*/
173
public void updateCountSql(int count, Contexts contexts, LabelExpression labelExpression, Writer output) throws LiquibaseException
174
175
/**
176
* Generate SQL for update to tag
177
* @param tag Target tag to generate SQL for
178
* @param contexts Contexts for filtering changesets
179
* @param labelExpression Label expression for filtering
180
* @param writer Writer for SQL output
181
*/
182
public void updateToTagSql(String tag, Contexts contexts, LabelExpression labelExpression, Writer writer) throws LiquibaseException
183
```
184
185
### Update with SQL Output (Deprecated Methods)
186
187
The following update methods with Writer output are deprecated but still available:
188
189
```java { .api }
190
/**
191
* Update database with SQL output (deprecated)
192
* @param contexts Comma-separated list of contexts
193
* @param output Writer for SQL output
194
* @deprecated Use updateSql instead
195
*/
196
@Deprecated
197
public void update(String contexts, Writer output) throws LiquibaseException
198
199
/**
200
* Update database with SQL output (deprecated)
201
* @param contexts Contexts for filtering changesets
202
* @param output Writer for SQL output
203
* @deprecated Use updateSql instead
204
*/
205
@Deprecated
206
public void update(Contexts contexts, Writer output) throws LiquibaseException
207
208
/**
209
* Update database with SQL output (deprecated)
210
* @param contexts Contexts for filtering changesets
211
* @param labelExpression Label expression for filtering
212
* @param output Writer for SQL output
213
* @deprecated Use updateSql instead
214
*/
215
@Deprecated
216
public void update(Contexts contexts, LabelExpression labelExpression, Writer output) throws LiquibaseException
217
218
/**
219
* Update database with SQL output and control (deprecated)
220
* @param contexts Contexts for filtering changesets
221
* @param labelExpression Label expression for filtering
222
* @param output Writer for SQL output
223
* @param checkLiquibaseTables Whether to check Liquibase tracking tables
224
* @deprecated Use updateSql instead
225
*/
226
@Deprecated
227
public void update(Contexts contexts, LabelExpression labelExpression, Writer output, boolean checkLiquibaseTables) throws LiquibaseException
228
229
/**
230
* Update limited changesets with SQL output
231
* @param changesToApply Number of changesets to apply
232
* @param contexts Comma-separated list of contexts
233
* @param output Writer for SQL output
234
*/
235
public void update(int changesToApply, String contexts, Writer output) throws LiquibaseException
236
237
/**
238
* Update limited changesets with SQL output (full control)
239
* @param changesToApply Number of changesets to apply
240
* @param contexts Contexts for filtering changesets
241
* @param labelExpression Label expression for filtering
242
* @param output Writer for SQL output
243
*/
244
public void update(int changesToApply, Contexts contexts, LabelExpression labelExpression, Writer output) throws LiquibaseException
245
246
/**
247
* Update to tag with SQL output
248
* @param tag Target tag to update to
249
* @param contexts Comma-separated list of contexts
250
* @param output Writer for SQL output
251
*/
252
public void update(String tag, String contexts, Writer output) throws LiquibaseException
253
254
/**
255
* Update to tag with SQL output (full control)
256
* @param tag Target tag to update to
257
* @param contexts Contexts for filtering changesets
258
* @param labelExpression Label expression for filtering
259
* @param output Writer for SQL output
260
*/
261
public void update(String tag, Contexts contexts, LabelExpression labelExpression, Writer output) throws LiquibaseException
262
```
263
264
## Rollback Operations
265
266
### Rollback by Count
267
268
Rollback a specific number of changesets:
269
270
```java { .api }
271
/**
272
* Rollback specified number of changesets
273
* @param changesToRollback Number of changesets to rollback
274
* @param contexts Comma-separated list of contexts
275
*/
276
public void rollback(int changesToRollback, String contexts) throws LiquibaseException
277
278
/**
279
* Rollback with contexts and labels
280
* @param changesToRollback Number of changesets to rollback
281
* @param contexts Contexts for filtering
282
* @param labelExpression Label expression for filtering
283
*/
284
public void rollback(int changesToRollback, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException
285
286
/**
287
* Rollback with custom rollback script
288
* @param changesToRollback Number of changesets to rollback
289
* @param rollbackScript Custom rollback script
290
* @param contexts Comma-separated list of contexts
291
*/
292
public void rollback(int changesToRollback, String rollbackScript, String contexts) throws LiquibaseException
293
294
/**
295
* Rollback with custom rollback script (full control)
296
* @param changesToRollback Number of changesets to rollback
297
* @param rollbackScript Custom rollback script
298
* @param contexts Contexts for filtering
299
* @param labelExpression Label expression for filtering
300
*/
301
public void rollback(int changesToRollback, String rollbackScript, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException
302
```
303
304
### Rollback by Count with SQL Output
305
306
Generate SQL for rollback operations by count:
307
308
```java { .api }
309
/**
310
* Generate rollback SQL by count
311
* @param changesToRollback Number of changesets to rollback
312
* @param contexts Comma-separated list of contexts
313
* @param output Writer for SQL output
314
*/
315
public void rollback(int changesToRollback, String contexts, Writer output) throws LiquibaseException
316
317
/**
318
* Generate rollback SQL by count with contexts
319
* @param changesToRollback Number of changesets to rollback
320
* @param contexts Contexts for filtering
321
* @param output Writer for SQL output
322
*/
323
public void rollback(int changesToRollback, Contexts contexts, Writer output) throws LiquibaseException
324
325
/**
326
* Generate rollback SQL by count (full control)
327
* @param changesToRollback Number of changesets to rollback
328
* @param contexts Contexts for filtering
329
* @param labelExpression Label expression for filtering
330
* @param output Writer for SQL output
331
*/
332
public void rollback(int changesToRollback, Contexts contexts, LabelExpression labelExpression, Writer output) throws LiquibaseException
333
334
/**
335
* Generate rollback SQL by count with custom script
336
* @param changesToRollback Number of changesets to rollback
337
* @param rollbackScript Custom rollback script
338
* @param contexts Comma-separated list of contexts
339
* @param output Writer for SQL output
340
*/
341
public void rollback(int changesToRollback, String rollbackScript, String contexts, Writer output) throws LiquibaseException
342
343
/**
344
* Generate rollback SQL by count with custom script (contexts)
345
* @param changesToRollback Number of changesets to rollback
346
* @param rollbackScript Custom rollback script
347
* @param contexts Contexts for filtering
348
* @param output Writer for SQL output
349
*/
350
public void rollback(int changesToRollback, String rollbackScript, Contexts contexts, Writer output) throws LiquibaseException
351
352
/**
353
* Generate rollback SQL by count with custom script (full control)
354
* @param changesToRollback Number of changesets to rollback
355
* @param rollbackScript Custom rollback script
356
* @param contexts Contexts for filtering
357
* @param labelExpression Label expression for filtering
358
* @param output Writer for SQL output
359
*/
360
public void rollback(int changesToRollback, String rollbackScript, Contexts contexts, LabelExpression labelExpression, Writer output) throws LiquibaseException
361
```
362
363
### Rollback by Tag
364
365
Rollback to a specific tag:
366
367
```java { .api }
368
/**
369
* Rollback to specific tag
370
* @param tagToRollBackTo Target tag name
371
* @param contexts Comma-separated list of contexts
372
*/
373
public void rollback(String tagToRollBackTo, String contexts) throws LiquibaseException
374
375
/**
376
* Rollback to tag with contexts
377
* @param tagToRollBackTo Target tag name
378
* @param contexts Contexts for filtering
379
*/
380
public void rollback(String tagToRollBackTo, Contexts contexts) throws LiquibaseException
381
382
/**
383
* Rollback to tag with full control
384
* @param tagToRollBackTo Target tag name
385
* @param contexts Contexts for filtering
386
* @param labelExpression Label expression for filtering
387
*/
388
public void rollback(String tagToRollBackTo, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException
389
390
/**
391
* Rollback to tag with custom rollback script
392
* @param tagToRollBackTo Target tag name
393
* @param rollbackScript Custom rollback script
394
* @param contexts Comma-separated list of contexts
395
*/
396
public void rollback(String tagToRollBackTo, String rollbackScript, String contexts) throws LiquibaseException
397
398
/**
399
* Rollback to tag with custom rollback script (contexts)
400
* @param tagToRollBackTo Target tag name
401
* @param rollbackScript Custom rollback script
402
* @param contexts Contexts for filtering
403
*/
404
public void rollback(String tagToRollBackTo, String rollbackScript, Contexts contexts) throws LiquibaseException
405
406
/**
407
* Rollback to tag with custom rollback script (full control)
408
* @param tagToRollBackTo Target tag name
409
* @param rollbackScript Custom rollback script
410
* @param contexts Contexts for filtering
411
* @param labelExpression Label expression for filtering
412
*/
413
public void rollback(String tagToRollBackTo, String rollbackScript, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException
414
```
415
416
### Rollback by Tag with SQL Output
417
418
Generate SQL for rollback operations by tag:
419
420
```java { .api }
421
/**
422
* Generate rollback SQL to tag
423
* @param tagToRollBackTo Target tag name
424
* @param contexts Comma-separated list of contexts
425
* @param output Writer for SQL output
426
*/
427
public void rollback(String tagToRollBackTo, String contexts, Writer output) throws LiquibaseException
428
429
/**
430
* Generate rollback SQL to tag with contexts
431
* @param tagToRollBackTo Target tag name
432
* @param contexts Contexts for filtering
433
* @param output Writer for SQL output
434
*/
435
public void rollback(String tagToRollBackTo, Contexts contexts, Writer output) throws LiquibaseException
436
437
/**
438
* Generate rollback SQL to tag (full control)
439
* @param tagToRollBackTo Target tag name
440
* @param contexts Contexts for filtering
441
* @param labelExpression Label expression for filtering
442
* @param output Writer for SQL output
443
*/
444
public void rollback(String tagToRollBackTo, Contexts contexts, LabelExpression labelExpression, Writer output) throws LiquibaseException
445
446
/**
447
* Generate rollback SQL to tag with custom script
448
* @param tagToRollBackTo Target tag name
449
* @param rollbackScript Custom rollback script
450
* @param contexts Comma-separated list of contexts
451
* @param output Writer for SQL output
452
*/
453
public void rollback(String tagToRollBackTo, String rollbackScript, String contexts, Writer output) throws LiquibaseException
454
455
/**
456
* Generate rollback SQL to tag with custom script (contexts)
457
* @param tagToRollBackTo Target tag name
458
* @param rollbackScript Custom rollback script
459
* @param contexts Contexts for filtering
460
* @param output Writer for SQL output
461
*/
462
public void rollback(String tagToRollBackTo, String rollbackScript, Contexts contexts, Writer output) throws LiquibaseException
463
464
/**
465
* Generate rollback SQL to tag with custom script (full control)
466
* @param tagToRollBackTo Target tag name
467
* @param rollbackScript Custom rollback script
468
* @param contexts Contexts for filtering
469
* @param labelExpression Label expression for filtering
470
* @param output Writer for SQL output
471
*/
472
public void rollback(String tagToRollBackTo, String rollbackScript, Contexts contexts, LabelExpression labelExpression, Writer output) throws LiquibaseException
473
```
474
475
### Rollback by Date
476
477
Rollback to a specific point in time:
478
479
```java { .api }
480
/**
481
* Rollback to specific date
482
* @param dateToRollBackTo Target date
483
* @param contexts Comma-separated list of contexts
484
*/
485
public void rollback(Date dateToRollBackTo, String contexts) throws LiquibaseException
486
487
/**
488
* Rollback to date with full control
489
* @param dateToRollBackTo Target date
490
* @param contexts Contexts for filtering
491
* @param labelExpression Label expression for filtering
492
*/
493
public void rollback(Date dateToRollBackTo, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException
494
495
/**
496
* Rollback to date with custom rollback script
497
* @param dateToRollBackTo Target date
498
* @param rollbackScript Custom rollback script
499
* @param contexts Comma-separated list of contexts
500
*/
501
public void rollback(Date dateToRollBackTo, String rollbackScript, String contexts) throws LiquibaseException
502
503
/**
504
* Rollback to date with custom rollback script (full control)
505
* @param dateToRollBackTo Target date
506
* @param rollbackScript Custom rollback script
507
* @param contexts Contexts for filtering
508
* @param labelExpression Label expression for filtering
509
*/
510
public void rollback(Date dateToRollBackTo, String rollbackScript, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException
511
```
512
513
### Rollback by Date with SQL Output
514
515
Generate SQL for rollback operations by date:
516
517
```java { .api }
518
/**
519
* Generate rollback SQL to date
520
* @param dateToRollBackTo Target date
521
* @param contexts Comma-separated list of contexts
522
* @param output Writer for SQL output
523
*/
524
public void rollback(Date dateToRollBackTo, String contexts, Writer output) throws LiquibaseException
525
526
/**
527
* Generate rollback SQL to date with custom script
528
* @param dateToRollBackTo Target date
529
* @param rollbackScript Custom rollback script
530
* @param contexts Comma-separated list of contexts
531
* @param output Writer for SQL output
532
*/
533
public void rollback(Date dateToRollBackTo, String rollbackScript, String contexts, Writer output) throws LiquibaseException
534
535
/**
536
* Generate rollback SQL to date (full control)
537
* @param dateToRollBackTo Target date
538
* @param contexts Contexts for filtering
539
* @param labelExpression Label expression for filtering
540
* @param output Writer for SQL output
541
*/
542
public void rollback(Date dateToRollBackTo, Contexts contexts, LabelExpression labelExpression, Writer output) throws LiquibaseException
543
544
/**
545
* Generate rollback SQL to date with custom script (full control)
546
* @param dateToRollBackTo Target date
547
* @param rollbackScript Custom rollback script
548
* @param contexts Contexts for filtering
549
* @param labelExpression Label expression for filtering
550
* @param output Writer for SQL output
551
*/
552
public void rollback(Date dateToRollBackTo, String rollbackScript, Contexts contexts, LabelExpression labelExpression, Writer output) throws LiquibaseException
553
```
554
555
### Future Rollback SQL Generation
556
557
Generate rollback SQL for pending changesets (useful for planning rollback strategies):
558
559
```java { .api }
560
/**
561
* Generate future rollback SQL for all pending changesets
562
* @param output Writer for SQL output
563
*/
564
public void futureRollbackSQL(Writer output) throws LiquibaseException
565
566
/**
567
* Generate future rollback SQL with context filtering
568
* @param contexts Comma-separated list of contexts
569
* @param output Writer for SQL output
570
*/
571
public void futureRollbackSQL(String contexts, Writer output) throws LiquibaseException
572
573
/**
574
* Generate future rollback SQL with table checking control
575
* @param contexts Comma-separated list of contexts
576
* @param output Writer for SQL output
577
* @param checkLiquibaseTables Whether to check Liquibase tracking tables
578
*/
579
public void futureRollbackSQL(String contexts, Writer output, boolean checkLiquibaseTables) throws LiquibaseException
580
581
/**
582
* Generate future rollback SQL for limited number of changesets
583
* @param count Number of changesets to generate rollback SQL for
584
* @param contexts Comma-separated list of contexts
585
* @param output Writer for SQL output
586
*/
587
public void futureRollbackSQL(Integer count, String contexts, Writer output) throws LiquibaseException
588
589
/**
590
* Generate future rollback SQL (full control)
591
* @param contexts Contexts for filtering changesets
592
* @param labelExpression Label expression for filtering
593
* @param output Writer for SQL output
594
*/
595
public void futureRollbackSQL(Contexts contexts, LabelExpression labelExpression, Writer output) throws LiquibaseException
596
597
/**
598
* Generate future rollback SQL for limited changesets with table checking
599
* @param count Number of changesets to generate rollback SQL for
600
* @param contexts Comma-separated list of contexts
601
* @param output Writer for SQL output
602
* @param checkLiquibaseTables Whether to check Liquibase tracking tables
603
*/
604
public void futureRollbackSQL(Integer count, String contexts, Writer output, boolean checkLiquibaseTables) throws LiquibaseException
605
606
/**
607
* Generate future rollback SQL for limited changesets (full control)
608
* @param count Number of changesets to generate rollback SQL for
609
* @param contexts Contexts for filtering changesets
610
* @param labelExpression Label expression for filtering
611
* @param output Writer for SQL output
612
*/
613
public void futureRollbackSQL(Integer count, Contexts contexts, LabelExpression labelExpression, Writer output) throws LiquibaseException
614
615
/**
616
* Generate future rollback SQL for limited changesets (full control with table checking)
617
* @param count Number of changesets to generate rollback SQL for
618
* @param contexts Contexts for filtering changesets
619
* @param labelExpression Label expression for filtering
620
* @param output Writer for SQL output
621
* @param checkLiquibaseTables Whether to check Liquibase tracking tables
622
*/
623
public void futureRollbackSQL(Integer count, Contexts contexts, LabelExpression labelExpression, Writer output, boolean checkLiquibaseTables) throws LiquibaseException
624
625
/**
626
* Generate future rollback SQL to specific tag
627
* @param tag Target tag to generate rollback SQL for
628
* @param contexts Contexts for filtering changesets
629
* @param labelExpression Label expression for filtering
630
* @param output Writer for SQL output
631
*/
632
public void futureRollbackSQL(String tag, Contexts contexts, LabelExpression labelExpression, Writer output) throws LiquibaseException
633
```
634
635
## Database Management Operations
636
637
### Schema Operations
638
639
Drop database objects and manage schema:
640
641
```java { .api }
642
/**
643
* Drop all database objects tracked by Liquibase
644
*/
645
public void dropAll() throws DatabaseException
646
647
/**
648
* Drop all database objects with history control
649
* @param dropDbclhistory Whether to drop database changelog history table
650
*/
651
public void dropAll(Boolean dropDbclhistory) throws DatabaseException
652
653
/**
654
* Drop all objects in specific schemas
655
* @param schemas Target schemas to drop objects from
656
*/
657
public void dropAll(CatalogAndSchema... schemas) throws DatabaseException
658
659
/**
660
* Drop all objects in specific schemas with history control
661
* @param dropDbclhistory Whether to drop database changelog history table
662
* @param schemas Target schemas to drop objects from
663
*/
664
public void dropAll(Boolean dropDbclhistory, CatalogAndSchema... schemas) throws DatabaseException
665
```
666
667
### Tagging Operations
668
669
Manage database tags for rollback points:
670
671
```java { .api }
672
/**
673
* Tag the current database state
674
* @param tagString Tag name to create
675
*/
676
public void tag(String tagString) throws LiquibaseException
677
678
/**
679
* Check if a tag exists in the database
680
* @param tagString Tag name to check
681
* @return true if tag exists
682
*/
683
public boolean tagExists(String tagString) throws LiquibaseException
684
```
685
686
### Checksum Operations
687
688
Manage changeset checksums for validation:
689
690
```java { .api }
691
/**
692
* Clear all checksums for recalculation
693
*/
694
public void clearCheckSums() throws LiquibaseException
695
696
/**
697
* Calculate checksum for specific changeset by identifier
698
* @param changeSetIdentifier Changeset identifier string
699
* @return Calculated checksum
700
*/
701
public CheckSum calculateCheckSum(String changeSetIdentifier) throws LiquibaseException
702
703
/**
704
* Calculate checksum for specific changeset
705
* @param changeSetPath Changeset file path
706
* @param changeSetId Changeset ID
707
* @param changeSetAuthor Changeset author
708
* @return Calculated checksum
709
*/
710
public CheckSum calculateCheckSum(String changeSetPath, String changeSetId, String changeSetAuthor) throws LiquibaseException
711
```
712
713
## Status and Reporting Operations
714
715
### Changeset Status
716
717
Query changeset execution status:
718
719
```java { .api }
720
/**
721
* Get list of unrun changesets (deprecated)
722
* @param contexts Contexts for filtering
723
* @return List of unrun changesets
724
* @deprecated Use listUnrunChangeSets(Contexts, LabelExpression) instead
725
*/
726
@Deprecated
727
public List<ChangeSet> listUnrunChangeSets(Contexts contexts) throws LiquibaseException
728
729
/**
730
* Get list of unrun changesets
731
* @param contexts Contexts for filtering
732
* @param labels Label expression for filtering
733
* @return List of unrun changesets
734
*/
735
public List<ChangeSet> listUnrunChangeSets(Contexts contexts, LabelExpression labels) throws LiquibaseException
736
737
/**
738
* Get list of unrun changesets with table checking control
739
* @param contexts Contexts for filtering
740
* @param labels Label expression for filtering
741
* @param checkLiquibaseTables Whether to check Liquibase tracking tables
742
* @return List of unrun changesets
743
*/
744
public List<ChangeSet> listUnrunChangeSets(Contexts contexts, LabelExpression labels, boolean checkLiquibaseTables) throws LiquibaseException
745
746
/**
747
* Get detailed status of all changesets (deprecated)
748
* @param contexts Contexts for filtering
749
* @return List of changeset statuses
750
* @deprecated Use getChangeSetStatuses(Contexts, LabelExpression) instead
751
*/
752
@Deprecated
753
public List<ChangeSetStatus> getChangeSetStatuses(Contexts contexts) throws LiquibaseException
754
755
/**
756
* Get detailed status of all changesets
757
* @param contexts Contexts for filtering
758
* @param labelExpression Label expression for filtering
759
* @return List of changeset statuses
760
*/
761
public List<ChangeSetStatus> getChangeSetStatuses(Contexts contexts, LabelExpression labelExpression) throws LiquibaseException
762
763
/**
764
* Get detailed status of all changesets with table checking control
765
* @param contexts Contexts for filtering
766
* @param labelExpression Label expression for filtering
767
* @param checkLiquibaseTables Whether to check Liquibase tracking tables
768
* @return List of changeset statuses
769
*/
770
public List<ChangeSetStatus> getChangeSetStatuses(Contexts contexts, LabelExpression labelExpression, boolean checkLiquibaseTables) throws LiquibaseException
771
772
/**
773
* Get unexpected changesets (run but not in changelog)
774
* @param contexts Comma-separated list of contexts
775
* @return Collection of unexpected changesets
776
*/
777
public Collection<RanChangeSet> listUnexpectedChangeSets(String contexts) throws LiquibaseException
778
779
/**
780
* Get unexpected changesets with full control
781
* @param contexts Contexts for filtering
782
* @param labelExpression Label expression for filtering
783
* @return Collection of unexpected changesets
784
*/
785
public Collection<RanChangeSet> listUnexpectedChangeSets(Contexts contexts, LabelExpression labelExpression) throws LiquibaseException
786
```
787
788
### Status Reporting
789
790
Generate status reports:
791
792
```java { .api }
793
/**
794
* Generate status report with contexts (string)
795
* @param verbose Include verbose information
796
* @param contexts Comma-separated list of contexts
797
* @param out Writer for output
798
*/
799
public void reportStatus(boolean verbose, String contexts, Writer out) throws LiquibaseException
800
801
/**
802
* Generate status report with contexts object
803
* @param verbose Include verbose information
804
* @param contexts Contexts for filtering
805
* @param out Writer for output
806
*/
807
public void reportStatus(boolean verbose, Contexts contexts, Writer out) throws LiquibaseException
808
809
/**
810
* Generate status report (full control)
811
* @param verbose Include verbose information
812
* @param contexts Contexts for filtering
813
* @param labels Label expression for filtering
814
* @param out Writer for output
815
*/
816
public void reportStatus(boolean verbose, Contexts contexts, LabelExpression labels, Writer out) throws LiquibaseException
817
818
/**
819
* Report unexpected changesets with contexts (string)
820
* @param verbose Include verbose information
821
* @param contexts Comma-separated list of contexts
822
* @param out Writer for output
823
*/
824
public void reportUnexpectedChangeSets(boolean verbose, String contexts, Writer out) throws LiquibaseException
825
826
/**
827
* Report unexpected changesets (full control)
828
* @param verbose Include verbose information
829
* @param contexts Contexts for filtering
830
* @param labelExpression Label expression for filtering
831
* @param out Writer for output
832
*/
833
public void reportUnexpectedChangeSets(boolean verbose, Contexts contexts, LabelExpression labelExpression, Writer out) throws LiquibaseException
834
```
835
836
### Lock Management
837
838
Manage database locks:
839
840
```java { .api }
841
/**
842
* List current database locks
843
* @return Array of current locks
844
*/
845
public DatabaseChangeLogLock[] listLocks() throws LiquibaseException
846
847
/**
848
* Report current locks to output stream
849
* @param out Output stream for lock report
850
*/
851
public void reportLocks(PrintStream out) throws LiquibaseException
852
853
/**
854
* Force release all database locks
855
*/
856
public void forceReleaseLocks() throws LiquibaseException
857
```
858
859
## Synchronization Operations
860
861
### Changelog Synchronization
862
863
Synchronize changelog state with database:
864
865
```java { .api }
866
/**
867
* Mark changelog changesets as run without executing them
868
* @param contexts Comma-separated list of contexts
869
*/
870
public void changeLogSync(String contexts) throws LiquibaseException
871
872
/**
873
* Mark changelog changesets as run with contexts object (deprecated)
874
* @param contexts Contexts for filtering
875
* @deprecated Use changeLogSync(Contexts, LabelExpression) instead
876
*/
877
@Deprecated
878
public void changeLogSync(Contexts contexts) throws LiquibaseException
879
880
/**
881
* Mark changelog changesets as run (full control)
882
* @param contexts Contexts for filtering
883
* @param labelExpression Label expression for filtering
884
*/
885
public void changeLogSync(Contexts contexts, LabelExpression labelExpression) throws LiquibaseException
886
887
/**
888
* Sync changelog to specific tag with contexts (string)
889
* @param tag Target tag to sync to
890
* @param contexts Comma-separated list of contexts
891
*/
892
public void changeLogSync(String tag, String contexts) throws LiquibaseException
893
894
/**
895
* Sync changelog to specific tag (full control)
896
* @param tag Target tag to sync to
897
* @param contexts Contexts for filtering
898
* @param labelExpression Label expression for filtering
899
*/
900
public void changeLogSync(String tag, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException
901
902
/**
903
* Mark next changeset as run without executing it
904
* @param contexts Comma-separated list of contexts
905
*/
906
public void markNextChangeSetRan(String contexts) throws LiquibaseException
907
908
/**
909
* Mark next changeset as run (full control)
910
* @param contexts Contexts for filtering
911
* @param labelExpression Label expression for filtering
912
*/
913
public void markNextChangeSetRan(Contexts contexts, LabelExpression labelExpression) throws LiquibaseException
914
```
915
916
### Synchronization with SQL Output
917
918
Generate SQL for synchronization operations:
919
920
```java { .api }
921
/**
922
* Generate sync SQL with contexts (string)
923
* @param contexts Comma-separated list of contexts
924
* @param output Writer for SQL output
925
*/
926
public void changeLogSync(String contexts, Writer output) throws LiquibaseException
927
928
/**
929
* Generate sync SQL (full control)
930
* @param contexts Contexts for filtering
931
* @param labelExpression Label expression for filtering
932
* @param output Writer for SQL output
933
*/
934
public void changeLogSync(Contexts contexts, LabelExpression labelExpression, Writer output) throws LiquibaseException
935
936
/**
937
* Generate sync to tag SQL with contexts (string)
938
* @param tag Target tag to sync to
939
* @param contexts Comma-separated list of contexts
940
* @param output Writer for SQL output
941
*/
942
public void changeLogSync(String tag, String contexts, Writer output) throws LiquibaseException
943
944
/**
945
* Generate sync to tag SQL (full control)
946
* @param tag Target tag to sync to
947
* @param contexts Contexts for filtering
948
* @param labelExpression Label expression for filtering
949
* @param output Writer for SQL output
950
*/
951
public void changeLogSync(String tag, Contexts contexts, LabelExpression labelExpression, Writer output) throws LiquibaseException
952
953
/**
954
* Generate mark next changeset SQL with contexts (string)
955
* @param contexts Comma-separated list of contexts
956
* @param output Writer for SQL output
957
*/
958
public void markNextChangeSetRan(String contexts, Writer output) throws LiquibaseException
959
960
/**
961
* Generate mark next changeset SQL (full control)
962
* @param contexts Contexts for filtering
963
* @param labelExpression Label expression for filtering
964
* @param output Writer for SQL output
965
*/
966
public void markNextChangeSetRan(Contexts contexts, LabelExpression labelExpression, Writer output) throws LiquibaseException
967
```
968
969
## Validation and Documentation
970
971
### Validation
972
973
Validate changelog files and database state:
974
975
```java { .api }
976
/**
977
* Validate changelog files and database consistency
978
*/
979
public void validate() throws LiquibaseException
980
```
981
982
### Documentation Generation
983
984
Generate database documentation:
985
986
```java { .api }
987
/**
988
* Generate database documentation
989
* @param outputDirectory Directory for generated documentation
990
*/
991
public void generateDocumentation(String outputDirectory) throws LiquibaseException
992
993
/**
994
* Generate database documentation with contexts
995
* @param outputDirectory Directory for generated documentation
996
* @param contexts Comma-separated list of contexts
997
*/
998
public void generateDocumentation(String outputDirectory, String contexts) throws LiquibaseException
999
1000
/**
1001
* Generate database documentation with contexts and schemas
1002
* @param outputDirectory Directory for generated documentation
1003
* @param contexts Comma-separated list of contexts
1004
* @param schemaList Specific schemas to document
1005
*/
1006
public void generateDocumentation(String outputDirectory, String contexts, CatalogAndSchema... schemaList) throws LiquibaseException
1007
1008
/**
1009
* Generate database documentation (full control)
1010
* @param outputDirectory Directory for generated documentation
1011
* @param contexts Contexts for filtering
1012
* @param labelExpression Label expression for filtering
1013
* @param schemaList Specific schemas to document
1014
*/
1015
public void generateDocumentation(String outputDirectory, Contexts contexts, LabelExpression labelExpression, CatalogAndSchema... schemaList) throws LiquibaseException
1016
```
1017
1018
## Testing and Advanced Operations
1019
1020
### Testing Methods
1021
1022
Methods for testing update and rollback operations:
1023
1024
```java { .api }
1025
/**
1026
* Test update and rollback operations
1027
* @param contexts Comma-separated list of contexts
1028
*/
1029
public void updateTestingRollback(String contexts) throws LiquibaseException
1030
1031
/**
1032
* Test update and rollback operations (full control)
1033
* @param contexts Contexts for filtering
1034
* @param labelExpression Label expression for filtering
1035
*/
1036
public void updateTestingRollback(Contexts contexts, LabelExpression labelExpression) throws LiquibaseException
1037
1038
/**
1039
* Test update to tag and rollback operations
1040
* @param tag Target tag to test update to
1041
* @param contexts Contexts for filtering
1042
* @param labelExpression Label expression for filtering
1043
*/
1044
public void updateTestingRollback(String tag, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException
1045
```
1046
1047
### Advanced Database Management
1048
1049
Additional database state and management operations:
1050
1051
```java { .api }
1052
/**
1053
* Check and update Liquibase tracking tables
1054
* @param updateExistingNullChecksums Whether to update existing null checksums
1055
* @param databaseChangeLog Database changelog object
1056
* @param contexts Contexts for filtering
1057
* @param labelExpression Label expression for filtering
1058
*/
1059
public void checkLiquibaseTables(boolean updateExistingNullChecksums, DatabaseChangeLog databaseChangeLog, Contexts contexts, LabelExpression labelExpression) throws LiquibaseException
1060
1061
/**
1062
* Check if it's safe to run update operations
1063
* @return true if safe to run update
1064
*/
1065
public boolean isSafeToRunUpdate() throws DatabaseException
1066
```
1067
1068
### Configuration and Lifecycle
1069
1070
Configuration and resource management methods:
1071
1072
```java { .api }
1073
/**
1074
* Set changelog parameter value
1075
* @param key Parameter key
1076
* @param value Parameter value
1077
*/
1078
public void setChangeLogParameter(String key, Object value)
1079
1080
/**
1081
* Close Liquibase instance and release resources (AutoCloseable)
1082
*/
1083
public void close() throws LiquibaseException
1084
1085
/**
1086
* Output header message (deprecated)
1087
* @param message Header message to output
1088
* @deprecated This method is deprecated
1089
*/
1090
@Deprecated
1091
public void outputHeader(String message) throws DatabaseException
1092
```
1093
1094
## Resource Access
1095
1096
### Property Access
1097
1098
Get Liquibase instance properties:
1099
1100
```java { .api }
1101
/**
1102
* Get associated database instance
1103
* @return Database instance
1104
*/
1105
public Database getDatabase()
1106
1107
/**
1108
* Get changelog file path
1109
* @return Changelog file path
1110
*/
1111
public String getChangeLogFile()
1112
1113
/**
1114
* Get resource accessor
1115
* @return Resource accessor instance
1116
*/
1117
public ResourceAccessor getResourceAccessor()
1118
1119
/**
1120
* Get changelog parameters
1121
* @return Changelog parameters instance
1122
*/
1123
public ChangeLogParameters getChangeLogParameters()
1124
1125
/**
1126
* Get logger instance
1127
* @return Logger instance
1128
*/
1129
public Logger getLog()
1130
1131
/**
1132
* Get database changelog object
1133
* @return Database changelog object
1134
* @throws LiquibaseException if changelog cannot be loaded
1135
*/
1136
public DatabaseChangeLog getDatabaseChangeLog() throws LiquibaseException
1137
1138
/**
1139
* Get default change execution listener
1140
* @return Default change execution listener
1141
*/
1142
public DefaultChangeExecListener getDefaultChangeExecListener()
1143
```
1144
1145
## Comparison and Change Generation
1146
1147
### Schema Comparison
1148
1149
Compare database schemas and generate difference reports:
1150
1151
```java { .api }
1152
/**
1153
* Compare two database schemas
1154
* @param referenceDatabase Reference database to compare against
1155
* @param targetDatabase Target database to compare
1156
* @param compareControl Control object specifying what to compare
1157
* @return Diff result containing differences
1158
*/
1159
public DiffResult diff(Database referenceDatabase, Database targetDatabase, CompareControl compareControl) throws LiquibaseException
1160
```
1161
1162
### Change Generation
1163
1164
Generate changelog from database structure:
1165
1166
```java { .api }
1167
/**
1168
* Generate changelog from database schema
1169
* @param catalogAndSchema Schema to generate changelog for
1170
* @param changeLogWriter Diff to changelog writer
1171
* @param outputStream Output stream for generated changelog
1172
* @param snapshotTypes Database object types to include
1173
*/
1174
public void generateChangeLog(CatalogAndSchema catalogAndSchema, DiffToChangeLog changeLogWriter, PrintStream outputStream, Class<? extends DatabaseObject>... snapshotTypes) throws DatabaseException, CommandExecutionException
1175
1176
/**
1177
* Generate changelog from database schema with custom serializer
1178
* @param catalogAndSchema Schema to generate changelog for
1179
* @param changeLogWriter Diff to changelog writer
1180
* @param outputStream Output stream for generated changelog
1181
* @param changeLogSerializer Custom changelog serializer
1182
* @param snapshotTypes Database object types to include
1183
*/
1184
public void generateChangeLog(CatalogAndSchema catalogAndSchema, DiffToChangeLog changeLogWriter, PrintStream outputStream, ChangeLogSerializer changeLogSerializer, Class<? extends DatabaseObject>... snapshotTypes) throws DatabaseException, CommandExecutionException
1185
```
1186
1187
## Example Usage
1188
1189
### Complete Update Example
1190
1191
```java { .api }
1192
import liquibase.Liquibase;
1193
import liquibase.database.DatabaseFactory;
1194
import liquibase.database.jvm.JdbcConnection;
1195
import liquibase.resource.ClassLoaderResourceAccessor;
1196
import liquibase.Contexts;
1197
import liquibase.LabelExpression;
1198
1199
// Setup database connection
1200
Connection connection = DriverManager.getConnection(
1201
"jdbc:postgresql://localhost:5432/mydb", "user", "password"
1202
);
1203
DatabaseConnection databaseConnection = new JdbcConnection(connection);
1204
Database database = DatabaseFactory.getInstance()
1205
.findCorrectDatabaseImplementation(databaseConnection);
1206
1207
// Create Liquibase instance
1208
Liquibase liquibase = new Liquibase(
1209
"db/changelog/db.changelog-master.xml",
1210
new ClassLoaderResourceAccessor(),
1211
database
1212
);
1213
1214
try {
1215
// Execute update with contexts and labels
1216
liquibase.update(
1217
new Contexts("dev", "test"),
1218
new LabelExpression("feature-1")
1219
);
1220
1221
// Tag current state
1222
liquibase.tag("release-1.0");
1223
1224
// Validate state
1225
liquibase.validate();
1226
1227
} finally {
1228
liquibase.close();
1229
}
1230
```
1231
1232
### Status and Rollback Example
1233
1234
```java { .api }
1235
// Check status
1236
List<ChangeSet> unrunChangeSets = liquibase.listUnrunChangeSets(
1237
new Contexts("prod"),
1238
new LabelExpression()
1239
);
1240
1241
System.out.println("Unrun changesets: " + unrunChangeSets.size());
1242
1243
// Rollback if needed
1244
if (needsRollback) {
1245
liquibase.rollback("release-1.0", "prod");
1246
}
1247
1248
// Generate rollback SQL for review
1249
StringWriter sqlOutput = new StringWriter();
1250
liquibase.rollbackToTagSql(
1251
"release-1.0",
1252
new Contexts("prod"),
1253
new LabelExpression(),
1254
sqlOutput
1255
);
1256
System.out.println("Rollback SQL:\n" + sqlOutput.toString());
1257
```