# How to create and apply DIFF programmatically?

**URL:** <https://forum.liquibase.org/t/how-to-create-and-apply-diff-programmatically/9561>\
**Category:** General Discussion\
**Created:** [July 25, 2024, 9:35am UTC](https://forum.liquibase.org/t/how-to-create-and-apply-diff-programmatically/9561 "2024-07-25T09:35:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![iceja](https://avatars.discourse-cdn.com/v4/letter/i/c2a13f/32.png) [@iceja](https://forum.liquibase.org/u/iceja)\
**Post date:** [July 25, 2024, 9:35am UTC](https://forum.liquibase.org/t/how-to-create-and-apply-diff-programmatically/9561/1 "2024-07-25T09:35:08Z")

</div>

The task is as follows: obtain a diff between the snapshot recorded on disk (an XML file) and the current database, and then store this diff to XML file  
everything should work from @Configuration SpringBoot application:

```auto
 @Bean
 @Order(4)
 public CommandLineRunner initEverithing(ResourceLoader resourceLoader) { ... and so on

```

not from CLI but from SpringBoot application.

---

<div class="post-metadata">

**Author:** ![wwillard](https://avatars.discourse-cdn.com/v4/letter/w/8e8cbc/32.png) [@wwillard](https://forum.liquibase.org/u/wwillard)\
**Post date:** [July 25, 2024, 1:44pm UTC](https://forum.liquibase.org/t/how-to-create-and-apply-diff-programmatically/9561/2 "2024-07-25T13:44:51Z")

</div>

To perform diff with a file (“offline” in Liquibase), you need to provide a snapshot of the database, not the changelog. You create this with the snapshot command, formatted as JSON.

---

<div class="post-metadata">

**Author:** ![iceja](https://avatars.discourse-cdn.com/v4/letter/i/c2a13f/32.png) [@iceja](https://forum.liquibase.org/u/iceja)\
**Post date:** [July 25, 2024, 4:44pm UTC](https://forum.liquibase.org/t/how-to-create-and-apply-diff-programmatically/9561/3 "2024-07-25T16:44:29Z")

</div>

Is there only way the CLI? Is there any programmatic API?

---

<div class="post-metadata">

**Author:** ![wwillard](https://avatars.discourse-cdn.com/v4/letter/w/8e8cbc/32.png) [@wwillard](https://forum.liquibase.org/u/wwillard)\
**Post date:** [July 25, 2024, 10:53pm UTC](https://forum.liquibase.org/t/how-to-create-and-apply-diff-programmatically/9561/4 "2024-07-25T22:53:24Z")

</div>

You just take a snapshot in JSON format, and then when you run diff you specify that as the `referenceURL` argument, like `offline:postgresql?snapshot=mysnapshot.json`. The `URL` argument is for the database.

You can take snapshot by using the Liquibase API.

---

<div class="post-metadata">

**Author:** ![iceja](https://avatars.discourse-cdn.com/v4/letter/i/c2a13f/32.png) [@iceja](https://forum.liquibase.org/u/iceja)\
**Post date:** [July 26, 2024, 11:30pm UTC](https://forum.liquibase.org/t/how-to-create-and-apply-diff-programmatically/9561/5 "2024-07-26T23:30:45Z")

</div>

How to run diff and take snapshot using the Liquibase API? ChatGPT don’t know how

---

<div class="post-metadata">

**Author:** ![wwillard](https://avatars.discourse-cdn.com/v4/letter/w/8e8cbc/32.png) [@wwillard](https://forum.liquibase.org/u/wwillard)\
**Post date:** [July 30, 2024, 1:21pm UTC](https://forum.liquibase.org/t/how-to-create-and-apply-diff-programmatically/9561/6 "2024-07-30T13:21:56Z")

</div>

This method should work. You would need to write the contents of the OutputStream to disk as a temp file, and then use that in your offline URL that you pass to the diff command.

> ```
> public static DatabaseSnapshot takeSnapshot(String url, Database database, String schemas, OutputStream stream) throws DatabaseException {
> 
> ```

```
    DatabaseSnapshot snapshot = null;
    try {
        CommandScope snapshotCommand = new CommandScope("snapshot");
        snapshotCommand.addArgumentValue(DbUrlConnectionArgumentsCommandStep.DATABASE_ARG, database);
        snapshotCommand.addArgumentValue(SnapshotCommandStep.SCHEMAS_ARG, schemas);
        snapshotCommand.setOutput(stream);
        CommandResults results = snapshotCommand.execute();
        snapshot = (DatabaseSnapshot) results.getResult("snapshot");
    } catch (Exception e) {
        String message = "";
        if (database.getConnection() instanceof OfflineConnection) {
            message =
                "The snapshot specified in the URL property cannot be accessed. Database-scoped checks cannot be completed.\n" +
                "Please check all database properties and credentials and try again.";
        } else {
            message =
                "The database target specified in the URL property cannot be accessed. Database-scoped checks cannot be completed.\n" +
                "Please check all database properties and credentials and try again.";
        }
        throw new DatabaseException(message, e);
    }
    return snapshot;
}

```
