use of liquibase.util.csv.CSVWriter in project liquibase by liquibase.
the class OfflineChangeLogHistoryService method appendChangeSet.
protected void appendChangeSet(ChangeSet changeSet, ChangeSet.ExecType execType) throws DatabaseException {
File oldFile = this.changeLogFile;
File newFile = new File(oldFile.getParentFile(), oldFile.getName() + ".new");
Reader reader = null;
Writer writer = null;
try {
reader = new InputStreamReader(new FileInputStream(oldFile), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
writer = new OutputStreamWriter(new FileOutputStream(newFile), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
CSVReader csvReader = new CSVReader(reader);
CSVWriter csvWriter = new CSVWriter(writer);
String[] line;
while ((line = csvReader.readNext()) != null) {
csvWriter.writeNext(line);
}
String[] newLine = new String[14];
newLine[COLUMN_ID] = changeSet.getId();
newLine[COLUMN_AUTHOR] = changeSet.getAuthor();
newLine[COLUMN_FILENAME] = changeSet.getFilePath();
newLine[COLUMN_DATEEXECUTED] = new ISODateFormat().format(new java.sql.Timestamp(new Date().getTime()));
newLine[COLUMN_ORDEREXECUTED] = String.valueOf(getNextSequenceValue());
newLine[COLUMN_EXECTYPE] = execType.value;
newLine[COLUMN_MD5SUM] = changeSet.generateCheckSum().toString();
newLine[COLUMN_DESCRIPTION] = changeSet.getDescription();
newLine[COLUMN_COMMENTS] = changeSet.getComments();
newLine[COLUMN_TAG] = "";
newLine[COLUMN_LIQUIBASE] = LiquibaseUtil.getBuildVersion().replaceAll("SNAPSHOT", "SNP");
if (newLine.length > 11) {
newLine[COLUMN_CONTEXTS] = changeSet.getContexts() == null ? null : changeSet.getContexts().toString();
newLine[COLUMN_LABELS] = changeSet.getLabels() == null ? null : changeSet.getLabels().toString();
}
if (newLine.length > 13) {
newLine[DEPLOYMENT_ID] = getDeploymentId();
}
csvWriter.writeNext(newLine);
csvWriter.flush();
csvWriter.close();
writer = null;
csvReader.close();
reader = null;
oldFile.delete();
newFile.renameTo(oldFile);
} catch (Exception e) {
throw new DatabaseException(e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ignore) {
}
}
if (writer != null) {
try {
writer.flush();
writer.close();
} catch (IOException ignore) {
}
}
}
}
use of liquibase.util.csv.CSVWriter in project liquibase by liquibase.
the class OfflineChangeLogHistoryService method writeHeader.
protected void writeHeader(File file) throws IOException {
Writer writer = null;
try {
writer = new OutputStreamWriter(new FileOutputStream(file), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
CSVWriter csvWriter = new CSVWriter(writer);
csvWriter.writeNext(new String[] { "ID", "AUTHOR", "FILENAME", "DATEEXECUTED", "ORDEREXECUTED", "EXECTYPE", "MD5SUM", "DESCRIPTION", "COMMENTS", "TAG", "LIQUIBASE", "CONTEXTS", "LABELS" });
} finally {
if (writer != null) {
writer.flush();
writer.close();
}
}
}
use of liquibase.util.csv.CSVWriter in project liquibase by liquibase.
the class OfflineChangeLogHistoryService method replaceChangeSet.
protected void replaceChangeSet(ChangeSet changeSet, ReplaceChangeSetLogic replaceLogic) throws DatabaseException {
File oldFile = this.changeLogFile;
File newFile = new File(oldFile.getParentFile(), oldFile.getName() + ".new");
Reader reader = null;
Writer writer = null;
try {
reader = new InputStreamReader(new FileInputStream(oldFile), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
writer = new OutputStreamWriter(new FileOutputStream(newFile), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding());
CSVReader csvReader = new CSVReader(reader);
CSVWriter csvWriter = new CSVWriter(writer);
String[] line;
while ((line = csvReader.readNext()) != null) {
if (changeSet == null || (line[COLUMN_ID].equals(changeSet.getId()) && line[COLUMN_AUTHOR].equals(changeSet.getAuthor()) && line[COLUMN_FILENAME].equals(changeSet.getFilePath()))) {
line = replaceLogic.execute(line);
}
if (line != null) {
csvWriter.writeNext(line);
}
}
csvWriter.flush();
csvWriter.close();
writer = null;
csvReader.close();
reader = null;
oldFile.delete();
newFile.renameTo(oldFile);
} catch (Exception e) {
throw new DatabaseException(e);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ignore) {
}
}
if (writer != null) {
try {
writer.flush();
writer.close();
} catch (IOException ignore) {
}
}
}
}
use of liquibase.util.csv.CSVWriter in project liquibase by liquibase.
the class MissingDataExternalFileChangeGenerator method fixMissing.
@Override
public Change[] fixMissing(DatabaseObject missingObject, DiffOutputControl outputControl, Database referenceDatabase, Database comparisionDatabase, ChangeGeneratorChain chain) {
Statement stmt = null;
ResultSet rs = null;
try {
Data data = (Data) missingObject;
Table table = data.getTable();
if (referenceDatabase.isLiquibaseObject(table)) {
return null;
}
String sql = "SELECT * FROM " + referenceDatabase.escapeTableName(table.getSchema().getCatalogName(), table.getSchema().getName(), table.getName());
stmt = ((JdbcConnection) referenceDatabase.getConnection()).createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
stmt.setFetchSize(100);
rs = stmt.executeQuery(sql);
List<String> columnNames = new ArrayList<String>();
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;
File parentDir = new File(dataDir);
if (!parentDir.exists()) {
parentDir.mkdirs();
}
if (!parentDir.isDirectory()) {
throw new RuntimeException(parentDir + " is not a directory");
}
}
CSVWriter outputFile = new CSVWriter(new BufferedWriter(new OutputStreamWriter(new FileOutputStream(fileName), LiquibaseConfiguration.getInstance().getConfiguration(GlobalConfiguration.class).getOutputEncoding())));
String[] 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()];
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));
} else {
line[i] = value.toString();
}
}
}
outputFile.writeNext(line);
rowNum++;
if (rowNum % 5000 == 0) {
outputFile.flush();
}
}
outputFile.flush();
outputFile.close();
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]);
change.addColumn(columnConfig);
}
return new Change[] { change };
} catch (Exception e) {
throw new UnexpectedLiquibaseException(e);
} finally {
if (rs != null) {
try {
rs.close();
} catch (SQLException ignore) {
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException ignore) {
}
}
}
}
Aggregations