Quantcast
Channel: Liquibase Forums
Viewing all articles
Browse latest Browse all 2993

Re : Maven + Hibernate

$
0
0
It's actually pretty simple to create some kind of diff yourself.
The following code uses only JPA stuff but some hibernate specific properties.

        String unitName = ... //JPA unit name
        String changeLogPath = ... //Path to the changelog master

        String driverClass = "org.h2.Driver";
        String oldJdbcUrl = "jdbc:h2:mem:old";
        String newJdbcUrl = "jdbc:h2:mem:new";
        String user = "test";
        String password = "test";

        Database newDatabase = new H2Database();
        newDatabase.setConnection(new JdbcConnection(DriverManager.getConnection(newJdbcUrl, user, password)));

        Database oldDatabase = new H2Database();
        oldDatabase.setConnection(new JdbcConnection(DriverManager.getConnection(oldJdbcUrl, user, password)));

        Liquibase oldLiquibase = new Liquibase(changeLogPath, new ClassLoaderResourceAccessor(), oldDatabase);
        oldLiquibase.update("");

        Liquibase newLiquibase = new Liquibase(null, null, newDatabase);
        newLiquibase.dropAll();

        // Schema export to new database
        Map<String, Object> properties = new HashMap<String, Object>();
        
        // Hibernate specific
        properties.put("hibernate.hbm2ddl.auto", "create");
        properties.put("hibernate.cache.region.factory_class", null);
        properties.put("hibernate.cache.use_query_cache", false);
        properties.put("hibernate.cache.use_second_level_cache", false);
        properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
        
        properties.put("javax.persistence.jtaDataSource", null);
        properties.put("javax.persistence.transactionType", "RESOURCE_LOCAL");
        properties.put("javax.persistence.jdbc.driver", driverClass);
        properties.put("javax.persistence.jdbc.user", user);
        properties.put("javax.persistence.jdbc.password", password);
        properties.put("javax.persistence.jdbc.url", newJdbcUrl);
        EntityManagerFactory emf = Persistence.createEntityManagerFactory(unitName, properties);

        // Diff
        DatabaseSnapshot oldSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(oldDatabase.getDefaultSchema(), oldDatabase, new SnapshotControl(oldDatabase));
        DatabaseSnapshot newSnapshot = SnapshotGeneratorFactory.getInstance().createSnapshot(newDatabase.getDefaultSchema(), newDatabase, new SnapshotControl(newDatabase));

        CompareControl compareControl = new CompareControl(
            new CompareControl.SchemaComparison[] { new CompareControl.SchemaComparison(new CatalogAndSchema(
                newDatabase.getDefaultCatalogName(), newDatabase.getDefaultSchemaName()), new CatalogAndSchema(
                oldDatabase.getDefaultCatalogName(), oldDatabase.getDefaultSchemaName())) },

            oldSnapshot.getSnapshotControl().getTypesToInclude());

        DiffResult diffResult = oldLiquibase.diff(newDatabase, oldDatabase, compareControl);
        new DiffToChangeLog(diffResult, new DiffOutputControl(false, false, true)){

            @Override
            public List<ChangeSet> generateChangeSets() {
                List<ChangeSet> changeSets = super.generateChangeSets();
                List<ChangeSet> newChangeSets = new ArrayList<ChangeSet>;
                ChangeSet changeSet = new ChangeSet(generateId(), getChangeSetAuthor(), false, false,
                                                    null, null, null, ObjectQuotingStrategy.QUOTE_ALL_OBJECTS, null);

                newChangeSets.add(changeSet);

for(ChangeSet set : changeSets) { for(Change change : set.getChanges()) {
                        boolean include = true;

                        if(change instanceof CreateIndexChange) { CreateIndexChange index = (CreateIndexChange) change; for(Change existingChange : changeSet.getChanges()) { if(existingChange instanceof AddUniqueConstraintChange) { AddUniqueConstraintChange unique = (AddUniqueConstraintChange) existingChange; boolean catalogEqual = unique.getCatalogName() == null ? index.getCatalogName() == null : unique.getCatalogName().equals(index.getCatalogName()); boolean schemaEqual = unique.getSchemaName() == null ? index.getSchemaName() == null : unique.getSchemaName().equals(index.getSchemaName()); if(catalogEqual && schemaEqual && unique.getTableName().equals(index.getTableName())) { StringBuilder sb = new StringBuilder(); for(ColumnConfig column : index.getColumns()) { sb.append(column.getName()).append(','); } sb.setLength(sb.length() - 1); if(unique.getColumnNames().equals(sb.toString())) { // TODO: instead of remove, add a precondition ifIndexDoesNotExist include = false; } break; } } } }

                        if(include) {
                            //create only a single changeset
                            changeSet.addChange(change);

                            //alternative, many changesets
                            //ChangeSet newChangeSet = new ChangeSet(generateId(), getChangeSetAuthor(), false, false, null, null, null, ObjectQuotingStrategy.QUOTE_ALL_OBJECTS, null);
                            //newChangeSet.addChange(change);
                            //newChangeSets.add(newChangeSet);
                        }
 } } return Arrays.asList(changeSet); } }.print(System.out); emf.close(); oldDatabase.close(); newDatabase.close();


Viewing all articles
Browse latest Browse all 2993

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>