3

I want Liquibase, to generate a changelog, from this DB 'testing'. Is it possible?

I have an existing database already, with its tables and data inside.

jdbc:mysql://localhost:3306/testing

Now, I want Liquibase, to generate a changelog, from this DB 'testing'. Is it possible?

This is my command, but it doesn't work.

liquibase --driver=com.mysql.jdbc.Driver --classpath=C:\mysql-connector-java-5.1.47.jar 
--changeLogFile=C:\db.changelog.xml --url="jdbc:mysql://localhost:3306/testing"
--username=root generateChangeLog

I don't use any password.

The error is related to --changeLogFile=C:\db.changelog.xml

I thought, Liquibase will refer to my DB 'testing', and generate changelog, with name 'db.changelog.xml' in folder C.

Which part I'm wrong? Do I miss something?

Or maybe, Liquibase is not intended, to generate changelog, from existing DB?

Or maybe, Liquibase is intended, to generate DB, from changelog only? And not vice versa?

Meraj al Maksud
  • 1,528
  • 2
  • 22
  • 36
Hendra
  • 115
  • 3
  • 9
  • This is possible. you might be having trouble since you are writing to a file in the root of your c: drive. try `c:\temp\changelog` instead. and keep in mind that you need to have a way to increment the changelog every time you run the script. I do not recall whether it does that by writing a log to the database it reads, or requires the previous changelog file... – JoSSte Oct 20 '19 at 17:35
  • @JoSSte thank you, it works. Sorry, I don't understand this part "a way to increment the changelog every time you run the script" - why do we need to increment the changelog – Hendra Oct 20 '19 at 19:02
  • I will try and elaborate in a full answer – JoSSte Oct 21 '19 at 11:10

1 Answers1

1

This is possible. You might be having trouble since you are writing to a file in the root of your c: drive. Try c:\temp\changelog instead.

My experience is that liquibase works in increments. So if you run that command on your database, it will produce a file as if everything in the database has to be created in the changelog file (as if starting with a completely empty database).

If you read the text on Liquibase's site regarding this command, it says:

When starting to use Liquibase on an existing database, it is often useful, particularly for testing, to have a way to generate the change log to create the current database schema.

This means that if you:

  1. Execute this command once against your dev database
  2. Run the result against a new database (let's say test)
  3. Run it again on your dev database
  4. Run that file against your test database

You will get a load of errors stating that functions already exist.

I gather that the idea behind this is that you create new entries in the changelog files and executing them against ALL your databases, instead of using other tools and using liquibase for the delta.

Sample entry

<changeSet author="liquibase-docs" id="addColumn-example">
  <addColumn catalogName="cat" schemaName="public" tableName="YY">           
    <column name="xx" type="varchar(255)"/>
  </addColumn>
</changeSet>

SQL equivalent

ALTER TABLE yy ADD COLUMN xx INT
JoSSte
  • 2,953
  • 6
  • 34
  • 54
  • Do you mean that, if we use the generated changelog, to generate database, with different name, in the same server, or to generate database, with same name, in another server, it will give errors ? So, in order to solve the errors, we should create new entries in the changelog files ? I'm sorry, I also don't understand, what do you mean by "entries", related to this changelog ? Do you mean we have to rename the changelog, or give new id to each changeset, inside the changelog ? – Hendra Oct 21 '19 at 13:58
  • i mean that you have to manually insert entries into the changelog. instead of doing a `alter table YY add column xx int` add the changeset to the changelog ` ` – JoSSte Oct 22 '19 at 09:32