use of org.folio.dbschema.Schema in project raml-module-builder by folio-org.
the class TenantAPIIT method previousSchemaSqlExistsTrue2.
@Test
public void previousSchemaSqlExistsTrue2(TestContext context) {
TenantAPI tenantAPI = new TenantAPI();
String tenantId = TenantTool.tenantId(okapiHeaders);
Async async = context.async();
// only run create.ftl .. This makes previousSchema to find nothing on 2nd invocation
tenantAPI.sqlFile(vertx.getOrCreateContext(), tenantId, null, false).compose(files -> tenantAPI.postgresClient(vertx.getOrCreateContext()).runSQLFile(files[0], true)).compose(x -> tenantAPI.previousSchema(vertx.getOrCreateContext(), tenantId, true).onComplete(context.asyncAssertSuccess(schema -> {
context.assertNull(schema);
async.complete();
})));
async.await();
}
use of org.folio.dbschema.Schema in project raml-module-builder by folio-org.
the class DbSchemaUtilsTest method setupSchema.
@Before
public void setupSchema() {
Schema schema = new Schema();
table = new Table();
table.setTableName("users");
table.setFullTextIndex(Arrays.asList(newIndex("name")));
table.setGinIndex(Arrays.asList(newIndex("name")));
table.setIndex(Arrays.asList(newIndex("name")));
table.setUniqueIndex(Arrays.asList(newIndex("email")));
table.setLikeIndex(Arrays.asList(newIndex("address")));
schema.setTables(Arrays.asList(table));
}
use of org.folio.dbschema.Schema in project raml-module-builder by folio-org.
the class AdminAPI method putAdminPostgresCreateIndexes.
@Validate
@Override
public void putAdminPostgresCreateIndexes(Map<String, String> okapiHeaders, Handler<AsyncResult<Response>> asyncResultHandler, Context vertxContext) {
try {
String tenantId = TenantTool.tenantId(okapiHeaders);
SchemaMaker sMaker = new SchemaMaker(tenantId, PostgresClient.getModuleName(), TenantOperation.CREATE, null, null);
InputStream tableInput = AdminAPI.class.getClassLoader().getResourceAsStream(TenantAPI.TABLE_JSON);
String tableInputStr = null;
if (tableInput != null) {
tableInputStr = IOUtils.toString(tableInput, "UTF8");
Schema schema = ObjectMapperTool.getMapper().readValue(tableInputStr, Schema.class);
sMaker.setSchema(schema);
}
String sqlFile = sMaker.generateIndexesOnly();
PostgresClient.getInstance(vertxContext.owner()).runSQLFile(sqlFile, true, reply -> {
try {
StringBuilder res = new StringBuilder();
if (reply.succeeded()) {
boolean failuresExist = false;
if (reply.result().size() > 0) {
failuresExist = true;
}
res.append(new JsonArray(reply.result()).encodePrettily());
if (failuresExist) {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PutAdminPostgresCreateIndexesResponse.respond400WithTextPlain(res.toString())));
} else {
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PutAdminPostgresCreateIndexesResponse.respond204()));
}
} else {
log.error(reply.cause().getMessage(), reply.cause());
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PutAdminPostgresCreateIndexesResponse.respond500WithTextPlain(reply.cause().getMessage())));
}
} catch (Exception e) {
log.error(e.getMessage(), e);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PutAdminPostgresCreateIndexesResponse.respond500WithTextPlain(e.getMessage())));
}
});
} catch (Exception e) {
log.error(e.getMessage(), e);
asyncResultHandler.handle(io.vertx.core.Future.succeededFuture(PutAdminPostgresCreateIndexesResponse.respond500WithTextPlain(e.getMessage())));
}
}
use of org.folio.dbschema.Schema 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;
}
use of org.folio.dbschema.Schema in project raml-module-builder by folio-org.
the class SchemaMakerIT method triggerExists.
private boolean triggerExists(TestContext context, String name) {
AtomicBoolean exists = new AtomicBoolean();
Async async = context.async();
PostgresClient postgresClient = PostgresClient.getInstance(vertx, tenant);
postgresClient.selectSingle("SELECT count(*) FROM pg_trigger " + "WHERE tgrelid = '" + schema + ".test_tenantapi'::regclass AND tgname='" + name + "'", context.asyncAssertSuccess(count -> {
exists.set(count.getInteger(0) == 1);
async.complete();
}));
async.await(5000);
return exists.get();
}
Aggregations