Skip to content

Commit

Permalink
Extract migration logic to separate class
Browse files Browse the repository at this point in the history
Signed-off-by: Jacob Laursen <jacob-github@vindvejr.dk>
  • Loading branch information
jlaur committed Oct 14, 2022
1 parent 25a2f47 commit 234b19e
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@

import java.sql.SQLInvalidAuthorizationSpecException;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -325,50 +324,15 @@ private void formatTableNames() {

Map<Integer, String> tableIds = new HashMap<>();

//
for (ItemsVO vo : getItemIDTableNames()) {
String t = namingStrategy.getTableName(vo.getItemid(), vo.getItemname());
sqlTables.put(vo.getItemname(), t);
tableIds.put(vo.getItemid(), t);
}

//
List<ItemsVO> al = getItemTables();

String oldName = "";
String newName = "";
List<ItemVO> oldNewTablenames = new ArrayList<>();
for (int i = 0; i < al.size(); i++) {
int id = -1;
oldName = al.get(i).getTable_name();
logger.info("JDBC::formatTableNames: found Table Name= {}", oldName);

if (oldName.startsWith(conf.getTableNamePrefix()) && !oldName.contains("_")) {
id = Integer.parseInt(oldName.substring(conf.getTableNamePrefix().length()));
logger.info("JDBC::formatTableNames: found Table with Prefix '{}' Name= {} id= {}",
conf.getTableNamePrefix(), oldName, (id));
} else if (oldName.contains("_")) {
id = Integer.parseInt(oldName.substring(oldName.lastIndexOf("_") + 1));
logger.info("JDBC::formatTableNames: found Table Name= {} id= {}", oldName, (id));
}
logger.info("JDBC::formatTableNames: found Table id= {}", id);

newName = tableIds.get(id);
logger.info("JDBC::formatTableNames: found Table newName= {}", newName);
List<ItemsVO> itemTables = getItemTables();

if (newName != null) {
if (!oldName.equalsIgnoreCase(newName)) {
oldNewTablenames.add(new ItemVO(oldName, newName));
logger.info("JDBC::formatTableNames: Table '{}' will be renamed to '{}'", oldName, newName);
} else {
logger.info("JDBC::formatTableNames: Table oldName='{}' newName='{}' nothing to rename", oldName,
newName);
}
} else {
logger.error("JDBC::formatTableNames: Table '{}' could NOT be renamed to '{}'", oldName, newName);
break;
}
}
List<ItemVO> oldNewTablenames = namingStrategy.prepareMigration(tableIds, itemTables);

updateItemTableNames(oldNewTablenames);
logger.info("JDBC::formatTableNames: Finished updating {} item table names", oldNewTablenames.size());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,15 @@
*/
package org.openhab.persistence.jdbc.internal;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.openhab.persistence.jdbc.dto.ItemVO;
import org.openhab.persistence.jdbc.dto.ItemsVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* This class manages strategy for table names.
Expand All @@ -21,8 +29,11 @@
*/
@NonNullByDefault
public class NamingStrategy {

private static final String ITEM_NAME_PATTERN = "[^a-zA-Z_0-9\\-]";

private final Logger logger = LoggerFactory.getLogger(NamingStrategy.class);

private JdbcConfiguration configuration;

public NamingStrategy(JdbcConfiguration configuration) {
Expand Down Expand Up @@ -50,4 +61,43 @@ private static String formatRight(final Object value, final int len) {
return valueAsString;
}
}

public List<ItemVO> prepareMigration(Map<Integer, String> tableIds, List<ItemsVO> itemTables) {
String oldName = "";
String newName = "";
List<ItemVO> oldNewTablenames = new ArrayList<>();
for (int i = 0; i < itemTables.size(); i++) {
int id = -1;
oldName = itemTables.get(i).getTable_name();
logger.info("JDBC::formatTableNames: found Table Name= {}", oldName);

if (oldName.startsWith(configuration.getTableNamePrefix()) && !oldName.contains("_")) {
id = Integer.parseInt(oldName.substring(configuration.getTableNamePrefix().length()));
logger.info("JDBC::formatTableNames: found Table with Prefix '{}' Name= {} id= {}",
configuration.getTableNamePrefix(), oldName, (id));
} else if (oldName.contains("_")) {
id = Integer.parseInt(oldName.substring(oldName.lastIndexOf("_") + 1));
logger.info("JDBC::formatTableNames: found Table Name= {} id= {}", oldName, (id));
}
logger.info("JDBC::formatTableNames: found Table id= {}", id);

newName = tableIds.get(id);
logger.info("JDBC::formatTableNames: found Table newName= {}", newName);

if (newName != null) {
if (!oldName.equalsIgnoreCase(newName)) {
oldNewTablenames.add(new ItemVO(oldName, newName));
logger.info("JDBC::formatTableNames: Table '{}' will be renamed to '{}'", oldName, newName);
} else {
logger.info("JDBC::formatTableNames: Table oldName='{}' newName='{}' nothing to rename", oldName,
newName);
}
} else {
logger.error("JDBC::formatTableNames: Table '{}' could NOT be renamed to '{}'", oldName, newName);
break;
}
}

return oldNewTablenames;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,24 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.Is.is;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.jupiter.MockitoExtension;
import org.openhab.persistence.jdbc.dto.ItemVO;
import org.openhab.persistence.jdbc.dto.ItemsVO;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;

/**
* Tests the {@link NamingStrategy} class.
Expand All @@ -36,6 +47,8 @@ public class NamingStrategyTest {

@BeforeEach
public void initialize() {
final Logger logger = (Logger) LoggerFactory.getLogger(NamingStrategy.class);
logger.setLevel(Level.OFF);
namingStrategy = new NamingStrategy(configurationMock);
}

Expand All @@ -58,4 +71,54 @@ public void getTableNameWhenNotUseRealItemNamesAndCount4NameHasLeavingZeros() {
Mockito.doReturn("Item").when(configurationMock).getTableNamePrefix();
assertThat(namingStrategy.getTableName(2, "Test"), is("Item0002"));
}

@Test
public void prepareMigrationFromNumberedToRealNames() {
final int itemId = 1;
final String itemName = "Test";
final String tableName = "Item1";

Mockito.doReturn(true).when(configurationMock).getTableUseRealItemNames();
Mockito.doReturn("Item").when(configurationMock).getTableNamePrefix();

Map<Integer, String> tableIds = getTableIds(itemId, itemName);
List<ItemsVO> itemTables = getItemTables(tableName);

List<ItemVO> actual = namingStrategy.prepareMigration(tableIds, itemTables);

assertThat(actual.size(), is(1));
assertThat(actual.get(0).getNewTableName(), is("test"));
}

@Test
public void prepareMigrationFromMixedToNewRealNames() {
final int itemId = 1;
final String itemName = "Test";
final String tableName = "test_0001";

Mockito.doReturn(true).when(configurationMock).getTableUseRealItemNames();
Mockito.doReturn("Item").when(configurationMock).getTableNamePrefix();

Map<Integer, String> tableIds = getTableIds(itemId, itemName);
List<ItemsVO> itemTables = getItemTables(tableName);

List<ItemVO> actual = namingStrategy.prepareMigration(tableIds, itemTables);

assertThat(actual.size(), is(1));
assertThat(actual.get(0).getNewTableName(), is("test"));
}

private Map<Integer, String> getTableIds(int itemId, String itemName) {
Map<Integer, String> tableIds = new HashMap<>();
tableIds.put(itemId, namingStrategy.getTableName(1, itemName));
return tableIds;
}

private List<ItemsVO> getItemTables(String tableName) {
List<ItemsVO> itemTables = new ArrayList<ItemsVO>();
ItemsVO itemTable = new ItemsVO();
itemTable.setTable_name(tableName);
itemTables.add(itemTable);
return itemTables;
}
}

0 comments on commit 234b19e

Please sign in to comment.