# Multi Tenant Database Migration with Liquibase

**URL:** <https://forum.liquibase.org/t/multi-tenant-database-migration-with-liquibase/8552>\
**Category:** General Discussion\
**Created:** [August 17, 2023, 3:15pm UTC](https://forum.liquibase.org/t/multi-tenant-database-migration-with-liquibase/8552 "2023-08-17T15:15:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![lusabo](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.liquibase.org/lusabo/32/1056_2.png) [@lusabo](https://forum.liquibase.org/u/lusabo)\
**Post date:** [August 17, 2023, 3:15pm UTC](https://forum.liquibase.org/t/multi-tenant-database-migration-with-liquibase/8552/1 "2023-08-17T15:15:47Z")

</div>

Hey guys,

I’m developing a SaaS and I want to use the Schema-per-Tenant pattern, I read that Liquibase provided this support, I just don’t know how to **run the changelog in the newly created schema**. Does the the code below is the best approach? Could anyone help?

```java
 public void runLiquibase(String schema) throws Exception {
        Map<String, Object> scopeValues = new HashMap<>();
        Scope.child(scopeValues, () -> {
            try (Connection connection = dataSource.getConnection()) {
                Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
                database.setDefaultSchemaName(schema);
                Liquibase liquibase = new Liquibase("db/db.changelog-tenant.yaml", new ClassLoaderResourceAccessor(), database);
                liquibase.update(""); // This will update the database to the latest changelog version
            } catch (Exception e) {
                // Handle exceptions
                throw new RuntimeException("Error running Liquibase", e);
            }
        });
    }

```

---

<div class="post-metadata">

**Author:** ![lusabo](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.liquibase.org/lusabo/32/1056_2.png) [@lusabo](https://forum.liquibase.org/u/lusabo)\
**Post date:** [August 18, 2023, 10:29pm UTC](https://forum.liquibase.org/t/multi-tenant-database-migration-with-liquibase/8552/2 "2023-08-18T22:29:25Z")

</div>

I improved the code above with:

```java
    public void runLiquibase(String schema) {
        LOG.info(String.format("Running Liquibase for the new schema: %s", schema));
        try (Connection connection = dataSource.getConnection()) {
            Database database = DatabaseFactory.getInstance().findCorrectDatabaseImplementation(new JdbcConnection(connection));
            database.setDefaultSchemaName(schema);
            Liquibase liquibase = new Liquibase("db/db.changelog-tenant.yaml", new ClassLoaderResourceAccessor(), database);
            liquibase.update(new Contexts(), new LabelExpression());
            LOG.info(String.format("Liquibase update complete for schema: %s", schema));
        } catch (SQLException | LiquibaseException e) {
            LOG.error(String.format("Error running Liquibase for schema [%s]: %s", schema, e.getMessage()));
            throw new LiquibaseExecutionException("Error running Liquibase", e);
        }
    }

```
