Matthew Layton
Matthew Layton

Reputation: 42390

Generating Liquibase change logs from Corda state schemas

In Corda we create state schemas for queryable states; for example:

object MyStateSchema {

    object MyStateSchemaV1 : MappedSchema(
        schemaFamily = MyStateSchema.javaClass,
        version = 1,
        mappedTypes = listOf(MyStateEntity::class.java)
    )

    @Entity
    @Table(name = "my_states")
    class MyStateEntity(
        @Column(name = "linear_id", nullable = false)
        val linearId: UUID = UUID.randomUUID(),

        @Column(name = "external_id", nullable = true)
        val externalId: String? = null,

        @Column(name = "identity", nullable = false)
        val identity: AbstractParty = NULL_PARTY,

        @Column(name = "value", nullable = false)
        val value: String = ""
    ) : PersistentState()
}

Notably, MyStateEntity is annotated with JPA annotations. From this we need to generate database agnostic scripts to create and update the database schema; for example:

<?xml version="1.1" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.5.xsd">
    <changeSet author="Me" id="create-my_states">
        <createTable tableName="my_states">
            <column name="output_index" type="INT">
                <constraints nullable="false"/>
            </column>
            <column name="transaction_id" type="NVARCHAR(64)">
                <constraints nullable="false"/>
            </column>
            <column name="linear_id" type="uuid">
                <constraints nullable="false"/>
            </column>
            <column name="external_id" type="NVARCHAR(255)" />
            <column name="identity" type="NVARCHAR(255)">
                <constraints nullable="false"/>
            </column>
            <column name="value" type="NVARCHAR(255)">
                <constraints nullable="false"/>
            </column>
        </createTable>
        <addPrimaryKey columnNames="output_index, transaction_id"
                       constraintName="PK_my_states"
                       tableName="my_states"/>
    </changeSet>
</databaseChangeLog>

Currently I'm writing the change logs by hand, and it's rather tiresome. I'm sure there must be a better way. I tried adding some dependencies to gradle so that I could run the following command (sadly it didn't work):

./gradlew generateChangeLog

Can liquibase generate these scripts automatically?

Upvotes: 3

Views: 290

Answers (2)

opticyclic
opticyclic

Reputation: 8154

Like you say, writing it by hand is tiresome.

However, if you start the node in devMode while developing, hibernate will generate the schema for you:

java -jar corda.jar --allow-hibernate-to-manage-app-schema

You can then download the liquibase distribution and get liquibase to generate the schema scripts automatically for you:

./liquibase --driver=org.h2.Driver --changeLogFile=schema.xml --url=jdbc:h2:~/Projects/my-corda-app/build/nodes/ClientName/persistence --username=sa --includeObjects=table:CUSTOM_STATE,table:SECOND_STATE generateChangeLog

This isn't perfect as it almost certainly wont have the right author and id in it, however, it saves you writing it all from scratch.

I opened an issue with Corda to try to get them to do it automatically.

https://github.com/corda/corda/issues/6813

Upvotes: 2

Peter Li
Peter Li

Reputation: 1032

For a brand new database (meaning first time deploy the nodes), I think manual generation would be the best approach because these xml files are used to create the tables during node deployment. Also, the schemas are custom schemas which related to your specific CorDapp. So I would think automation means something needs to be able to read the schema files in the CorDapp contract folders. Yet, we don't have anything tool like that yet.

I also saw some posts discussing that if you have an existing database and want to have these xml files generated automatically, I think this is when you configure a liquidbase.properties file and use the generateChangeLog command.

Upvotes: 0

Related Questions