Skip to content

Commit

Permalink
[TOOLS-4563] Automatically change the trigger action definition when …
Browse files Browse the repository at this point in the history
…creating a foreign key query if the column in NOT NULL (#161)

http://jira.cubrid.org/browse/TOOLS-4563
  • Loading branch information
Srltas authored Mar 5, 2024
1 parent 2c987db commit 28fd608
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,11 @@ public class FK extends DBObject {
private String name;
private String ddl;

// private int deferability;
private int deleteRule;
private int updateRule;
private boolean changedReferentialAction = false;

private String referencedTableName;
// private String onCacheObject;

private final Map<String, String> col2RefMapping = new TreeMap<String, String>();

Expand Down Expand Up @@ -248,6 +247,14 @@ public Map<String, String> getColumns() {
return new TreeMap<String, String>(col2RefMapping);
}

public void setChangedReferentialAction(boolean changed) {
this.changedReferentialAction = changed;
}

public boolean isChangedReferentialAction() {
return this.changedReferentialAction;
}

/**
* Copy attributes from source
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Copyright (C) 2016 CUBRID Corporation. All rights reserved by Search Solution.
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
*
* - Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* - Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* - Neither the name of the <ORGANIZATION> nor the names of its contributors
* may be used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
*
*/

package com.cubrid.cubridmigration.core.engine.event;

import com.cubrid.cubridmigration.core.dbobject.DBObject;
import com.cubrid.cubridmigration.core.dbobject.FK;

/**
* Foreign key option setting alert event
*
* @author Dongmin Kim
*/
public class MigrationFKWarningEvent extends MigrationEvent {

private final DBObject dbObject;

public DBObject getDbObject() {
return dbObject;
}

public MigrationFKWarningEvent(DBObject dbObject) {
this.dbObject = dbObject;
}

/**
* Event to string.
*
* @return event string
*/
public String toString() {
if (dbObject == null) {
return "NULL";
}

FK fk = (FK) dbObject;
return "[FK_WARNING] table: "
+ fk.getTable().getName()
+ " fk: "
+ fk.getName()
+ " Change option(SET NULL -> RESTRICT)";
}

@Override
public int getLevel() {
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import com.cubrid.cubridmigration.core.dbobject.Table;
import com.cubrid.cubridmigration.core.engine.config.MigrationConfiguration;
import com.cubrid.cubridmigration.core.engine.config.SourceTableConfig;
import com.cubrid.cubridmigration.core.engine.event.MigrationFKWarningEvent;
import com.cubrid.cubridmigration.core.engine.task.ExportTask;

/**
Expand All @@ -56,5 +57,11 @@ protected void executeExportTask() {
for (FK fk : tt.getFks()) {
importTaskExecutor.execute((Runnable) taskFactory.createImportFKTask(fk));
}

for (FK fk : tt.getFks()) {
if (fk.isChangedReferentialAction()) {
eventHandler.handleEvent(new MigrationFKWarningEvent(fk));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
import com.cubrid.cubridmigration.core.dbobject.View;
import com.cubrid.cubridmigration.core.sql.SQLHelper;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.lang.StringUtils;
Expand All @@ -61,10 +63,18 @@ public class CUBRIDSQLHelper extends SQLHelper {

private static final Logger LOG = LogUtil.getLogger(CUBRIDSQLHelper.class);

private static enum ReferentialAction {
CASCADE,
RESTRICT,
SET_NULL,
NO_ACTION
}

private static final String[] UPDATE_RULE =
new String[] {"CASCADE", "RESTRICT", "SET NULL", "NO ACTION"};
private static final String[] DELETE_RULE =
new String[] {"CASCADE", "RESTRICT", "SET NULL", "NO ACTION"};

private static final String NEWLINE = "\n";
private static final String HINT = "/*+ NO_STATS */";
private static final String END_LINE_CHAR = ";";
Expand Down Expand Up @@ -176,6 +186,35 @@ public String getDBQualifier(String objectName) {
return getQuotedObjName(objectName);
}

/**
* If the referential action is 'SET NULL', check the column and change it to 'RESTRICT' if it
* is 'NOT NULL'
*
* @param referentialAction int
* @param fk FK
* @return int
*/
private int changeReferentialAction(int referentialAction, FK fk) {
if (referentialAction != ReferentialAction.SET_NULL.ordinal()) {
return referentialAction;
}

List<String> fkColumnNames = fk.getColumnNames();
Map<String, Column> tableColumnMap = new HashMap<String, Column>();
for (Column column : fk.getTable().getColumns()) {
tableColumnMap.put(column.getName(), column);
}

for (String columnName : fkColumnNames) {
if (!tableColumnMap.get(columnName).isNullable()) {
fk.setChangedReferentialAction(true);
return ReferentialAction.RESTRICT.ordinal();
}
}

return referentialAction;
}

/**
* DDL of FK in creating and altering a schema
*
Expand Down Expand Up @@ -222,8 +261,10 @@ private String getFKDDLConstraint(
}

bf.append(")");
bf.append(" ON DELETE ").append(DELETE_RULE[fk.getDeleteRule()]);
bf.append(" ON UPDATE ").append(UPDATE_RULE[fk.getUpdateRule()]);
bf.append(" ON DELETE ")
.append(DELETE_RULE[changeReferentialAction(fk.getDeleteRule(), fk)]);
bf.append(" ON UPDATE ")
.append(UPDATE_RULE[changeReferentialAction(fk.getUpdateRule(), fk)]);
return bf.toString();
}

Expand Down

0 comments on commit 28fd608

Please sign in to comment.