Search in sources :

Example 1 with TenantOperation

use of org.folio.dbschema.TenantOperation in project raml-module-builder by folio-org.

the class TenantAPI method sqlFile.

/**
 * @param tenantExists false for initial installation, true for upgrading
 * @param tenantAttributes parameters like module version that may influence generated SQL
 * @param previousSchema schema to upgrade from, may be null if unknown and on initial install
 * @return the SQL commands . 0 is immediate script (create, disable), 1 (optional) is update schema script.
 * @throws NoSchemaJsonException when templates/db_scripts/schema.json doesn't exist
 * @throws TemplateException when processing templates/db_scripts/schema.json fails
 */
public String[] sqlFile(String tenantId, boolean tenantExists, TenantAttributes tenantAttributes, Schema previousSchema) throws IOException, TemplateException {
    InputStream tableInput = TenantAPI.class.getClassLoader().getResourceAsStream(getTablePath());
    if (tableInput == null) {
        log.info("Could not find templates/db_scripts/schema.json , " + " RMB will not run any scripts for " + tenantId);
        throw new NoSchemaJsonException();
    }
    TenantOperation op = TenantOperation.CREATE;
    String previousVersion = null;
    String newVersion = tenantAttributes == null ? null : tenantAttributes.getModuleTo();
    if (tenantExists) {
        op = TenantOperation.UPDATE;
        if (tenantAttributes != null) {
            previousVersion = tenantAttributes.getModuleFrom();
        }
    }
    SchemaMaker sMaker = new SchemaMaker(tenantId, PostgresClient.getModuleName(), op, previousVersion, newVersion);
    String tableInputStr = IOUtils.toString(tableInput, StandardCharsets.UTF_8);
    sMaker.setSchemaJson(tableInputStr);
    Schema schema = ObjectMapperTool.readValue(tableInputStr, Schema.class);
    sMaker.setSchema(schema);
    sMaker.setPreviousSchema(previousSchema);
    if (tenantAttributes != null && Boolean.TRUE.equals(tenantAttributes.getPurge())) {
        return new String[] { sMaker.generatePurge() };
    }
    if (tenantAttributes != null && tenantAttributes.getModuleFrom() != null && tenantAttributes.getModuleTo() == null) {
        return new String[] { "" };
    }
    String[] scripts = new String[] { sMaker.generateCreate(), sMaker.generateSchemas() };
    log.debug("GENERATED CREATE {}", scripts[0]);
    log.debug("GENERATED SCHEMAS {}", scripts[1]);
    return scripts;
}
Also used : InputStream(java.io.InputStream) Schema(org.folio.dbschema.Schema) TenantOperation(org.folio.dbschema.TenantOperation) SchemaMaker(org.folio.rest.persist.ddlgen.SchemaMaker)

Aggregations

InputStream (java.io.InputStream)1 Schema (org.folio.dbschema.Schema)1 TenantOperation (org.folio.dbschema.TenantOperation)1 SchemaMaker (org.folio.rest.persist.ddlgen.SchemaMaker)1