Permalink
Browse files

Format json changelog non-string values correctly

  • Loading branch information...
1 parent 0890453 commit 73677feac46ef63a1b92cd9bb5dbdab563a4c62b @nvoxland nvoxland committed Mar 27, 2013
@@ -4,7 +4,9 @@
import liquibase.serializer.core.yaml.YamlChangeLogSerializer;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
+import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.Tag;
+import org.yaml.snakeyaml.representer.Represent;
public class JsonChangeLogSerializer extends YamlChangeLogSerializer {
@@ -20,7 +22,11 @@ protected Yaml createYaml() {
@Override
public String serialize(LiquibaseSerializable object, boolean pretty) {
- return yaml.dumpAs(toMap(object), Tag.MAP, DumperOptions.FlowStyle.FLOW);
+ String out = yaml.dumpAs(toMap(object), Tag.MAP, DumperOptions.FlowStyle.FLOW);
+ out = out.replaceAll("!!int \"(\\d+)\"", "$1");
+ out = out.replaceAll("!!bool \"(\\w+)\"", "$1");
+ out = out.replaceAll("!!timestamp \"([^\"]*)\"", "$1");
+ return out;
}
@@ -4,6 +4,9 @@
import liquibase.changelog.DatabaseChangeLog;
import liquibase.serializer.ChangeLogSerializer;
import liquibase.serializer.LiquibaseSerializable;
+import liquibase.statement.DatabaseFunction;
+import liquibase.statement.SequenceCurrentValueFunction;
+import liquibase.statement.SequenceNextValueFunction;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.introspector.FieldProperty;
@@ -13,13 +16,15 @@
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.NodeTuple;
import org.yaml.snakeyaml.nodes.Tag;
+import org.yaml.snakeyaml.representer.Represent;
import org.yaml.snakeyaml.representer.Representer;
import java.beans.IntrospectionException;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Type;
+import java.sql.Timestamp;
import java.util.*;
public class YamlChangeLogSerializer implements ChangeLogSerializer {
@@ -97,6 +102,11 @@ public void append(ChangeSet changeSet, File changeLogFile) throws IOException {
public static class LiquibaseRepresenter extends Representer {
+ public LiquibaseRepresenter() {
+ multiRepresenters.put(DatabaseFunction.class, new AsStringRepresenter());
+ multiRepresenters.put(SequenceNextValueFunction.class, new AsStringRepresenter());
+ multiRepresenters.put(SequenceCurrentValueFunction.class, new AsStringRepresenter());
+ }
@Override
protected Tag getTag(Class<?> clazz, Tag defaultTag) {
@@ -159,6 +169,12 @@ public Object get(Object object) {
return ((LiquibaseSerializable) object).getSerializableFieldValue(getName());
}
}
+
+ private class AsStringRepresenter implements Represent {
+ public Node representData(Object data) {
+ return representScalar(Tag.STR, data.toString());
+ }
+ }
}
private static class ChangeSetComparator implements Comparator<String> {
@@ -2,12 +2,7 @@
import liquibase.database.AbstractJdbcDatabase;
import liquibase.database.Database;
-import liquibase.database.core.DB2Database;
-import liquibase.database.core.HsqlDatabase;
-import liquibase.database.core.MSSQLDatabase;
-import liquibase.database.core.MySQLDatabase;
-import liquibase.database.core.OracleDatabase;
-import liquibase.database.core.PostgresDatabase;
+import liquibase.database.core.*;
import liquibase.exception.DatabaseException;
import liquibase.executor.ExecutorService;
import liquibase.snapshot.InvalidExampleException;
@@ -106,6 +101,8 @@ protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) thro
"and t.tabname = '" + database.correctObjectName(table.getName(), Table.class) + "' " +
"and t.tabschema = '" + database.correctObjectName(schema.getCatalogName(), Catalog.class) + "' " +
"and t.type='U'";
+ } else if (database instanceof FirebirdDatabase) {
+ sql = "SELECT RDB$INDEX_NAME FROM RDB$INDICES WHERE RDB$RELATION_NAME='"+database.correctObjectName(table.getName(), Table.class)+"' AND RDB$UNIQUE_FLAG IS NOT NULL);";
} else {
sql = "select CONSTRAINT_NAME, CONSTRAINT_TYPE " +
"from information_schema.constraints " +
@@ -0,0 +1,74 @@
+package liquibase.serializer.core.json;
+
+import liquibase.change.ColumnConfig;
+import liquibase.change.core.AddColumnChange;
+import liquibase.changelog.ChangeSet;
+import liquibase.statement.DatabaseFunction;
+import liquibase.statement.SequenceNextValueFunction;
+import org.junit.Test;
+
+import java.util.Date;
+
+import static org.junit.Assert.assertEquals;
+
+public class JsonChangeLogSerializerTest {
+
+ @Test
+ public void serialize_changeSet() {
+ AddColumnChange addColumnChange = new AddColumnChange();
+ addColumnChange.setCatalogName("cat");
+ addColumnChange.addColumn(new ColumnConfig().setName("col1").setDefaultValueNumeric(3));
+ addColumnChange.addColumn(new ColumnConfig().setName("col2").setDefaultValueComputed(new DatabaseFunction("NOW()")));
+ addColumnChange.addColumn(new ColumnConfig().setName("col3").setDefaultValueBoolean(true));
+ addColumnChange.addColumn(new ColumnConfig().setName("col2").setDefaultValueDate(new Date(0)));
+ addColumnChange.addColumn(new ColumnConfig().setName("col2").setDefaultValueSequenceNext(new SequenceNextValueFunction("seq_me")));
+
+ ChangeSet changeSet = new ChangeSet("1", "nvoxland", false, false, "path/to/file.json", null, null);
+ changeSet.addChange(addColumnChange);
+ assertEquals("{\n" +
+ " \"changeSet\": {\n" +
+ " \"id\": \"1\",\n" +
+ " \"author\": \"nvoxland\",\n" +
+ " \"changes\": [\n" +
+ " {\n" +
+ " \"addColumn\": {\n" +
+ " \"catalogName\": \"cat\",\n" +
+ " \"columns\": [\n" +
+ " {\n" +
+ " \"column\": {\n" +
+ " \"defaultValueNumeric\": 3,\n" +
+ " \"name\": \"col1\"\n" +
+ " }\n" +
+ " },\n" +
+ " {\n" +
+ " \"column\": {\n" +
+ " \"defaultValueComputed\": \"NOW()\",\n" +
+ " \"name\": \"col2\"\n" +
+ " }\n" +
+ " },\n" +
+ " {\n" +
+ " \"column\": {\n" +
+ " \"defaultValueBoolean\": true,\n" +
+ " \"name\": \"col3\"\n" +
+ " }\n" +
+ " },\n" +
+ " {\n" +
+ " \"column\": {\n" +
+ " \"defaultValueDate\": 1970-01-01T00:00:00Z,\n" +
+ " \"name\": \"col2\"\n" +
+ " }\n" +
+ " },\n" +
+ " {\n" +
+ " \"column\": {\n" +
+ " \"defaultValueSequenceNext\": \"seq_me\",\n" +
+ " \"name\": \"col2\"\n" +
+ " }\n" +
+ " }]\n" +
+ " \n" +
+ " }\n" +
+ " }]\n" +
+ " \n" +
+ " }\n" +
+ "}\n", new JsonChangeLogSerializer().serialize(changeSet, true));
+ }
+}
@@ -9,7 +9,7 @@
/**
* To configure database:
- * create database 'c:\firebird\liquibase.fdb' page_size 8192;
+ * create database 'c:\firebird\liquibase.fdb' page_size 8192
* user 'liquibase' password 'liquibase';
*
* connecting later:

0 comments on commit 73677fe

Please sign in to comment.