Search in sources :

Example 1 with PostUpdateScripts

use of com.haulmont.cuba.core.sys.PostUpdateScripts in project cuba by cuba-platform.

the class DbUpdaterImpl method executeGroovyScript.

@Override
protected boolean executeGroovyScript(final ScriptResource file) {
    Binding bind = new Binding();
    bind.setProperty("ds", getDataSource());
    bind.setProperty("log", LoggerFactory.getLogger(String.format("%s$%s", DbUpdaterEngine.class.getName(), StringUtils.removeEndIgnoreCase(file.getName(), ".groovy"))));
    if (!StringUtils.endsWithIgnoreCase(file.getName(), "." + UPGRADE_GROOVY_EXTENSION)) {
        bind.setProperty("postUpdate", new PostUpdateScripts() {

            @Override
            public void add(Closure closure) {
                postUpdateScripts.put(closure, file);
                postUpdate.add(closure);
            }

            @Override
            public List<Closure> getUpdates() {
                return postUpdate.getUpdates();
            }
        });
    }
    try {
        scripting.evaluateGroovy(file.getContent(), bind, ScriptExecutionPolicy.DO_NOT_USE_COMPILE_CACHE);
    } catch (Exception e) {
        throw new RuntimeException(ERROR + "Error executing Groovy script " + file.name + "\n" + e.getMessage(), e);
    }
    return !postUpdateScripts.containsValue(file);
}
Also used : Binding(groovy.lang.Binding) PostUpdateScripts(com.haulmont.cuba.core.sys.PostUpdateScripts) Closure(groovy.lang.Closure) List(java.util.List) DbInitializationException(com.haulmont.cuba.core.sys.DbInitializationException)

Example 2 with PostUpdateScripts

use of com.haulmont.cuba.core.sys.PostUpdateScripts in project cuba by cuba-platform.

the class DbUpdaterEngine method executeGroovyScript.

protected boolean executeGroovyScript(ScriptResource file) {
    try {
        ClassLoader classLoader = getClass().getClassLoader();
        CompilerConfiguration cc = new CompilerConfiguration();
        cc.setRecompileGroovySource(true);
        Binding bind = new Binding();
        bind.setProperty("ds", getDataSource());
        bind.setProperty("log", LoggerFactory.getLogger(String.format("%s$%s", DbUpdaterEngine.class.getName(), StringUtils.removeEndIgnoreCase(file.getName(), ".groovy"))));
        if (!StringUtils.endsWithIgnoreCase(file.getName(), "." + UPGRADE_GROOVY_EXTENSION)) {
            bind.setProperty("postUpdate", new PostUpdateScripts() {

                @Override
                public void add(Closure closure) {
                    super.add(closure);
                    log.warn("Added post update action will be ignored for data store [{}]", storeNameToString(storeName));
                }
            });
        }
        GroovyShell shell = new GroovyShell(classLoader, bind, cc);
        Script script = shell.parse(file.getContent());
        script.run();
    } catch (Exception e) {
        throw new RuntimeException(String.format("%sError executing Groovy script %s\n%s", ERROR, file.name, e.getMessage()), e);
    }
    return true;
}
Also used : Binding(groovy.lang.Binding) Script(groovy.lang.Script) PostUpdateScripts(com.haulmont.cuba.core.sys.PostUpdateScripts) Closure(groovy.lang.Closure) CompilerConfiguration(org.codehaus.groovy.control.CompilerConfiguration) GroovyShell(groovy.lang.GroovyShell) DbInitializationException(com.haulmont.cuba.core.sys.DbInitializationException) IOException(java.io.IOException)

Example 3 with PostUpdateScripts

use of com.haulmont.cuba.core.sys.PostUpdateScripts in project cuba by cuba-platform.

the class DbUpdaterImpl method doUpdate.

@Override
protected void doUpdate() {
    postUpdate = new PostUpdateScripts();
    try {
        super.doUpdate();
    } catch (RuntimeException e) {
        postUpdateScripts.clear();
        throw e;
    }
    if (!postUpdate.getUpdates().isEmpty()) {
        log.info(String.format("Execute '%s' post update actions", postUpdate.getUpdates().size()));
        for (Closure closure : postUpdate.getUpdates()) {
            ScriptResource groovyFile = postUpdateScripts.remove(closure);
            if (groovyFile != null) {
                log.info("Execute post update from " + getScriptName(groovyFile));
            }
            closure.call();
            if (groovyFile != null && !postUpdateScripts.containsValue(groovyFile)) {
                log.info("All post update actions completed for " + getScriptName(groovyFile));
                markScript(getScriptName(groovyFile), false);
            }
        }
    }
}
Also used : PostUpdateScripts(com.haulmont.cuba.core.sys.PostUpdateScripts) Closure(groovy.lang.Closure)

Example 4 with PostUpdateScripts

use of com.haulmont.cuba.core.sys.PostUpdateScripts in project cuba by cuba-platform.

the class ServerDbUpdater method doUpdate.

@Override
protected void doUpdate() {
    postUpdate = new PostUpdateScripts();
    try {
        super.doUpdate();
    } catch (RuntimeException e) {
        postUpdateScripts.clear();
        throw e;
    }
    if (!postUpdate.getUpdates().isEmpty()) {
        log.info(String.format("Execute '%s' post update actions", postUpdate.getUpdates().size()));
        for (Closure closure : postUpdate.getUpdates()) {
            ScriptResource groovyFile = postUpdateScripts.remove(closure);
            if (groovyFile != null) {
                log.info("Execute post update from {}", getScriptName(groovyFile));
            }
            closure.call();
            if (groovyFile != null && !postUpdateScripts.containsValue(groovyFile)) {
                log.info("All post update actions completed for {}", getScriptName(groovyFile));
                markScript(getScriptName(groovyFile), false);
            }
        }
    }
}
Also used : PostUpdateScripts(com.haulmont.cuba.core.sys.PostUpdateScripts) Closure(groovy.lang.Closure)

Example 5 with PostUpdateScripts

use of com.haulmont.cuba.core.sys.PostUpdateScripts in project cuba by cuba-platform.

the class ServerDbUpdater method executeGroovyScript.

@Override
protected boolean executeGroovyScript(final ScriptResource file) {
    Binding bind = new Binding();
    bind.setProperty("ds", getDataSource());
    bind.setProperty("log", LoggerFactory.getLogger(String.format("%s$%s", DbUpdaterEngine.class.getName(), StringUtils.removeEndIgnoreCase(file.getName(), ".groovy"))));
    if (!StringUtils.endsWithIgnoreCase(file.getName(), "." + UPGRADE_GROOVY_EXTENSION)) {
        bind.setProperty("postUpdate", new PostUpdateScripts() {

            @Override
            public void add(Closure closure) {
                postUpdateScripts.put(closure, file);
                postUpdate.add(closure);
            }

            @Override
            public List<Closure> getUpdates() {
                return postUpdate.getUpdates();
            }
        });
    }
    try {
        scripting.evaluateGroovy(file.getContent(), bind, ScriptExecutionPolicy.DO_NOT_USE_COMPILE_CACHE);
    } catch (Exception e) {
        throw new RuntimeException(ERROR + "Error executing Groovy script " + file.name + "\n" + e.getMessage(), e);
    }
    return !postUpdateScripts.containsValue(file);
}
Also used : Binding(groovy.lang.Binding) PostUpdateScripts(com.haulmont.cuba.core.sys.PostUpdateScripts) Closure(groovy.lang.Closure) List(java.util.List)

Aggregations

PostUpdateScripts (com.haulmont.cuba.core.sys.PostUpdateScripts)5 Closure (groovy.lang.Closure)5 Binding (groovy.lang.Binding)3 DbInitializationException (com.haulmont.cuba.core.sys.DbInitializationException)2 List (java.util.List)2 GroovyShell (groovy.lang.GroovyShell)1 Script (groovy.lang.Script)1 IOException (java.io.IOException)1 CompilerConfiguration (org.codehaus.groovy.control.CompilerConfiguration)1