how to create a schema for defaultSchemaName

Hi Team,

As of now, I am creating database schema through some script in oracle.

But I want to create db schema through liquibase.  I used below changeset and I can able to create it.

<changeSet id=“create-schema” author=“admin”
        failOnError=“false” runAlways=“true”>
       
            SELECT COUNT(1) FROM dba_users
                WHERE USERNAME=‘test_schema’
       

        CREATE USER test_schema IDENTIFIED BY test;
   

But I  want the “databasechangelog” table inside test_schema. In my Spring boot if I give ‘test_schema’ as default schema it is complaining error

“spring.liquibase.default-schema = test_schema”

I get an error about not being able to create the databasechangelog table, because test_schema is not present yet.

I have gone through below post

https://forum.liquibase.org/topic/advice-on-how-to-create-a-schema-for-defaultschemaname

It is saying that we need to create default schema before liquibase startup.  But it is 10 years old topic.

Can you suggest whether it is still same scenario Or Is there a way to create default schema and keep databasechangelog inside test_schema.

Can anyone provide help here.

Thanks

Senthil

Hi Senthil,

Liquibase automation is based on layering changes over schemas that already exist, and the link you sent with Nathan’s reply is still spot on correct info:

… it is sort of a chicken and egg problem.  When starting liquibase, we don’t know what initial startup we need to do because we can’t check the table in the schema to know what we have already done…


Good to see you are searching the discussion topics. Liquibase automation and automation in general is best for use in repetitive tasks, which assumes a known set of schemas. 

Thanks,

Ronak

If you are using Spring Boot then you can use Pre-Liquibase module for this. It solves the chicken-and-egg problem in the sense that it allows you to execute some arbitrary SQL before Liquibase itself fires. Typically what you put in Pre-Liquibase SQL file would look something like this:

CREATE SCHEMA IF NOT EXISTS myschema;

but Pre-Liquibase even allows the SQL script to be dynamic:

CREATE SCHEMA IF NOT EXISTS ${spring.liquibase.default-schema};

More information at the website.

You can use Pre-Liquibase outside of a Spring Boot context, i.e. in pure Spring Framework. You cannot, however, use it without Spring as it relies on Spring for parsing your SQL file and executing it.