Skip to content

Commit f3038f8

Browse files
author
Giorgio Trettenero
committed
Disable ON DELETE CASCADE + TESTS
fix checkstyle fix checkstyle see if the test error is due to schema change check tests check tests checkstyle test test test test test test test test hopefully last test checkstyle already exists checkstyle test move test fix moved test fix test final
1 parent 8698f76 commit f3038f8

File tree

8 files changed

+77
-6
lines changed

8 files changed

+77
-6
lines changed

metacat-common-server/src/main/java/com/netflix/metacat/common/server/connectors/exception/ConnectorException.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ public ConnectorException(final String message, @Nullable final Throwable cause)
5050
*
5151
* @param message message
5252
* @param cause cause
53-
* @param enableSuppression eable suppression
53+
* @param enableSuppression enable suppression
5454
* @param writableStackTrace stacktrace
5555
*/
5656
public ConnectorException(
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.netflix.metacat.common.server.connectors.exception;
2+
3+
/*
4+
*
5+
* Copyright 2024 Netflix, Inc.
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
import com.netflix.metacat.common.QualifiedName;
22+
import lombok.Getter;
23+
24+
import javax.annotation.Nullable;
25+
26+
/**
27+
* Exception when database can't be deleted because ON DELETE CASCADE is
28+
* disabled and a table still exists in the database.
29+
*
30+
* @author gtret
31+
*/
32+
@Getter
33+
public class DatabasePreconditionFailedException extends ConnectorException {
34+
/**
35+
* Constructor.
36+
*
37+
* @param name qualified name of the database
38+
* @param message error description
39+
* @param error stacktrace
40+
*/
41+
public DatabasePreconditionFailedException(final QualifiedName name,
42+
@Nullable final String message,
43+
@Nullable final Throwable error) {
44+
super(String.format("Precondition failed to update table %s. %s", name, message), error);
45+
}
46+
}
47+
48+

metacat-connector-polaris/src/functionalTest/resources/schema.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ create table TBLS (
2525
created_date TIMESTAMP not null,
2626
last_updated_by STRING(255),
2727
last_updated_date TIMESTAMP not null,
28-
foreign key (db_name) references DBS(name) ON DELETE CASCADE ON UPDATE CASCADE
28+
foreign key (db_name) references DBS(name) ON DELETE RESTRICT ON UPDATE CASCADE
2929
);

metacat-connector-polaris/src/main/java/com/netflix/metacat/connector/polaris/PolarisConnectorDatabaseService.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import com.netflix.metacat.common.server.connectors.exception.ConnectorException;
1111
import com.netflix.metacat.common.server.connectors.exception.DatabaseAlreadyExistsException;
1212
import com.netflix.metacat.common.server.connectors.exception.DatabaseNotFoundException;
13+
import com.netflix.metacat.common.server.connectors.exception.DatabasePreconditionFailedException;
1314
import com.netflix.metacat.common.server.connectors.exception.InvalidMetaException;
1415
import com.netflix.metacat.common.server.connectors.model.DatabaseInfo;
1516
import com.netflix.metacat.connector.polaris.common.PolarisUtils;
@@ -87,11 +88,19 @@ public void delete(final ConnectorRequestContext context, final QualifiedName na
8788
try {
8889
this.polarisStoreService.deleteDatabase(name.getDatabaseName());
8990
} catch (DataIntegrityViolationException exception) {
91+
if (exception.getCause() instanceof org.hibernate.exception.ConstraintViolationException) {
92+
throw new DatabasePreconditionFailedException(
93+
name,
94+
String.format("Cannot delete database %s because it is not empty.", name.getDatabaseName()),
95+
exception
96+
);
97+
}
9098
throw new InvalidMetaException(name, exception);
9199
} catch (Exception exception) {
92100
throw new ConnectorException(
93101
String.format("Failed deleting polaris database %s", name), exception);
94102
}
103+
System.out.println("DID NOT CATCH THE DB DELETE ERROR");
95104
}
96105

97106
/**

metacat-connector-polaris/src/test/java/com/netflix/metacat/connector/polaris/PolarisConnectorDatabaseServiceTest.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
21
package com.netflix.metacat.connector.polaris;
32

43
import com.google.common.collect.Maps;
@@ -167,4 +166,3 @@ public void testDeleteDb() {
167166
Assert.assertFalse(polarisDBService.exists(requestContext, DB1_QUALIFIED_NAME));
168167
}
169168
}
170-

metacat-connector-polaris/src/test/java/com/netflix/metacat/connector/polaris/store/PolarisStoreConnectorTest.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.boot.test.mock.mockito.MockBean;
2020
import org.springframework.boot.test.mock.mockito.SpyBean;
2121
import org.springframework.dao.DataAccessException;
22+
import org.springframework.dao.DataIntegrityViolationException;
2223
import org.springframework.dao.OptimisticLockingFailureException;
2324
import org.springframework.data.auditing.AuditingHandler;
2425
import org.springframework.data.auditing.DateTimeProvider;
@@ -182,6 +183,21 @@ public void testTableCreationAndDeletionWithParams() {
182183
Assert.assertFalse(polarisConnector.tableExistsById(tblEntity.getTblId()));
183184
}
184185

186+
/**
187+
* Test database deletion if table exists and ON DELETE CASCADE is disabled.
188+
*/
189+
@Test
190+
public void testDbDeletionNoCascade() {
191+
final String dbName = generateDatabaseName();
192+
final String tblName = generateTableName();
193+
final PolarisDatabaseEntity dbEntity = createDB(dbName);
194+
final PolarisTableEntity tblEntity = createTable(dbName, tblName);
195+
196+
Assertions.assertThrows(DataIntegrityViolationException.class, () ->
197+
polarisConnector.deleteDatabase(dbName));
198+
Assert.assertTrue(polarisConnector.databaseExists(dbName));
199+
}
200+
185201
/**
186202
* Test to verify that table name can be updated.
187203
*/

metacat-connector-polaris/src/test/resources/h2db/schema.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ create table TBLS (
2525
created_date TIMESTAMP not null,
2626
last_updated_by varchar(255),
2727
last_updated_date TIMESTAMP not null,
28-
foreign key (db_name) references DBS(name) ON DELETE CASCADE ON UPDATE CASCADE
28+
foreign key (db_name) references DBS(name) ON DELETE RESTRICT ON UPDATE CASCADE
2929
);
3030

3131
CREATE INDEX DB_NAME_IDX ON TBLS(db_name);

metacat-functional-tests/metacat-test-cluster/datastores/crdb/sql/schema.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ create table TBLS (
2525
last_updated_by STRING(255),
2626
last_updated_date TIMESTAMP not null,
2727
constraint uniq_name unique(db_name, tbl_name),
28-
foreign key (db_name) references DBS(name) ON DELETE CASCADE ON UPDATE CASCADE
28+
foreign key (db_name) references DBS(name) ON DELETE RESTRICT ON UPDATE CASCADE
2929
);

0 commit comments

Comments
 (0)