From 81b3ef314459e1631d56626560214af4fd0768ec Mon Sep 17 00:00:00 2001 From: Jeff Burke Date: Fri, 6 Sep 2024 08:27:03 -0700 Subject: [PATCH] CADC-13694 add api_created boolean to tap_schema tables --- cadc-tap-schema/build.gradle | 4 ++-- .../ca/nrc/cadc/tap/schema/TapSchemaDAO.java | 21 +++++++++++-------- .../nrc/cadc/vosi/actions/DeleteAction.java | 10 +++++++-- .../ca/nrc/cadc/vosi/actions/PutAction.java | 2 ++ .../postgresql/tap_schema.upgrade-1.2.1.sql | 5 +++++ .../resources/postgresql/tap_schema11.sql | 3 +++ 6 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 cadc-tap-schema/src/main/resources/postgresql/tap_schema.upgrade-1.2.1.sql diff --git a/cadc-tap-schema/build.gradle b/cadc-tap-schema/build.gradle index d290c8b8..c7dcca6c 100644 --- a/cadc-tap-schema/build.gradle +++ b/cadc-tap-schema/build.gradle @@ -16,7 +16,7 @@ sourceCompatibility = 1.8 group = 'org.opencadc' -version = '1.1.33' +version = '1.2.0' description = 'OpenCADC TAP-1.1 tap schema server library' def git_url = 'https://github.com/opencadc/tap' @@ -34,7 +34,7 @@ dependencies { compile 'org.opencadc:cadc-cdp:[1.2.3,2.0)' compile 'org.opencadc:cadc-gms:[1.0,2.0)' compile 'org.opencadc:cadc-rest:[1.3.1,2.0)' - compile 'org.opencadc:cadc-tap:[1.1,2.0)' + compile 'org.opencadc:cadc-tap:[1.1.17,2.0)' compile 'uk.ac.starlink:jcdf:[1.2.3,2.0)' compile 'uk.ac.starlink:stil:[4.0,5.0)' diff --git a/cadc-tap-schema/src/main/java/ca/nrc/cadc/tap/schema/TapSchemaDAO.java b/cadc-tap-schema/src/main/java/ca/nrc/cadc/tap/schema/TapSchemaDAO.java index 8ad133a8..44f3d58d 100644 --- a/cadc-tap-schema/src/main/java/ca/nrc/cadc/tap/schema/TapSchemaDAO.java +++ b/cadc-tap-schema/src/main/java/ca/nrc/cadc/tap/schema/TapSchemaDAO.java @@ -71,18 +71,12 @@ import ca.nrc.cadc.auth.AuthenticationUtil; import ca.nrc.cadc.auth.IdentityManager; -import ca.nrc.cadc.cred.client.CredUtil; import ca.nrc.cadc.db.DatabaseTransactionManager; import ca.nrc.cadc.net.ResourceNotFoundException; import ca.nrc.cadc.profiler.Profiler; -import ca.nrc.cadc.reg.Standards; -import ca.nrc.cadc.reg.client.LocalAuthority; import ca.nrc.cadc.uws.Job; import java.net.URI; -import java.security.AccessControlException; -import java.security.cert.CertificateExpiredException; -import java.security.cert.CertificateNotYetValidException; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; @@ -96,9 +90,7 @@ import javax.security.auth.Subject; import javax.sql.DataSource; import org.apache.log4j.Logger; -import org.opencadc.gms.GroupClient; import org.opencadc.gms.GroupURI; -import org.opencadc.gms.GroupUtil; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.PreparedStatementCreator; import org.springframework.jdbc.core.RowMapper; @@ -152,6 +144,9 @@ public class TapSchemaDAO { protected static String readWriteCol = "read_write_group"; private static String[] accessControlCols = new String[] { ownerCol, readAnonCol, readOnlyCol, readWriteCol }; + // api_created is in the tables schema but not exposed as a tap_schema column + private static String apiCreated = "api_created"; + protected Job job; protected DataSource dataSource; private boolean ordered; @@ -480,7 +475,7 @@ public void put(TableDesc td) { jdbc.update(pts); prof.checkpoint("put-table"); - // add/remove columns not supported so udpate flag is same for the table and + // add/remove columns not supported so update flag is same for the table and // column(s) PutColumnStatement pcs = new PutColumnStatement(update); for (ColumnDesc cd : td.getColumnDescs()) { @@ -827,6 +822,7 @@ public PreparedStatement createPreparedStatement(Connection conn) throws SQLExce StringBuilder sb = new StringBuilder(); sb.append("SELECT ").append(toCommaList(tsTablesCols, 0)); sb.append(",").append(toCommaList(accessControlCols, 0)); + sb.append(",").append(apiCreated); sb.append(" FROM ").append(tap_schema_tab); if (tableName != null) { @@ -1161,8 +1157,10 @@ public PreparedStatement createPreparedStatement(Connection conn) throws SQLExce sb.append("INSERT INTO ").append(tablesTableName); sb.append(" ("); sb.append(toCommaList(tsTablesCols, 0)); + sb.append(",").append(apiCreated); sb.append(") VALUES ("); sb.append(toParamList(tsTablesCols, 0)); + sb.append(",?"); sb.append(")"); } String sql = sb.toString(); @@ -1178,6 +1176,10 @@ public PreparedStatement createPreparedStatement(Connection conn) throws SQLExce safeSetInteger(sb, ps, col++, table.tableIndex); safeSetString(sb, ps, col++, table.getSchemaName()); safeSetString(sb, ps, col++, table.getTableName()); + // created set only in a PUT + if (!update) { + safeSetBoolean(sb, ps, col++, table.created); + } return ps; } @@ -1546,6 +1548,7 @@ public Object mapRow(ResultSet rs, int rowNum) throws SQLException { if (tapPermissionsMapper != null) { tableDesc.tapPermissions = tapPermissionsMapper.mapRow(rs, rowNum); } + tableDesc.created = rs.getInt("api_created") == 1; return tableDesc; } diff --git a/cadc-tap-schema/src/main/java/ca/nrc/cadc/vosi/actions/DeleteAction.java b/cadc-tap-schema/src/main/java/ca/nrc/cadc/vosi/actions/DeleteAction.java index 226a8cec..b0de22b0 100644 --- a/cadc-tap-schema/src/main/java/ca/nrc/cadc/vosi/actions/DeleteAction.java +++ b/cadc-tap-schema/src/main/java/ca/nrc/cadc/vosi/actions/DeleteAction.java @@ -73,6 +73,7 @@ import ca.nrc.cadc.profiler.Profiler; import ca.nrc.cadc.rest.RestAction; import ca.nrc.cadc.tap.db.TableCreator; +import ca.nrc.cadc.tap.schema.TableDesc; import ca.nrc.cadc.tap.schema.TapSchemaDAO; import java.security.AccessControlException; import javax.sql.DataSource; @@ -118,8 +119,13 @@ public void doAction() throws Exception { // drop table TableCreator tc = new TableCreator(ds); - tc.dropTable(tableName); - prof.checkpoint("delete-table"); + // if the table was created with the API, drop the table, + // otherwise only delete the table from the tap_schema + TableDesc tableDesc = ts.getTable(tableName); + if (tableDesc.created) { + tc.dropTable(tableName); + prof.checkpoint("delete-table"); + } // remove from tap_schema last to minimise locking ts.delete(tableName); diff --git a/cadc-tap-schema/src/main/java/ca/nrc/cadc/vosi/actions/PutAction.java b/cadc-tap-schema/src/main/java/ca/nrc/cadc/vosi/actions/PutAction.java index 0146d85e..779fe908 100644 --- a/cadc-tap-schema/src/main/java/ca/nrc/cadc/vosi/actions/PutAction.java +++ b/cadc-tap-schema/src/main/java/ca/nrc/cadc/vosi/actions/PutAction.java @@ -216,6 +216,8 @@ private void createTable(TapSchemaDAO ts, String schemaName, String tableName) t if (td != null) { throw new ResourceAlreadyExistsException("table " + tableName + " already exists"); } + // flag table as created using the API to allow table deletion in the DeleteAction + td.created = true; Profiler prof = new Profiler(PutAction.class); DatabaseTransactionManager tm = new DatabaseTransactionManager(ds); diff --git a/cadc-tap-schema/src/main/resources/postgresql/tap_schema.upgrade-1.2.1.sql b/cadc-tap-schema/src/main/resources/postgresql/tap_schema.upgrade-1.2.1.sql new file mode 100644 index 00000000..1d5aebd9 --- /dev/null +++ b/cadc-tap-schema/src/main/resources/postgresql/tap_schema.upgrade-1.2.1.sql @@ -0,0 +1,5 @@ +-- +-- upgrade from 1.2.0 to 1.2.1 +-- add api_created to flag a table created using the TAP API + +alter table tap_schema.tables11 add column api_created integer; diff --git a/cadc-tap-schema/src/main/resources/postgresql/tap_schema11.sql b/cadc-tap-schema/src/main/resources/postgresql/tap_schema11.sql index c2a5cffd..84856250 100644 --- a/cadc-tap-schema/src/main/resources/postgresql/tap_schema11.sql +++ b/cadc-tap-schema/src/main/resources/postgresql/tap_schema11.sql @@ -40,6 +40,9 @@ create table tap_schema.tables11 read_only_group varchar(128), read_write_group varchar(128), +-- extension: flag to indicate if a table was created using a Tap service API + api_created integer, + primary key (table_name), foreign key (schema_name) references tap_schema.schemas11 (schema_name) )