Creating view in DB2 LUW with Liquibase - db2

I'm using Liquibase 3.5.3 to create a view in DB2 LUW 11.0.
I set "replaceIfExists" property to true and at runtime it give me an "replaceIfExists is not allowed on db2" error.
I know that officially this property is not supported for DB2 in Liquibase but the database api allows it (I manually execute sql script with create or replace and works fine).
There is some workaround to run this scripts in DB2 or maybe can you implement this feature?
Example:
CREATE OR REPLACE myView AS (
SELECT * FROM myTable
)

I would put that script into a file and then include the file from within liquibase with the runOnChange="true" for the changeSet
<changeSet author="arthur.dent" id="42" runOnChange="true">
<sqlFile path="create_view.sql"
encoding="UTF-8"
relativeToChangelogFile="true"
stripComments="false"
splitStatements="false"/>
</changeSet>
Because of the runOnChange, Liquibase will include a checksum of the actual SQL file and will only run it if the definition of the view changed.
Another option is to use runAlways="true" instead and then the view will be re-created every time you run Liquibase. This would be necessary to automatically pick up changes in the underlying table(s).

Related

Passing parameters in sqlfile tag in liquibase 4.0

I am making a simple CI/CD pipeline in which there are some sql files that i need to run on aws RDS using liquibase. Here schema name can be different and will depend on the branch from which the code is pulled. Sample sql in createSchema.sql file :
create schema if not exist '${schemaName}'
in my changelog.xml file i am doing something like this :
<changeSet author="liquibase-docs" id="sqlFile-example">
<sqlFile
path="createSchema.sql"
splitStatements="true"
stripComments="true">
<property name="schemaName" value="test_order"/>
</sqlFile>
</changeSet>
But this thing is not working. I am also not able to find any solid info regarding to pass params in an external file in liquibase. Please let me know what do i need to do to fix it.
I use Liquibase CLI, and you can pass parameters (key=value) in from the command line:
liquibase.bat update -Dengine=myisam
More info: https://docs.liquibase.com/tools-integrations/cli/home.html

How to copy a database schema from database A to database B

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:
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.
If it's not possible with liquibase, what other tools or approaches would make this possible?
--
Let me add more context:
We initialize a new database a schema using liquibase changeset.
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.
Now, we create a new database b.
We want to import the schema of the database a to b, with people table.
I hope that is clear.
Thanks.
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.
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

export data from database in liquibase format

is it possible to export data in liquibase format from already exist postgresql database? until today i each time when i run my project in groovy grails i was using bootstrap file where i was generating everything to database. to export schemas i used grails dbm-generate-changelog and works fine. i used configure tutorial from http://grails-plugins.github.io/grails-database-migration/2.0.x/index.html
best regard!!! :-)
You can use a regular SQL Dump from your DB in the liquibase migrations with minor effort:
Create your SQL file and put it under grails-app/migrations. E.g.
grails-app/migrations/2016-03-17-002-activiti-5.19.0.2.mysql.create.engine.sql
Add that file to your changelog.groovy
Add the following preamble to the SQL file
--liquibase formatted sql
Separate your SQL file into sections you want to see as changesets (add at least one at the beginning):
--changeset activiti:5.19.0.2-create-engine
See the docs: http://www.liquibase.org/documentation/sql_format.html

Control where liquibase DATABASECHANGELOG tables are created on Z/Os

I'm testing using liquibase for DB2 on Z/Os. I have created several TEST databases running in their own table space. When I run liquibase, it applies my changes but it creates the DATABASECHANGELOG table in SYSDEFLT storage group.
Is it possible to designate where the databaseChangeLog tables are created? Instead of creating them in the SYSDEFLT storage group we would like to designate a user database for them.
Yes, you can. How you do it depends on how you run liquibase.
From the command line, either pass arguments or set the arguments in a liquibase.properties file.
The properties are liquibase.databaseChangeLogTableName and liquibase.databaseChangeLogLockTableName
If you are using other ways of running Liquibase, it will be similar.

How can i check the template for postgis in postgres ubuntu

I am following this tutorial
http://technobytz.com/install-postgis-postgresql-9-3-ubuntu.html
and i created db with this command
createdb test_db -T template_postgis2.1
but i get this error
test_db2=# select postgis_version();
ERROR: function postgis_version() does not exist
LINE 1: select postgis_version();
This works if use
create extension postgis
i want to know that is that ok or i have error. because i made the template before. Didn't that template automatically make the db as postgis
According to the official documentation on the topic, you have to create the extension in each new database you create. Why? This has to do with a change in the way a database is PostGIS-enabled in PostgreSQL-9.1+ and PostGIS-2+. Previously, there were a series of scripts that had to be run to load the functions, types, and other features of PostGIS into a database. Consequently, the best practice was to create a template database (template_postgis, etc.), run all the scripts against that template, and create each new PostGIS-enabled database against that template. In newer versions of PostgreSQL (9.1+), you can enabled PostGIS support within a new database by simply executing the command CREATE EXTENSION postgis; as such, you should skip the template step entirely.
So to sum up:
CREATE EXTENSION postgis; is the way to go for PostgreSQL-9.1+ and PostGIS-2+
Making a template database is the way to go for prior versions of PostgreSQL or PostGIS.
I hope that helps clear it up!