Skip to content

Commit

Permalink
Move liberty oauth tables to the FHIR_OAUTH schema and gen by default
Browse files Browse the repository at this point in the history
Previously, a user needed to create these tables themselves if they
wanted to use liberty as an OAuth 2.0 provider with a databaseStore
(e.g. to dynamically manage OAuth 2.0 clients).

Now these tables will be created/updated as part of the
fhir-persistence-schema create-schema/update-schema actions.

Signed-off-by: Lee Surprenant <lmsurpre@us.ibm.com>
  • Loading branch information
lmsurpre committed Apr 21, 2020
1 parent 6625b99 commit e674bde
Show file tree
Hide file tree
Showing 14 changed files with 247 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -396,12 +396,9 @@ public void grantSequencePrivileges(String schemaName, String objectName, Collec


/**
* Create FHIR data and admin schemas
* Create a database schema
*
* @param schemaName
* @param adminSchemaName
*/
public void createFhirSchemas(String schemaName, String adminSchemaName);


public void createSchema(String schemaName);
}
Original file line number Diff line number Diff line change
Expand Up @@ -432,19 +432,13 @@ public boolean checkCompatibility(String adminSchema) {
}

@Override
public void createFhirSchemas(String schemaName, String adminSchemaName) {
public void createSchema(String schemaName) {
try {
String ddl = "CREATE SCHEMA " + schemaName;
runStatement(ddl);
} catch (DuplicateNameException e) {
logger.log(Level.WARNING, "The schema '" + schemaName + "' already exists; proceed with caution.");
}
try {
String ddl = "CREATE SCHEMA " + adminSchemaName;
runStatement(ddl);
} catch (DuplicateNameException e) {
logger.log(Level.WARNING, "The schema '" + adminSchemaName + "' already exists; proceed with caution.");
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,9 @@ protected List<String> prefixTenantColumn(String tenantColumnName, List<String>
}

@Override
public void createFhirSchemas(String schemaName, String adminSchemaName) {
public void createSchema(String schemaName) {
String ddl = "CREATE SCHEMA " + schemaName;
runStatement(ddl);
ddl = "CREATE SCHEMA " + adminSchemaName;
runStatement(ddl);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
import com.ibm.fhir.database.utils.api.IDatabaseTypeAdapter;

/**
* Blob Column
* Binary Large OBject (BLOB) Column
*/
public class BlobColumn extends ColumnBase {

private final long size;

private final int inlineSize;

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/*
* (C) Copyright IBM Corp. 2020
*
* SPDX-License-Identifier: Apache-2.0
*/

package com.ibm.fhir.database.utils.model;

import com.ibm.fhir.database.utils.api.IDatabaseTypeAdapter;

/**
* Character Large OBject (CLOB) Column
*/
public class ClobColumn extends ColumnBase {
public ClobColumn(String name, boolean nullable) {
super(name, nullable);
}

public ClobColumn(String name, boolean nullable, String defaultVal) {
super(name, nullable, defaultVal);
}

@Override
public String getTypeInfo(IDatabaseTypeAdapter adapter) {
return "CLOB";
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (C) Copyright IBM Corp. 2019
* (C) Copyright IBM Corp. 2019, 2020
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -12,13 +12,16 @@
* An immutable definition of a column in a table
*/
public abstract class ColumnBase {

// Name of the column
private final String name;

// Does the column allow null values
private final boolean nullable;

// The default value for the column (usually null)
private final String defaultVal;

/**
* Protected constructor - for use by subclasses
* @param name
Expand All @@ -27,6 +30,19 @@ public abstract class ColumnBase {
protected ColumnBase(String name, boolean nullable) {
this.name = name;
this.nullable = nullable;
this.defaultVal = null;
}

/**
* Protected constructor - for use by subclasses
* @param name
* @param nullable
* @param defaultVal
*/
protected ColumnBase(String name, boolean nullable, String defaultVal) {
this.name = name;
this.nullable = nullable;
this.defaultVal = defaultVal;
}

/**
Expand All @@ -45,6 +61,14 @@ public boolean isNullable() {
return nullable;
}

/**
* Getter for the default value of this column
* @return possibly null
*/
public String getDefaultVal() {
return defaultVal;
}

/**
* Get the type info string
* @param adapter
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (C) Copyright IBM Corp. 2019
* (C) Copyright IBM Corp. 2019, 2020
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -16,19 +16,20 @@ public class ColumnDef implements Comparable<ColumnDef> {
private long size;
private Integer precision;
private int inlineSize;

private String defaultVal;

/**
* Public constructor
* @param name
*/
public ColumnDef(String name) {
this.name = name;
}

public String getName() {
return name;
}

public boolean isNullable() {
return nullable;
}
Expand All @@ -53,13 +54,22 @@ public Integer getPrecision() {
public void setPrecision(int precision) {
this.precision = precision;
}
public String getDefaultVal() {
return defaultVal;
}
/**
* @implNote Currently only works for CLOB columns
*/
public void setDefaultVal(String defaultVal) {
this.defaultVal = defaultVal;
}

@Override
public int compareTo(ColumnDef that) {
return this.name.compareTo(that.name);
}
@Override

@Override
public boolean equals(Object other) {
if (other instanceof ColumnDef) {
ColumnDef that = (ColumnDef)other;
Expand All @@ -69,8 +79,8 @@ public boolean equals(Object other) {
return false;
}
}
@Override

@Override
public int hashCode() {
return this.name.hashCode();
}
Expand All @@ -82,5 +92,4 @@ public int getInlineSize() {
public void setInlineSize(int inlineSize) {
this.inlineSize = inlineSize;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ public List<ColumnBase> buildColumns() {
case BLOB:
column = new BlobColumn(cd.getName(), cd.getSize(), cd.getInlineSize(), cd.isNullable());
break;
case CLOB:
column = new ClobColumn(cd.getName(), cd.isNullable(), cd.getDefaultVal());
break;
default:
throw new IllegalStateException("Unsupported column type: " + cd.getColumnType().name());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (C) Copyright IBM Corp. 2019
* (C) Copyright IBM Corp. 2019, 2020
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -18,5 +18,6 @@ public enum ColumnType {
DECIMAL,
DOUBLE,
TIMESTAMP,
BLOB
BLOB,
CLOB
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* (C) Copyright IBM Corp. 2019
* (C) Copyright IBM Corp. 2019, 2020
*
* SPDX-License-Identifier: Apache-2.0
*/
Expand All @@ -10,7 +10,7 @@
* Basic set of privileges that can be granted to a database object
*/
public enum Privilege {

ALL,
SELECT,
INSERT,
UPDATE,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,19 @@ public Builder addBlobColumn(String columnName, long size, int inlineSize, boole
return this;
}

public Builder addClobColumn(String columnName, boolean nullable, String defaultVal) {
ColumnDef cd = new ColumnDef(columnName);
if (columns.contains(cd)) {
throw new IllegalArgumentException("Duplicate column: " + columnName);
}

cd.setNullable(nullable);
cd.setColumnType(ColumnType.CLOB);
cd.setDefaultVal(defaultVal);
columns.add(cd);
return this;
}

/**
* Set one of the columns to be the identity column for the table
* @param constraintName
Expand Down Expand Up @@ -589,6 +602,9 @@ protected List<ColumnBase> buildColumns() {
case BLOB:
column = new BlobColumn(cd.getName(), cd.getSize(), cd.getInlineSize(), cd.isNullable());
break;
case CLOB:
column = new ClobColumn(cd.getName(), cd.isNullable(), cd.getDefaultVal());
break;
default:
throw new IllegalStateException("Unsupported column type: " + cd.getColumnType().name());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
import com.ibm.fhir.schema.control.FhirSchemaConstants;
import com.ibm.fhir.schema.control.FhirSchemaGenerator;
import com.ibm.fhir.schema.control.GetResourceTypeList;
import com.ibm.fhir.schema.control.OAuthSchemaGenerator;
import com.ibm.fhir.schema.control.PopulateResourceTypes;
import com.ibm.fhir.schema.model.DbType;
import com.ibm.fhir.schema.model.ResourceType;
Expand All @@ -69,7 +70,7 @@
import com.ibm.fhir.task.core.service.TaskService;

/**
* Utility app to connect to a Db2 database and create/update the FHIR schema.
* Utility app to connect to a database and create/update the IBM FHIR Server schema.
* The DDL processing is idempotent, with only the necessary changes applied.
* <br>
* This utility also includes an option to exercise the tenant partitioning code.
Expand All @@ -92,6 +93,9 @@ public class Main {
// The schema used for administration of tenants
private String adminSchemaName = "FHIR_ADMIN";

// The schema used for administration of OAuth 2.0 clients
private String oauthSchemaName = "FHIR_OAUTH";

// Arguments requesting we drop the objects from the schema
private boolean dropSchema = false;
private boolean dropAdmin = false;
Expand Down Expand Up @@ -520,12 +524,16 @@ private IDatabaseAdapter getDbAdapter(IConnectionProvider connectionProvider){
*/
protected void updateSchema() {

// Build/update the tables as well as the stored procedures
// Build/update the FHIR-related tables as well as the stored procedures
FhirSchemaGenerator gen = new FhirSchemaGenerator(adminSchemaName, schemaName);
PhysicalDataModel pdm = new PhysicalDataModel();
gen.buildSchema(pdm);
gen.buildProcedures(pdm);

// Build/update the Liberty OAuth-related tables
OAuthSchemaGenerator oauthSchemaGenerator = new OAuthSchemaGenerator(oauthSchemaName);
oauthSchemaGenerator.buildOAuthSchema(pdm);

// The objects are applied in parallel, which relies on each object
// expressing its dependencies correctly. Changes are only applied
// if their version is greater than the current version.
Expand All @@ -545,7 +553,9 @@ protected void createFhirSchemas() {
try {
JdbcTarget target = new JdbcTarget(c);
IDatabaseAdapter adapter = getDbAdapter(target);
adapter.createFhirSchemas(schemaName, adminSchemaName);
adapter.createSchema(schemaName);
adapter.createSchema(adminSchemaName);
adapter.createSchema(oauthSchemaName);
} catch (Exception x) {
c.rollback();
throw x;
Expand Down
Loading

0 comments on commit e674bde

Please sign in to comment.