[ 
https://issues.apache.org/jira/browse/ISIS-1471?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15383980#comment-15383980
 ] 

Dan Haywood commented on ISIS-1471:
-----------------------------------

Timothy wrote:

Hello Kambiz, Hello Jeroen,

My colleague pointed me to your mails at the mailing list as we were also 
interested in that topic. In datanucleus documentation see [1] I found a hook 
that can be used. Please note that I implemented that yesterday, so it is not 
well tested but the application starts and all integration tests are green.

In persistor_datanucleus.properties add this:
isis.persistor.datanucleus.impl.javax.jdo.PersistenceManagerFactoryClass=com.example.FlywayJdoPersistenceManagerFactory

and turn off the auto generation:
isis.persistor.datanucleus.impl.datanucleus.schema.autoCreateAll=false

Also I turned off validation because I had issues with LONGVARBINARY as HSQL 
replaced it to VARBINARY which datanucleus was not very happy about that ;)
isis.persistor.datanucleus.impl.datanucleus.schema.validateAll=false
isis.persistor.datanucleus.impl.datanucleus.schema.validateTables=false
isis.persistor.datanucleus.impl.datanucleus.schema.validateColumns=false
isis.persistor.datanucleus.impl.datanucleus.schema.validateConstraints=false

Here the implementation of the class:

import org.datanucleus.api.jdo.JDOPersistenceManagerFactory;
import org.datanucleus.metadata.PersistenceUnitMetaData;
import org.flywaydb.core.Flyway;

public class FlywayJdoPersistenceManagerFactory extends 
JDOPersistenceManagerFactory {

    public FlywayJdoPersistenceManagerFactory() {
        super();
    }

    public FlywayJdoPersistenceManagerFactory(final PersistenceUnitMetaData 
pumd, final Map overrideProps) {
        super(pumd, overrideProps);
        migrateDatabase();
    }

    public FlywayJdoPersistenceManagerFactory(final Map props) {
        super(props);
        migrateDatabase();
    }

    private void migrateDatabase() {

        //String driverName = 
(String)this.getProperties().get("javax.jdo.option.ConnectionDriverName"); 
Flyway uses auto detection...
//        String url = 
(String)this.getProperties().get("javax.jdo.option.ConnectionURL"); don't use, 
as the propertie names are changed during initialization of superclass
//        String userName = 
(String)this.getProperties().get("javax.jdo.option.ConnectionUserName");
//        String password = 
(String)this.getProperties().get("javax.jdo.option.ConnectionPassword");

        try {
            Flyway flyway = new Flyway();
            flyway.setDataSource(this.getConnectionURL(), 
this.getConnectionUserName(), this.getConnectionPassword());
            flyway.migrate();

        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Hope that helps you!

Regards Timothy


> Integrate Flyway or Liquibase or something similar
> --------------------------------------------------
>
>                 Key: ISIS-1471
>                 URL: https://issues.apache.org/jira/browse/ISIS-1471
>             Project: Isis
>          Issue Type: New Feature
>          Components: Core
>    Affects Versions: 1.13.0
>            Reporter: Dan Haywood
>            Assignee: Dan Haywood
>             Fix For: 1.14.0
>
>
> from Kambiz:
> Hi,
> in our non-Isis projects, we use FlyWay [1] for DB migrations and I
> would like to integrate it into our Isis workflow. The simplest path to
> do so would be a DomainService with a PostConstruct annotated init
> method:
> @PostConstruct
> public void init(final Map<String, String> properties) {
>     Flyway flyway = new Flyway();
>     // Point it to the database
>     String jdbcUrl = 
> properties.get("isis.persistor.datanucleus.impl.javax.jdo.option.ConnectionURL");
>     String user = 
> properties.get("isis.persistor.datanucleus.impl.javax.jdo.option.ConnectionUserName");
>     String password = 
> properties.get("isis.persistor.datanucleus.impl.javax.jdo.option.ConnectionPassword");
>     flyway.setDataSource(jdbcUrl, user, password);
>     flyway.setLocations("classpath:db/migrations");
>     // Start the migration
>     flyway.migrate();
> }
> but this isn't a viable solution, as IsisSessionFactoryBuilder's
> buildSessionFactory() method initialises the DataNucleus (DN)
> PersistenceSessionFactory before the services are constructed [2].
> So DN has already found the mismatch between the JDO annotations and the
> database before we enter the init method of our DB migration
> bootstrap/seed service.
> I could contribute a patch, if someone could hint on the preferred way
> of implementing the functionality.
> Thank you
> Kambiz
> [1] https://flywaydb.org/
> [2] 
> https://github.com/apache/isis/blob/master/core/runtime/src/main/java/org/apache/isis/core/runtime/system/session/IsisSessionFactoryBuilder.java#L184



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to