22

Can I automatically convert Liquibase changelog files in the XML format to the YAML format?

user3364825
  • 1,541
  • 1
  • 15
  • 23

3 Answers3

14

There is nothing built in, but you can easily do it with a little scripting.

Some starting points: liquibase.parser.ChangeLogParserFactory.getInstance().getParser(".xml", resourceAccessor).parse(...) will return a DatabaseChangeLog object representing the changelog file.

liquibase.serializer.ChangeLogSerializerFactory.getInstance().getSerializer(".ya‌​ml").write(...) will output the changeSets in the DatabaseChangeLog object out to a file in yaml format

Nathan Voxland
  • 15,453
  • 2
  • 48
  • 64
11

I know this is a bit late of an answer; however, I was looking into using Liquibase for a project at work and was looking for something to do what he OP wanted. I saw Nathan Voxland's answer and made a command-line utility written in Java to convert this. The code can be found at here on Github. Feel free to modify it or fix it and send PRs; I literally wrote it in an hour or so, so please be easy on judging the code base :). Hopefully this will help out the next person that is looking for a similar tool and stumbles here.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Daniel Aquino
  • 401
  • 5
  • 9
4

Smart way is to directly generate yaml instead of xml, Liquibase provides this functionality OOB. You only need to change the extension of output changeLogFile file to .yaml and liquibase will understand you nicely (tested with liquibase-3.6.0).

For example: I ran this command for my Postgres DB:

liquibase --driver=org.postgresql.Driver 
          --classpath=\path\to\postgresql-9.0-801.jdbc4.jar
          --changeLogFile=D:\Liquibase\changeLog.yaml 
          --url="jdbc:postgresql://localhost:5432/db_name" 
          --username=postgres 
          --password=postgres 
          --logLevel=debug generateChangeLog

Caution: New lines were added for readability purpose.

Now you know what will happen if you change the extension of output file to .xml

PS: If liquibase command doesn't work try the same with java -jar liquibase.jar.

Ubercool
  • 1,029
  • 2
  • 14
  • 29
  • 1
    Good answer, but I think that OP wants to convert already existing xml changelogs to yml and not to generate them. Anyway your answer was helpful. Thanks. – senjin.hajrulahovic Aug 01 '18 at 13:29
  • Sometimes it is unclear what OP wanted to do and what he can actually do. May be the OP generated xml and tried converting them. Since the question was very old I didn't asked about it. Happy that it helped you. – Ubercool Aug 02 '18 at 10:31
  • Helped me too. Liquibase documentation about generation didn't mention YAML so it wasn't obvious that it could detect from the extension and create appropriately. – Pool Dec 12 '18 at 13:13