How make use of liquibase-modify-column.jar in maven project? - java

<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd
http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd">
<changeSet author="Administrator" id="20" runOnChange="true">
<modifyColumn tableName="TEST_TABLE">
<column name="FIELD_NAME" type="java.sql.Types.VARCHAR(250)" />
</modifyColumn>
</changeSet>
</databaseChangeLog>
I'm new to liquibase, Can anybody help me on how to use liquibase-modify-column jar for using modifyColumn feature in liquibase 3.6.3 to fix the current problem.
cvc-complex-type.2.4.a: Invalid content was found starting with element '{"http://www.liquibase.org/xml/ns/dbchangelog":modifyColumn}'. One of '{"http://www.liquibase.org/xml/ns/dbchangelog":comment, "http://www.liquibase.org/xml/ns/dbchangelog":createTable, "http://www.liquibase.org/xml/ns/dbchangelog":dropTable, "http://www.liquibase.org/xml/ns/dbchangelog":createView, "http://www.liquibase.org/xml/ns/dbchangelog":renameView, "http://www.liquibase.org/xml/ns/dbchangelog":dropView, "http://www.liquibase.org/xml/ns/dbchangelog":insert, "http://www.liquibase.org/xml/ns/dbchangelog":addColumn, "http://www.liquibase.org/xml/ns/dbchangelog":sql, "http://www.liquibase.org/xml/ns/dbchangelog":createProcedure, "http://www.liquibase.org/xml/ns/dbchangelog":dropProcedure, "http://www.liquibase.org/xml/ns/dbchangelog":sqlFile, "http://www.liquibase.org/xml/ns/dbchangelog":renameTable, "http://www.liquibase.org/xml/ns/dbchangelog":renameColumn, "http://www.liquibase.org/xml/ns/dbchangelog":dropColumn, "http://www.liquibase.org/xml/ns/dbchangelog":mergeColumns, "http://www.liquibase.org/xml/ns/dbchangelog":modifyDataType, "http://www.liquibase.org/xml/ns/dbchangelog":createSequence, "http://www.liquibase.org/xml/ns/dbchangelog":alterSequence, "http://www.liquibase.org/xml/ns/dbchangelog":dropSequence, "http://www.liquibase.org/xml/ns/dbchangelog":createIndex, "http://www.liquibase.org/xml/ns/dbchangelog":dropIndex, "http://www.liquibase.org/xml/ns/dbchangelog":addNotNullConstraint, "http://www.liquibase.org/xml/ns/dbchangelog":dropNotNullConstraint, "http://www.liquibase.org/xml/ns/dbchangelog":addForeignKeyConstraint, "http://www.liquibase.org/xml/ns/dbchangelog":dropForeignKeyConstraint, "http://www.liquibase.org/xml/ns/dbchangelog":dropAllForeignKeyConstraints, "http://www.liquibase.org/xml/ns/dbchangelog":addPrimaryKey, "http://www.liquibase.org/xml/ns/dbchangelog":dropPrimaryKey, "http://www.liquibase.org/xml/ns/dbchangelog":addLookupTable, "http://www.liquibase.org/xml/ns/dbchangelog":addAutoIncrement, "http://www.liquibase.org/xml/ns/dbchangelog":addDefaultValue, "http://www.liquibase.org/xml/ns/dbchangelog":dropDefaultValue, "http://www.liquibase.org/xml/ns/dbchangelog":addUniqueConstraint, "http://www.liquibase.org/xml/ns/dbchangelog":dropUniqueConstraint, "http://www.liquibase.org/xml/ns/dbchangelog":customChange, "http://www.liquibase.org/xml/ns/dbchangelog":update, "http://www.liquibase.org/xml/ns/dbchangelog":delete, "http://www.liquibase.org/xml/ns/dbchangelog":loadData, "http://www.liquibase.org/xml/ns/dbchangelog":loadUpdateData, "http://www.liquibase.org/xml/ns/dbchangelog":executeCommand, "http://www.liquibase.org/xml/ns/dbchangelog":empty, "http://www.liquibase.org/xml/ns/dbchangelog":stop, "http://www.liquibase.org/xml/ns/dbchangelog":rollback, WC[##other:"http://www.liquibase.org/xml/ns/dbchangelog"], "http://www.liquibase.org/xml/ns/dbchangelog":modifySql}' is expected.
versions used: liquibase-3.6.3 and liquibase-modify-column-3.1.jar

POM.xml
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.6.3</version>
<dependencies>
<dependency>
<groupId>org.liquibase.ext</groupId>
<artifactId>liquibase-modify-column</artifactId>
<version>3.1</version>
</dependency>
</dependencies>
</plugin>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd
http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.6.xsd">
<changeSet author="Administrator" id="20" runOnChange="true">
<ext:modifyColumn tableName="TEST_TABLE">
<column name="FIELD_NAME" type="java.sql.Types.VARCHAR(250)" />
</ext:modifyColumn>
</changeSet>
</databaseChangeLog>
adding liquibase-modify-column.jar in liqubase dependency and using
ext:modifyColumn </ext:modifyColumn>
will make to use modifyColumn functional with latest liquibase
https://forum.liquibase.org/t/custom-preconditions-as-an-extension-not-working/1428

<modifyColumn> is not allowed where you placed it. As far as I can see in the docs <modifyColumn> is no valid data type at all for liquibase. Instead you'll have to put any of the tags the error message mentions. You should check https://docs.liquibase.com/change-types/home.html for a list of valid Change Types that can be used under <changeSet>. Based on what you put into your <modifyColumn> tag I guess you're looking for the modifyDataType.

Related

“Time(3)” type on Liquibase with MySQL 8

I'm facing a problem using Liquibase with MySQL 8 where the following script is not putting the fraction part of type "time(3)", it only puts "time" on the type of the column. We run this script before with MySQL 5 and it worked fine.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.16.xsd"
logicalFilePath="20220901.xml">
<changeSet author="MyUser" id="Change column 'time' to Datatype to milliseconds">
<modifyDataType
columnName="time"
newDataType="TIME(3)"
schemaName="${defaultSchema}"
tableName="table1"/>
<addNotNullConstraint
columnDataType="TIME(3)"
columnName="time"
schemaName="${defaultSchema}"
tableName="table1" />
</changeSet>
</databaseChangeLog>
I tried to update to most recent versions on maven dependencies of liquibase.core(to 4.16.1) and mysql-connector-java(to 8.0.30), the problem persists.
After multiple tests, i discover that the problem may be on liquibase generated query that not includes the fraction part "(3)", so as a workaround I used "modifySql" to change the query at the end.
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.16.xsd"
logicalFilePath="20220901.xml">
<!-- WORK-AROUND - Liquibase was generating a query with type 'TIME' instead of 'TIME(3)' so
we use 'REPLACE_WITH_TIME' as auxiliary type to replace all of it's occurrences in query
by 'TIME(3)' with 'modifySql'. -->
<changeSet author="MyUser"
id="Fix time column type to time(3) - 2022-10-06">
<modifyDataType columnName="time"
newDataType="REPLACE_WITH_TIME" schemaName="${defaultSchema}"
tableName="table1" />
<addNotNullConstraint columnDataType="REPLACE_WITH_TIME"
columnName="time" schemaName="${defaultSchema}"
tableName="table1" />
<modifySql>
<replace replace="REPLACE_WITH_TIME" with="TIME(3)" />
</modifySql>
</changeSet>
</databaseChangeLog>
It resolves the problem but its not the best solution.
So i wanted to ask if anybody noticed that and knows if its actally a liquibase bug or not.
Thanks in advance.

why Liquibase does not create my table in postgres when spring boot microservce is started?

Why When i run my project which is with Java 8, Spring Boot, Liquibase and Postgresql, i do not see any new table in my postgres database?
I installed PostgreSQL 11.6,
This is changelog-master.xml:
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<include file="/db/changelog/changes/create-table-changelog-1.xml"/>
</databaseChangeLog>
This is create-table-changelog-1.xml
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
<changeSet author="admin" id="1">
<createTable tableName="person11">
<column autoIncrement="true" name="id" type="INT">
<constraints primaryKey="true"/>
</column>
<column name="name" type="VARCHAR(255)">
<constraints nullable="false"/>
</column>
<column name="address" type="VARCHAR(255)"/>
</createTable>
<!-- <rollback>
<dropTable tableName="person11"/>
</rollback>-->
</changeSet>
</databaseChangeLog>
This is application.properties:
spring.liquibase.changeLog = classpath:/db/changelog/changelog-master.xml
spring.datasource.url= jdbc:postgresql://localhost:5432/
spring.datasource.username=postgres
spring.datasource.password=postgres
This is pom.xml file:
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>3.8.9</version>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.14</version>
<scope>compile</scope>
</dependency>
I think you should also specify the database name at the end of your datasource url, like this jdbc:postgresql://localhost:5432/DB_NAME
You must ensure to have an implementation of JPA in order for JPA to interact with the database. I usually use the dependency spring-boot-starter-data-jpa which comes with hibernate, add the dependency in the pom file.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
You may need to add the following dependency in your pom.xml as well:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
And if you are using Spring Boot 2, then you would also need to change the property name to spring.liquibase.change-log in your application.properties file.
Thank you all for your answers,
When i followed this link, my issue is solved. In this link there is an example for integrating Spring-Boot, JPA and Liquibase:
https://auth0.com/blog/integrating-spring-data-jpa-postgresql-liquibase/

after first time myBatis generator getting lots of "cannot be resolved to a type"

launched for first time myBatis generator using this instructions:
https://mybatis.org/generator/running/runningWithEclipse.html
this is my generatorConfig.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<classPathEntry location="C:/***/ojdbc6.jar"/>
<context id="context">
<jdbcConnection
connectionURL="jdbc:oracle:thin:***:***/***"
driverClass="oracle.jdbc.driver.OracleDriver"
password="***"
userId="***" />
<javaModelGenerator
targetPackage="***.model"
targetProject="***-dao" />
<javaClientGenerator
targetPackage="***.mapper"
targetProject="***-dao"
type="XMLMAPPER" />
<table
schema="***"
tableName="USERS">
</table>
</context>
</generatorConfiguration>
In eclipse - run as - run myBatis genarator.
Got what I guess is the right code generated, in the right project and package.
Anyway:
Users.java --> no issues
UsersDynamicSqlSupport.java --> lots of erros, for example "SqlColumn cannot be resolved to a type"
UsersMapper.java --> lots of erros, for example "BasicColumn cannot be resolved to a type"
in my pom I have (related to myBatis) these dependencies:
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-cdi</artifactId>
<version>1.1.0</version>
</dependency>
maybe I'm missing something else in pom?
Or other problems, maybe some missing or bad configs in generatorConfig?
after a little of search on internet I found the jar in which are defined those classes, then searched on mvn and found the right dependency:
<!-- https://mvnrepository.com/artifact/org.mybatis.dynamic-sql/mybatis-dynamic-sql -->
<dependency>
<groupId>org.mybatis.dynamic-sql</groupId>
<artifactId>mybatis-dynamic-sql</artifactId>
<version>1.1.4</version>
</dependency>

MyBatis Generator include Views

Is there a way to include views on MyBatis Generartor? I am using v1.3.7.
I already look on the MyBatis Generator official documentation but nothing was mentioned there.
https://mybatis.org/generator/
I have stumbled upon hotrod but not sure what will be the impact on our existing framework.
For reference, I am using Postgres 10.10, Mybatis Generator 1.4.0. Below is my mybatis generator config and pom.
MyBatis Generator XML Config:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN" "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">
<generatorConfiguration>
<context id="context1">
<jdbcConnection
connectionURL="jdbc:postgresql://127.0.0.1:5432/test_database"
driverClass="org.postgresql.Driver" password="MyPassword"
userId="postgres" />
<javaModelGenerator
targetPackage="com.test.model"
targetProject="src/main/gen" />
<sqlMapGenerator
targetPackage="com.test.xml"
targetProject="src/main/gen" />
<javaClientGenerator
targetPackage="com.test.mapper"
targetProject="src/main/gen" type="XMLMAPPER" />
<table schema="schema1" tableName="%" delimitIdentifiers="true">
</table>
</context>
</generatorConfiguration>
POM Plugin:
<plugin>
<groupId>org.mybatis.generator</groupId>
<artifactId>mybatis-generator-maven-plugin</artifactId>
<version>1.3.7</version>
<dependencies>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>9.4-1204-jdbc4</version>
</dependency>
</dependencies>
</plugin>
Any advice will be appreciated.

Liquibase refactor or undo changeset in multiple regions

I am new to liquibase and have created a sample table utilizing a spring boot + liquibase project. My initial changelog to create the table in file 'createSampleTable.xml':
<?xml version="1.0" encoding="UTF-8"?>
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.7"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.7
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.7.xsd">
<changeSet id="1" author="Russ">
<comment>A sample table to see if liquibase works</comment>
<createTable tableName="testy">
<column name="VALUE" type="varchar(32)"/>
</createTable>
<insert tableName="testy">
<column name="value" value="Hello, world!"/>
</insert>
<insert tableName="testy">
<column name="value" value="Riddikulus!"/>
</insert>
</changeSet>
</databaseChangeLog>
Now that I've verified my liquibase configuration, this same deployment has run in our 2 lower regions (dev and test) but we have not yet stood up stage or prod. I would like to "undo" the sample table and start creating my real database structure.
My understanding is I have two options: conditional drop table, rollback
I am currently trying to implement the conditional drop table as the documentation states but the proposed attributes are not recognized even though the preconditions documentation clearly states the onFail annotation should be recognized. Here is my implementation of the proposed solution (this is the current contents of the 'createSampleTable.xml' file:
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog/1.7"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog/1.7
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-1.7.xsd">
<!-- The documentation for liquibase says if you ever create a changeset
in error that you never really wanted to follow this approach.
The general idea is to delete the original changeset and then
provide a new changeset that should only run if the original changset
has run as well. In essence this allows us to remove a naughty change
entirely from the code and prevent it from being run in new
environments while providing an "undo" changeset to be ran in
environments where this changeset has unfortunately already ran.
Documentation referenced and approach taken:
http://www.liquibase.org/2008/10/dealing-with-changing-changesets.html
To satisfy developer's curiosity and prevent them from having to
look up the history in the repository the original changeset of id=1
was simply to create a sample table to make sure the initial liquibase
config was working.
-->
<changeSet id="1-undo" author="Russ">
<preConditions onFail="MARK_RAN">
<changeSetExecuted id="1" author="Russ" changeLogFile="liquibase/createSampleTable.xml" />
</preConditions>
<dropTable tableName="testy"/>
</changeSet>
</databaseChangeLog>
However, when running this both the onFail attribute and the
<changeSetExecuted> tag are unrecognized by the schema. After this I tried to implement the maven plugin to rollback, but this executes on the build, so this target will only ever resolve one region.
What is the generally accepted approach for undoing changes? Does the approach differ if you're implementing a spring boot liquibase project?
Looks like your applied schema is quite old (1.7).
Check your included version (if Maven, check your pom).
You should be able to use the required tags if you use the 3.1 version xml schema:
<databaseChangeLog
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd">
...
</databaseChangeLog>
Note the schema dbchangelog-3.1.xsd
If using Maven, the above schema works with the following plugin config:
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<propertyFile>src/main/liquibase.properties</propertyFile>
</configuration>
</plugin>

Categories

Resources