Skip to content

Commit

Permalink
Revert "Fix CheckSum generation issues + CURRENT_CHECKSUM_ALGORITHM_V…
Browse files Browse the repository at this point in the history
…ERSION updated from 8 to 9."

This reverts commit ba254bd.
  • Loading branch information
filipe committed Mar 3, 2023
1 parent fd07dbf commit 72c8031
Show file tree
Hide file tree
Showing 17 changed files with 205 additions and 396 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -468,19 +468,7 @@ public boolean supportsRollback(Database database) {
*/
@Override
public CheckSum generateCheckSum() {
return CheckSum.compute(new StringChangeLogSerializer(new StringChangeLogSerializer.FieldFilter() {
@Override
public boolean include(Object obj, String field, Object value) {
if(Arrays.stream(getExcludedFieldFilters()).anyMatch(filter -> filter.equals(field))) {
return false;
}
return super.include(obj, field, value);
}
}).serialize(this, false));
}

public String[] getExcludedFieldFilters() {
return new String[0];
return CheckSum.compute(new StringChangeLogSerializer().serialize(this, false));
}

/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -197,11 +197,11 @@ public CheckSum generateCheckSum() {
}

if (sql != null) {
stream = new ByteArrayInputStream(sql.getBytes(GlobalConfiguration.FILE_ENCODING.getCurrentValue())
stream = new ByteArrayInputStream(sql.getBytes(GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue())
);
}

return CheckSum.compute(new NormalizingStream(stream), false);
return CheckSum.compute(new NormalizingStream(this.getEndDelimiter(), this.isSplitStatements(), this.isStripComments(), stream), false);
} catch (IOException e) {
throw new UnexpectedLiquibaseException(e);
} finally {
Expand Down Expand Up @@ -282,7 +282,8 @@ protected String normalizeLineEndings(String string) {
}

public static class NormalizingStream extends InputStream {
private InputStream stream;
private ByteArrayInputStream headerStream;
private PushbackInputStream stream;

private byte[] quickBuffer = new byte[100];
private List<Byte> resizingBuffer = new ArrayList<>();
Expand All @@ -291,25 +292,44 @@ public static class NormalizingStream extends InputStream {
private int lastChar = 'X';
private boolean seenNonSpace;

@Deprecated
public NormalizingStream(String endDelimiter, Boolean splitStatements, Boolean stripComments, InputStream stream) {
this(stream);
}

public NormalizingStream(InputStream stream) {
if(stream == null) {
stream = new ByteArrayInputStream(new byte[0]);
}
this.stream = new PushbackInputStream(stream, 2048);
try {
this.headerStream = new ByteArrayInputStream((endDelimiter+":"+splitStatements+":"+stripComments+":").getBytes(GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue()));
} catch (UnsupportedEncodingException e) {
throw new UnexpectedLiquibaseException(e);
}
}

@Override
public int read() throws IOException {
if (headerStream != null) {
int returnChar = headerStream.read();
if (returnChar != -1) {
return returnChar;
}
headerStream = null;
}

int returnChar = stream.read();
if (isWhiteSpace(returnChar)) {
returnChar = ' ';
}

while (isWhiteSpace(returnChar)) {
while ((returnChar == ' ') && (!seenNonSpace || (lastChar == ' '))) {
returnChar = stream.read();

if (isWhiteSpace(returnChar)) {
returnChar = ' ';
}
}

seenNonSpace = true;

lastChar = returnChar;

if ((lastChar == ' ') && isOnlyWhitespaceRemaining()) {
return -1;
}

return returnChar;
Expand All @@ -335,6 +355,40 @@ public synchronized void reset() throws IOException {
stream.reset();
}

private boolean isOnlyWhitespaceRemaining() throws IOException {
try {
int quickBufferUsed = 0;
while (true) {
byte read = (byte) stream.read();
if (quickBufferUsed >= quickBuffer.length) {
resizingBuffer.add(read);
} else {
quickBuffer[quickBufferUsed++] = read;
}

if (read == -1) {
return true;
}
if (!isWhiteSpace(read)) {
if (!resizingBuffer.isEmpty()) {

byte[] buf = new byte[resizingBuffer.size()];
for (int i=0; i< resizingBuffer.size(); i++) {
buf[i] = resizingBuffer.get(i);
}

stream.unread(buf);
}

stream.unread(quickBuffer, 0, quickBufferUsed);
return false;
}
}
} finally {
resizingBuffer.clear();
}
}

private boolean isWhiteSpace(int read) {
return (read == ' ') || (read == '\n') || (read == '\r') || (read == '\t');
}
Expand Down
8 changes: 5 additions & 3 deletions liquibase-core/src/main/java/liquibase/change/CheckSum.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public final class CheckSum {
private int version;
private String storedCheckSum;

private static final int CURRENT_CHECKSUM_ALGORITHM_VERSION = 9;
private static final int CURRENT_CHECKSUM_ALGORITHM_VERSION = 8;
private static final char DELIMITER = ':';
private static final Pattern CHECKSUM_PATTERN = Pattern.compile("(^\\d)" + DELIMITER + "([a-zA-Z0-9]++)");

Expand Down Expand Up @@ -69,8 +69,10 @@ public static int getCurrentVersion() {
public static CheckSum compute(String valueToChecksum) {
return new CheckSum(MD5Util.computeMD5(
//remove "Unknown" unicode char 65533
Normalizer.normalize(StringUtil.standardizeLineEndings(valueToChecksum)
.replace("\uFFFD", ""), Normalizer.Form.NFC)
Normalizer.normalize(
StringUtil.standardizeLineEndings(valueToChecksum)
.replace("\uFFFD", "")
, Normalizer.Form.NFC)
), getCurrentVersion());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Map;

@DatabaseChange(
Expand Down Expand Up @@ -111,7 +110,13 @@ public void setRelativeToChangelogFile(Boolean relativeToChangelogFile) {
this.relativeToChangelogFile = relativeToChangelogFile;
}

@DatabaseChangeProperty(isChangeProperty = false)
@DatabaseChangeProperty(
exampleValue = "CREATE OR REPLACE PROCEDURE testHello\n" +
" IS\n" +
" BEGIN\n" +
" DBMS_OUTPUT.PUT_LINE('Hello From The Database!');\n" +
" END;",
serializationType = SerializationType.DIRECT_VALUE)
/**
* @deprecated Use getProcedureText() instead
*/
Expand All @@ -127,13 +132,7 @@ public void setProcedureBody(String procedureText) {
this.procedureText = procedureText;
}

@DatabaseChangeProperty(
exampleValue = "CREATE OR REPLACE PROCEDURE testHello\n" +
" IS\n" +
" BEGIN\n" +
" DBMS_OUTPUT.PUT_LINE('Hello From The Database!');\n" +
" END;",
serializationType = SerializationType.DIRECT_VALUE)
@DatabaseChangeProperty(isChangeProperty = false)
public String getProcedureText() {
return procedureText;
}
Expand Down Expand Up @@ -241,25 +240,39 @@ public InputStream openSqlStream() throws IOException {
*/
@Override
public CheckSum generateCheckSum() {
if (this.path == null) {
return super.generateCheckSum();
}

InputStream stream = null;
try {
if (this.path == null) {
String procedureText = this.procedureText;
Charset encoding = GlobalConfiguration.FILE_ENCODING.getCurrentValue();
if (procedureText != null) {
stream = openSqlStream();
} catch (IOException e) {
throw new UnexpectedLiquibaseException(e);
}

try {
String procedureText = this.procedureText;
if ((stream == null) && (procedureText == null)) {
procedureText = "";
}

String encoding = GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue();
if (procedureText != null) {
try {
stream = new ByteArrayInputStream(procedureText.getBytes(encoding));
} catch (UnsupportedEncodingException e) {
throw new AssertionError(encoding +
" is not supported by the JVM, this should not happen according to the JavaDoc of " +
"the Charset class"
);
}
}
else {
stream = openSqlStream();
}
CheckSum checkSum = CheckSum.compute(new AbstractSQLChange.NormalizingStream(stream), false);
return CheckSum.compute(super.generateCheckSum().toString() + ":" + checkSum.toString());

} catch (IOException e) {
throw new UnexpectedLiquibaseException(e);
}
finally {
CheckSum checkSum = CheckSum.compute(new AbstractSQLChange.NormalizingStream(";", false, false, stream), false);

return CheckSum.compute(super.generateCheckSum().toString() + ":" + checkSum);
} finally {
if (stream != null) {
try {
stream.close();
Expand All @@ -271,18 +284,6 @@ public CheckSum generateCheckSum() {

}

@Override
public String[] getExcludedFieldFilters() {
return new String[]{
"path",
"dbms",
"relativeToChangelogFile",
"procedureText",
"encoding",
"comments"
};
}

@Override
public SqlStatement[] generateStatements(Database database) {
String endDelimiter = ";";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@
import liquibase.snapshot.SnapshotGeneratorFactory;
import liquibase.sqlgenerator.SqlGeneratorFactory;
import liquibase.statement.SqlStatement;
import liquibase.statement.core.CreateViewStatement;
import liquibase.statement.core.DropViewStatement;
import liquibase.statement.core.SetViewRemarksStatement;
import liquibase.statement.core.*;
import liquibase.structure.core.View;
import liquibase.util.FileUtil;
import liquibase.util.ObjectUtil;
Expand All @@ -26,7 +24,7 @@
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
Expand Down Expand Up @@ -177,39 +175,42 @@ protected InputStream openSqlStream() throws IOException {
}
}

@Override
public String[] getExcludedFieldFilters() {
return new String[] {
"path",
"relativeToChangelogFile",
"selectQuery",
"encoding"
};
}

/**
* Calculates the checksum based on the contained SQL.
*
* @see liquibase.change.AbstractChange#generateCheckSum()
*/
@Override
public CheckSum generateCheckSum() {
if (this.path == null) {
return super.generateCheckSum();
}

InputStream stream = null;
try {
if (this.path == null) {
String selectQuery = this.selectQuery;
Charset encoding = GlobalConfiguration.FILE_ENCODING.getCurrentValue();
stream = new ByteArrayInputStream(selectQuery.getBytes(encoding));
} else {
stream = openSqlStream();
stream = openSqlStream();
} catch (IOException e) {
throw new UnexpectedLiquibaseException(e);
}

try {
String selectQuery = this.selectQuery;
if ((stream == null) && (selectQuery == null)) {
selectQuery = "";
}

String encoding = GlobalConfiguration.OUTPUT_FILE_ENCODING.getCurrentValue();
if (selectQuery != null) {
try {
stream = new ByteArrayInputStream(selectQuery.getBytes(encoding));
} catch (UnsupportedEncodingException e) {
throw new AssertionError(encoding+" is not supported by the JVM, this should not happen according to the JavaDoc of the Charset class");
}
}

CheckSum checkSum = CheckSum.compute(new AbstractSQLChange.NormalizingStream(stream), false);
return CheckSum.compute(super.generateCheckSum().toString() + ":" + checkSum.toString());
CheckSum checkSum = CheckSum.compute(new AbstractSQLChange.NormalizingStream(";", false, false, stream), false);

} catch (IOException e) {
throw new UnexpectedLiquibaseException(e);
return CheckSum.compute(super.generateCheckSum().toString() + ":" + checkSum);
} finally {
if (stream != null) {
try {
Expand Down
Loading

0 comments on commit 72c8031

Please sign in to comment.