Example Changelogs: JSON Format

Liquibase supports JSON as a format for storing your Changelog files.

Uses

When using a JSON-based changelog file, you need to choose one of the following ways to audit your database and execute changes:

  • Pass it as an argument in the command line during runtime:
  • liquibase update --changelog-file=example-changelog.json
  • Specify it in the Liquibase properties file:
  • changelog-file: ../example-changelog.json

You can also include other related properties in the properties file, such as the searchPath, which specifies the directories and .jar files to search for changelog files. If you have multiple files, they can be separated with commas.

Note: For more information, see Create and Configure a liquibase.properties File.

Example

This example changelog contains changesets that:

  1. Create a new person table with columns id, firstname, lastname, and state
  2. Add a new username column to the person table
  3. Create a lookup table state using data from person

The example precondition requires the user making the deployment to be liquibase.

{
    "databaseChangeLog": [
      {
        "preConditions": [
          {
            "runningAs": {
              "username": "liquibase"
            }
          }
        ]
      },
      {
        "changeSet": {
          "id": "1",
          "author": "nvoxland",
          "changes": [
            {
              "createTable": {
                "tableName": "person",
                "columns": [
                  {
                    "column": {
                      "name": "id",
                      "type": "int",
                      "autoIncrement": true,
                      "constraints": {
                        "primaryKey": true,
                        "nullable": false
                      },
                      
                    }
                  },
                  {
                    "column": {
                      "name": "firstname",
                      "type": "varchar(50)"
                    }
                  },
                  {
                    "column": {
                      "name": "lastname",
                      "type": "varchar(50)",
                      "constraints": {
                        "nullable": false
                      },
                      
                    }
                  },
                  {
                    "column": {
                      "name": "state",
                      "type": "char(2)"
                    }
                  }
                ]
              }
            }
          ]
        }
      },
      {
        "changeSet": {
          "id": "2",
          "author": "nvoxland",
          "changes": [
            {
              "addColumn": {
                "tableName": "person",
                "columns": [
                  {
                    "column": {
                      "name": "username",
                      "type": "varchar(8)"
                    }
                  }
                ]
              }
            }
          ]
        }
      },
      {
        "changeSet": {
          "id": "3",
          "author": "nvoxland",
          "changes": [
            {
              "addLookupTable": {
                "existingTableName": "person",
                "existingColumnName": "state",
                "newTableName": "state",
                "newColumnName": "id",
                "newColumnDataType": "char(2)",
                
              }
            }
          ]
        }
      }
    ]
}

Related links