From bleny@users.nuiton.org Tue May 20 18:34:38 2014 From: bleny@users.nuiton.org To: topia-commits@list.nuiton.org Subject: [Topia-commits] r3115 - trunk/topia-service-liquibase/src/main/java/org/nuiton/topia/liquibase Date: Tue, 20 May 2014 18:34:38 +0200 Message-ID: <20140520163438.95994180A07@goh.codelutin.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="===============7974604251074009160==" --===============7974604251074009160== Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Author: bleny Date: 2014-05-20 18:34:38 +0200 (Tue, 20 May 2014) New Revision: 3115 Url: http://forge.nuiton.org/projects/topia/repository/revisions/3115 Log: refs #3204 make liquibase service create schema if needed Modified: trunk/topia-service-liquibase/src/main/java/org/nuiton/topia/liquibase/Top= iaLiquibaseServiceImpl.java Modified: trunk/topia-service-liquibase/src/main/java/org/nuiton/topia/liquib= ase/TopiaLiquibaseServiceImpl.java =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D --- trunk/topia-service-liquibase/src/main/java/org/nuiton/topia/liquibase/To= piaLiquibaseServiceImpl.java 2014-05-20 09:48:56 UTC (rev 3114) +++ trunk/topia-service-liquibase/src/main/java/org/nuiton/topia/liquibase/To= piaLiquibaseServiceImpl.java 2014-05-20 16:34:38 UTC (rev 3115) @@ -1,5 +1,6 @@ package org.nuiton.topia.liquibase; =20 +import liquibase.Contexts; import liquibase.Liquibase; import liquibase.database.Database; import liquibase.database.DatabaseFactory; @@ -30,6 +31,8 @@ =20 private static final Log log =3D LogFactory.getLog(TopiaLiquibaseService= Impl.class); =20 + protected Contexts contexts =3D new Contexts(""); + /** * Place to find the changelog, default name has been fixed according * to best pract= ices. @@ -60,8 +63,16 @@ =20 Liquibase liquibase =3D getLiquibase(database, resourceAccessor); =20 - update(liquibase); + if (isSchemaEmpty(liquibase, topiaApplicationContext)) { =20 + createSchema(liquibase, topiaApplicationContext); + + } else { + + migrateSchema(liquibase, topiaApplicationContext); + + } + } catch (LiquibaseException e) { throw new TopiaException("unable to init liquibase", e); } finally { @@ -76,10 +87,40 @@ =20 } =20 - protected void update(Liquibase liquibase) throws LiquibaseException { - liquibase.update(""); // "" mean all contexts + /** + * Check if schema exists. If true is returned, we won't use any migrati= ons and just create shema + * according to model. If false, a schema already exists and an migratio= n should be tried. + */ + protected boolean isSchemaEmpty(Liquibase liquibase, TopiaApplicationCon= text topiaApplicationContext) { + + boolean schemaEmpty =3D topiaApplicationContext.isSchemaEmpty(); + + return schemaEmpty; + } =20 + protected void createSchema(Liquibase liquibase, TopiaApplicationContext= topiaApplicationContext) throws LiquibaseException { + + if (log.isInfoEnabled()) { + log.info("schema is empty, ignore migrations and let topia creat= e schema"); + } + + topiaApplicationContext.createSchema(); + + if (log.isInfoEnabled()) { + log.info("sync changelog"); + } + + liquibase.changeLogSync(contexts); + + } + + protected void migrateSchema(Liquibase liquibase, TopiaApplicationContex= t topiaApplicationContext) throws LiquibaseException { + + liquibase.update(contexts); // "" mean all contexts + + } + protected Liquibase getLiquibase(Database database, ResourceAccessor res= ourceAccessor) throws LiquibaseException { Liquibase liquibase =3D new Liquibase(changeLogPath, resourceAccesso= r, database); return liquibase; --===============7974604251074009160==--