Skip to content

Commit

Permalink
Merge pull request #873 from fliptaboada/fix-loaddata-csv
Browse files Browse the repository at this point in the history
generateChangeLog loadData skipping empty tables and columns
  • Loading branch information
nvoxland authored Jul 8, 2019
2 parents 6ee3092 + 7fa5dcc commit 5db89d9
Show file tree
Hide file tree
Showing 2 changed files with 81 additions and 79 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ release.properties
liquibase.integrationtest.local.properties
/liquibase
derby.log
/.idea
.idea
*.iml
.DS_Store
/tmp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,101 +65,103 @@ public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl outpu
stmt.setFetchSize(100);
rs = stmt.executeQuery(sql);

List<String> columnNames = new ArrayList<>();
for (int i=0; i< rs.getMetaData().getColumnCount(); i++) {
columnNames.add(rs.getMetaData().getColumnName(i+1));
}
if (rs.isBeforeFirst()) {
List<String> columnNames = new ArrayList<>();
for (int i = 0; i < rs.getMetaData().getColumnCount(); i++) {
columnNames.add(rs.getMetaData().getColumnName(i + 1));
}

String fileName = table.getName().toLowerCase() + ".csv";
if (dataDir != null) {
fileName = dataDir + "/" + fileName;
String fileName = table.getName().toLowerCase() + ".csv";
if (dataDir != null) {
fileName = dataDir + "/" + fileName;

File parentDir = new File(dataDir);
if (!parentDir.exists()) {
parentDir.mkdirs();
}
if (!parentDir.isDirectory()) {
throw new IOException(parentDir.getAbsolutePath() + " is not a valid directory");
File parentDir = new File(dataDir);
if (!parentDir.exists()) {
parentDir.mkdirs();
}
if (!parentDir.isDirectory()) {
throw new IOException(parentDir.getAbsolutePath() + " is not a valid directory");
}
}
}

String[] dataTypes = new String[0];
try (

String[] dataTypes = new String[0];
try (
FileOutputStream fileOutputStream = new FileOutputStream(fileName);
OutputStreamWriter outputStreamWriter = new OutputStreamWriter(
fileOutputStream,
LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class)
.getOutputEncoding()
fileOutputStream,
LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class)
.getOutputEncoding()
);
CSVWriter outputFile = new CSVWriter(new BufferedWriter(outputStreamWriter));
) {

dataTypes = new String[columnNames.size()];
String[] line = new String[columnNames.size()];
for (int i = 0; i < columnNames.size(); i++) {
line[i] = columnNames.get(i);
}
outputFile.writeNext(line);

int rowNum = 0;
while (rs.next()) {
line = new String[columnNames.size()];

) {

dataTypes = new String[columnNames.size()];
String[] line = new String[columnNames.size()];
for (int i = 0; i < columnNames.size(); i++) {
Object value = JdbcUtils.getResultSetValue(rs, i + 1);
if ((dataTypes[i] == null) && (value != null)) {
if (value instanceof Number) {
dataTypes[i] = "NUMERIC";
} else if (value instanceof Boolean) {
dataTypes[i] = "BOOLEAN";
} else if (value instanceof Date) {
dataTypes[i] = "DATE";
} else {
dataTypes[i] = "STRING";
line[i] = columnNames.get(i);
}
outputFile.writeNext(line);

int rowNum = 0;
while (rs.next()) {
line = new String[columnNames.size()];

for (int i = 0; i < columnNames.size(); i++) {
Object value = JdbcUtils.getResultSetValue(rs, i + 1);
if ((dataTypes[i] == null) && (value != null)) {
if (value instanceof Number) {
dataTypes[i] = "NUMERIC";
} else if (value instanceof Boolean) {
dataTypes[i] = "BOOLEAN";
} else if (value instanceof Date) {
dataTypes[i] = "DATE";
} else {
dataTypes[i] = "STRING";
}
}
}
if (value == null) {
line[i] = "NULL";
} else {
if (value instanceof Date) {
line[i] = new ISODateFormat().format(((Date) value));
if (value == null) {
line[i] = "NULL";
} else {
line[i] = value.toString();
if (value instanceof Date) {
line[i] = new ISODateFormat().format(((Date) value));
} else {
line[i] = value.toString();
}
}
}
}
outputFile.writeNext(line);
rowNum++;
if ((rowNum % 5000) == 0) {
outputFile.flush();
outputFile.writeNext(line);
rowNum++;
if ((rowNum % 5000) == 0) {
outputFile.flush();
}
}
}
}

LoadDataChange change = new LoadDataChange();
change.setFile(fileName);
change.setEncoding(LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
if (outputControl.getIncludeCatalog()) {
change.setCatalogName(table.getSchema().getCatalogName());
}
if (outputControl.getIncludeSchema()) {
change.setSchemaName(table.getSchema().getName());
}
change.setTableName(table.getName());

for (int i = 0; i < columnNames.size(); i++) {
String colName = columnNames.get(i);
LoadDataColumnConfig columnConfig = new LoadDataColumnConfig();
columnConfig.setHeader(colName);
columnConfig.setName(colName);
columnConfig.setType(dataTypes[i]);
LoadDataChange change = new LoadDataChange();
change.setFile(fileName);
change.setEncoding(LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
if (outputControl.getIncludeCatalog()) {
change.setCatalogName(table.getSchema().getCatalogName());
}
if (outputControl.getIncludeSchema()) {
change.setSchemaName(table.getSchema().getName());
}
change.setTableName(table.getName());

change.addColumn(columnConfig);
}
for (int i = 0; i < columnNames.size(); i++) {
String colName = columnNames.get(i);
LoadDataColumnConfig columnConfig = new LoadDataColumnConfig();
columnConfig.setHeader(colName);
columnConfig.setName(colName);
columnConfig.setType(dataTypes[i] != null ? dataTypes[i] : "skip");

return new Change[]{
change
};
change.addColumn(columnConfig);
}
return new Change[]{
change
};
}
return new Change[]{};
} catch (Exception e) {
throw new UnexpectedLiquibaseException(e);
} finally {
Expand Down

0 comments on commit 5db89d9

Please sign in to comment.