Vítor Brito
Vítor Brito

Reputation: 29

Creating liquibase control tables in different schema

I'm trying to set up liquibase to manage an existing mysql database.

Ideally, i'd like to create the control tables in a control schema and apply migrations to a default schema.

Configuration for liquibase goes as follows:

liquibase.classpath:/liquibase/changelog
liquibase.command.changelogFile: master.xml
liquibase.command.url: jdbc:mysql://host.docker.internal:3306/<default_schema>
liquibase.command.username: <root_user>
liquibase.command.password: <super_safe_password>

I also have a master changelog in xml pointing to a subdirectory with sql migrations:

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-4.1.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.1.xsd">
    <includeAll path="migrations/" relativeToChangelogFile="true"/>
</databaseChangeLog>

Finally I am using liquibase's docker container to avoid installing its command line tool across many machines, as i want to run commands on CI/CD pipelines.

I have a script to orchestrate it all:

#!/bin/bash

set -e

if [ "$#" -lt 3 ]; then
  echo "usage: liquibase <schema> <zone> <command>"
  echo "available zones: local"
  echo "available schemas: ..."
  exit 1
fi

SCHEMA=$1
ZONE=$2
COMMANDS=${@:3}

if [ ! -d "$PWD/schemas/$SCHEMA" ]; then
  echo "Schema not supported: $SCHEMA"
  exit 0
fi

CONF_DIR="$PWD/schemas/$SCHEMA/conf"

if [ ! -f "$CONF_DIR/liquibase.$ZONE.properties" ]; then
  echo "Zone not supported: $ZONE"
  exit 0
fi

MIGRATIONS_DIR="$PWD/schemas/$SCHEMA/changelog/migrations"
MASTER_CHANGELOG_DIR="$PWD/schemas/$SCHEMA/changelog"

docker run --rm \
  -e INSTALL_MYSQL=true \
  -v "$MASTER_CHANGELOG_DIR":/liquibase/changelog \
  -v "$CONF_DIR":/conf \
  -v "$MIGRATIONS_DIR":/liquibase/changelog/migrations \
  liquibase/liquibase \
  --defaultsFile=/conf/liquibase."$ZONE".properties \
  "$COMMANDS"

I've tried using two of the liquibase configurations that one would assume solves the problem with no luck:

liquibase.liquibaseSchemaName:<control_schema>
liquibase.command.defaultSchemaName:<default_schema>

It seems that the schema defined in defaultSchemaName overrides the previous config forcing to keep everything in the same schema.

Im using the latest liquibase version at the time:

Liquibase Version: 4.5.0
Liquibase Community 4.5.0 by Datical

Is this possible at all?

Upvotes: 0

Views: 1203

Answers (1)

Aditi
Aditi

Reputation: 367

There is no schema in MySQL database. But if you mean to have the control tables in different database, then you could use liquibase.liquibaseCatalogName: <db_name>to have all your control tables and liquibase.command.defaultCatalogName: <db_name> to have all other changes.

Upvotes: 1

Related Questions