Nathan Q
Nathan Q

Reputation: 1902

Force liquibase changelog parameters to be set

I have in my liquibase changelogs also grant statements defined. I also need the user and host to be variable, so I defined it as following using changelog parameters:

    <changeSet id="exampleuser_grants_tbl_example" author="me">
        <preConditions onFail="MARK_RAN">
            <tableExists tableName="tbl_example" />
        </preConditions>
        <sql>
            GRANT INSERT, UPDATE, DELETE 
            ON `tbl_example` TO '${grants.user}'@'${grants.host}';
        </sql>
    </changeSet>

I can then define the parameters when running liquibase in command line:

-Dgrants.user=exampleuser -Dgrants.host=examplehost

But when I forget to define these parameters, it does not give any errors about this. Does MySQL not require a user to exist before using it in a GRANT statement? I could add a precondition to check if the user does exist, but that doesn't really solve the problem totally as the host parameter might not be set.

Is there any way to force a parameter to be set before using the changelog files?

EDIT

Ok so I fixed it thanks to the answer below as following:

At the top of the changelog file I added

<preConditions>
    <changeLogPropertyDefined property="grants.user" />
    <changeLogPropertyDefined property="grants.host" />
</preConditions>

Which now gives following error if for example the host parameter was not set:

1 preconditions failed

.../changelog-master.xml : Changelog property 'grants.host' was not set


As for the problem why my MySQL server did not generate any errors on creating a grant on a user named ${grants.user}@${grants.host}:

http://dev.mysql.com/doc/refman/5.7/en/sql-mode.html#sqlmode_no_auto_create_user

Upvotes: 3

Views: 2399

Answers (1)

dbf
dbf

Reputation: 6499

Have a look at changeLogPropertyDefined precondition here http://www.liquibase.org/documentation/preconditions.html. Also there is a customPrecondition tag so you could implement any custom logic in Java.

Upvotes: 3

Related Questions