0
votes

I have created a Postgresql database A using liquibase changesets. Now, I'm creating an application that allows creating a new database B and copies the schema from database A in real-time including the liquibase changesets as the database can still be updated later. Note that at the time of the copied schema in database A could already be updated, making the base changesets outdated.

My main question would be:

  1. How to copy PostgreSQL schema x from database a (dynamically generated at run-time) to b using liquibase? Database b could be on another server.
  2. If it's not possible with liquibase, what other tools or approaches would make this possible?

--

Let me add more context:

  1. We initialize a new database a schema using liquibase changeset.
  2. We can add a new table and field to the database an at run-time. Or during the time when the application is running. For example, we add a new table people to the schema of database a, which is not originally in the changeset. This is done using liquibase classes too. So changeset is added to databasechangelog table.
  3. Now, we create a new database b.
  4. We want to import the schema of the database a to b, with people table.

I hope that is clear.

Thanks.

2
I don't really understand the question. If you already have the Liquibase changesets, then you can simply run them against the new schema/databasea_horse_with_no_name
Sorry about that. I'm talking at run time. I need to copy the schema created from liquibase changeset to another schema on another database. At the time of the copy the schema in database A could already be updated. Note that database B could be on another server or host.czetsuya
@czetsuya if you created schema A with Liquibase, just run the same changesets on the schema B. You'll get identical schema, but without data. If someone will create new changesets, it will be possible to run them later. If your schema is manipulated somewhere else then Liquibase, your are in trouble. Migration scripts should be the only source of truth for your DB. It is other question if your app manipulates with schema in runtime. Could you clarify this point?Anton Feoktistov

2 Answers

0
votes

All schema changes must be run through your schema migration tool

The point of using a database schema migration tool such as Liquibase or Flyway is to have a “single source of truth” regarding the structure of your database tables. Your set of Liquibase changesets (or Flyway scripts) is supposed to be that single source of truth for your database.

If you are altering the structure of you database at runtime, such as adding a table named people, outside the scope of your migration tool, well, then you have violated the rules of the game. You will have defeated the purpose of using a schema migration tool. The intention of using a schema migration tool is that you make all schema changes through that tool.

If you need to add a table while running in production, you should be dropping the physical file for the Liquibase changeset (or Flyway script) into the file system of your database server environment, and then invoking Liquibase (or Flyway) to run a migration.

Perhaps you have been misunderstanding the sequence of events:

  • If you have built a database on server "A", that means you installed Postgres, created an empty database, then installed the collection of Liquibase changesets you have carefully built, then ran a Liquibase migration operation on that server.
  • When you go to create a database on server "B", you should be following the same steps: Install Postgres, create an empty database, installing the very same collection of Liquibase changesets, and then running a Liquibase migration operation.

Alternatively, if making a copy of server "A" to create server "B", that copy should include the exact same Liquibase changesets. So at the end of your copy process, the two databases+changesets are identical.

0
votes

Here's how I solved this problem of mine using the Liquibase Java library:

1.) Export the changelog from the source database into a temporary file (XML).

Liquibase liquibase = new Liquibase(liquibaseOutFile.getAbsolutePath(), new FileSystemResourceAccessor(), sourceDatabase);
liquibase.generateChangeLog(catalogAndSchema, changeLogWriter, new PrintStream(liquibaseOutFile.getAbsolutePath()), null);

2.) Execute the temporary file to the new data source.

Liquibase targetLiquibase = new Liquibase(liquibaseOutFile.getAbsolutePath(), new FileSystemResourceAccessor(), targetDatabase);
Contexts context = new Contexts();
targetLiquibase.update(context);

Here's the complete code: https://czetsuya-tech.blogspot.com/2019/12/generate-postgresql-schema-using-java.html