1672066691
In this blog, we look at database migrations with the very popular Liquibase database migration library and how you can use it in the context of a Spring Boot application.
By using default Spring Boot auto-configures Liquibase while we upload the Liquibase dependency to our construct document.
Spring Boot makes use of the primary DataSource to run Liquibase (i.e. the only annotated with @primary if there is a couple of). In case we need to apply a special DataSource we can mark that bean as @LiquibaseDataSource.
rather, we are able to set the spring. liquibase.[URL, user, password]properties, in order that spring creates a Datasource on its personal and uses it to automobile-configure Liquibase.
Through default, Spring Boot runs Liquibase database migrations mechanically on application startup.
It looks for a grasp changelog report in the folder DB/migration within the classpath with the name DB.changelog-master. YAML. If we need to use other Liquibase changelog codecs or use distinctive report naming conventions, we are able to configure the spring. liquibase. exchange-log application property to point to an exclusive grasp changelog record.
for example, to use db/migration/db.change-log.json because of the master changelog document, we set the following assets in application.yml:
liquibase.change-log=classpath:db/migration/db.changelog-master.xml
The master changelog can consist of different changelogs so that we can break up our modifications into logical steps.
After setting the whole lot up, permit’s create our first database migration. We’ll create the database desk user_details in this example.
Let’s initiate a file with the term db.changelog-master.yaml
and place it in src/main/resources/db/changelog
:
<databaseChangeLog
<include file="db/changelog/db.changelog-1.0.0.xml"/>
</databaseChangeLog>
The master file is simply a cluster of includes that implies changelogs with the authentic changes.
Next, we build the changelog with the first real changeset and placed it into the file src/main/resources/db/changelog-yaml-example.yaml
:
<databaseChangeLog>
<changeSet author="Krishna (generated)" id="1503460396396-1">
<createTable tableName="employee_table">
<column autoIncrement="true" name="employee_id" type="INT">
<constraints primaryKey="true" />
</column>
<column name="email" type="VARCHAR(255)" />
<column name="employee_name" type="VARCHAR(255)" />
<column name="salary" type="DOUBLE" />
</createTable>
</changeSet>
<changeSet author="Kishank (generated)" id="1503460396396-2">
<createIndex indexName="EMAIL_INDEX"
tableName="employee_table">
<column name="email" />
</createIndex>
</changeSet>
<changeSet id="1203460396356-3" author="Krishna (generated)">
<insert tableName="employee_table">
<column name="employee_name" value="Deepak"></column>
<column name="email" value="rock.deep@yahoo.com"></column>
<column name="salary" valueNumeric="85000.00"></column>
</insert>
<insert tableName="employee_table">
<column name="employee_name" value="Manish"></column>
<column name="email" value="manish.s@yahoo.com"></column>
<column name="salary" valueNumeric="90000.00">
</column>
</insert>
</changeSet>
</databaseChangeLog>
As defined earlier, context can be used to control which exchange units have to run. let’s use this to add take a look at the information in the check and local environments:
<databaseChangeLog>
<changeSet
author="Krishna Jaiswal"
id="1503460396396-1"
context="test or local">
<loadUpdateData
encoding="UTF-8"
file="db/data/employee.csv"
onlyUpdate="false"
primaryKey="id"
quotchar="'"
separator=","
tableName="emp_details">
</loadUpdateData>
</changeSet>
</databaseChangeLog>
We’re using the expression test or local so it runs for these contexts, but not in production.
So now we need to pass the context to Liquibase using the property spring.liquibase.contexts
:
spring.profiles: docker
liquibase.parameters.textColumnType: TEXT
contexts: test
To create a new migration, you could run the make: migration Artisan command, and on the way bootstrap a new class to your Laravel utility, in the database/migrations folder.
create table employee_table (
employee_id integer not null auto_increment,
email varchar(255),
employee_name varchar(255),
salary double precision,
primary key (employee_id)
);
Allowing info degree logging for Liquibase will assist to peer the trade sets that Liquibase executes for the duration of the beginning of the application. It additionally enables us to identify that the utility has no longer commenced yet due to the fact it’s miles waiting to collect a changelog lock at some stage in the startup.
upload the subsequent application belongings in application.yml to allow info logs:
logging:
level:
"liquibase" : info
Liquibase enables automating database migrations, and Spring Boot makes it less complicated to apply Liquibase. This manual furnished info on a way to use Liquibase in the Spring Boot software and a few best practices.
https://blog.nimbleways.com/db-migrations-with-liquibase-and-spring-boot/
You can find the example code on GitHub.
Original article source at: https://blog.knoldus.com/
1672062840
In this Blog, we will see an example of evolving database using Spring Boot and Liquibase with YAML and SQL configuration. Here we will learn how to build applications using maven build tools.
Liquibase is an open-source library for tracking, managing, and deploying database changes that can be used for any database. It helps you create the schema, run them during deployment, and also helps you write automated tests so that your changes will work in production.
We need to create maven based project.
If you are creating maven based project then add the following dependencies.
Create a YAML file called db.changelog-master.yaml under the src/main/resources/db folder. It will cover all the changelogs written in different files. The complete master changelog file content is given below:
Notice in the above YAML file, we have not specified any endDelimiter
for changeSet ids insertTableAddresses and insertTableUsers because the default end delimiter is ;
.
Now create below SQL file 01-create-users-and-addresses-schema.sql under src/main/resources/db/changelog/scripts to create tables in the MySQL database:
NowcreatebelowSQLfile 02-insert-data-addresses.sql under src/main/resources/db/changelog/scripts folder to insert data into the ADDRESSES table:
Now create below SQL file 02-insert-data-users.sql under src/main/resources/db/changelog/scripts to insert data into the USERS table:
Create file src/main/resource/application.properties to load the database changelog file during Spring Boot application startup. Here we configure the database connection in this file.
The default location of the changelog master file is classpath:/db/changelog and Liquibase searches for a file db.changelog-master.yaml. So we need to declare the location of the changelog-master file.
In the Spring Boot application the key liquibase.change-log
does not work, so you need to use spring.liquibase.changeLog
.
Here is the main application in order to start the application and the creation and insertion of the above table into the database will be occurring during the application execution.
After running the project it will have the following output:
You will find tables created in the database. The two rows are inserted into addresses and two rows are inserted into users’ tables. Also, there are three rows inserted into the table DATABASECHANGELOG and the row identifies all details about the executed file. We will also find one row inserted into the table DATABASECHANGELOGLOCK and this row identifies whether the current operation holds a lock on changesets or not.
Original article source at: https://blog.knoldus.com/
1671815460
In the previous Liquibase blog we saw the essential introduction to Liquibase and its advantages. Now, let’s move to the second step of the series in which we will see the “Best Practices for Managing Liquibase ChangeLogs”.
ChangeLogs are an ordered list of changes to be deployed to the database. You can also try to mix and match the different changelog formats according to your requirements. There was a poll conducted by people at liquibase on LinkedIn to see which changelog format is preferred the most –
SQL and XML became the most preferred and popular changelog formats. Regardless of what format you are using, are all structured the same way.
The basic structure that is being followed for all the formats is –
There are several types of changelogs that can be used in Liquibase, including XML, JSON, YAML, and SQL. XML is the most commonly used changelog format, but you can choose the format that works best for your needs.
XML ChangeLogs – XML changelogs are written using the Liquibase XML format, which is a standardized way of defining database changes. This format allows you to define changes using a series of XML tags, such as <createTable>
and <addColumn>
. XML changelogs are easy to read and understand, and they are a good choice if you want to write your own changelogs by hand.
JSON ChangeLogs – JSON and YAML changelogs are similar to XML changelogs, but they use different syntaxes. YAML and JSON changelogs are written using a key-value format, which makes them easy to read and understand. JSON and YAML changelogs are good choices if you want to use a tool to automatically generate your changelogs.
SQL ChangeLogs – SQL changelogs are written using standard SQL statements. This means that you can use your existing SQL knowledge to write changelogs, and you can also use tools like MySQL Workbench to generate SQL changelogs. SQL changelogs are a good choice if you want to use your existing SQL skills to manage your database changes.
Changelogs are a crucial component of Liquibase since they give you the ability to monitor and control the alterations you make to your database structure. A changelog is a file that lists all the modifications made to your database, along with the SQL commands used to implement those modifications. As a result, it is simple to view the history of modifications made to your database and, if required, roll those changes back. Changelogs also assist in ensuring that your database is consistently in a known and consistent state, which is critical for preserving the integrity of your data.
Interested to know more about LiquiBase, please refer to the official documentation of LiquiBase.
To read more tech blogs, please refer to Knoldus Blogs.
Original article source at: https://blog.knoldus.com/
1670501880
Liquibase is an open-source database-independent library for tracking, managing, and applying database schema changes. Basically, it works with various types of databases and supports various file formats for defining the database structure. The feature that is probably most useful in Liquibase is its ability to roll changes back and forward from a specific point and save you from needing to know what was the last change you ran on a specific database instance.
Liquibase is a Java-based tool integrated with Maven and Spring Boot and now we need to create a spring boot project using Spring Initializr.
After the configuration in the project structure under src/main/resources, we will see that we have a package with the name db and inside there is another package with the name changelog, Now we will add all changelogs.
Here, we need to create the changelog master file as db/changelog-master.xml and it will track all changes.
This is the content of the file:
After that just configure that file in the application.properties file as follows:
spring.liquibase.change-log=classpath:db/changelog/db.changelog-master.xml
Now, Let’s make our first migration in which we will create a new table name as a student which will have id, first name, and last name.
Create a file in db/changelog and name it db-changelog-create_student_table-1.0.sql.
That’s it, now let’s run the application.
If we inspect the database changelog table and we will see our first migration as follow :
We learned how easy and useful Spring Boot Liquibase is. When you are starting a new project this is a database versioning tool that will help in managing the database schema and speed up the productivity of the team and if we have an old project we can still integrate it easily. So, This blog provided details on how to use Liquibase in the Spring Boot application.
https://en.wikipedia.org/wiki/Liquibase
Original article source at: https://blog.knoldus.com/
1670327109
New to Liquibase? Welcome! This blog will explain the essential introduction to Liquibase and its Advantages.
Liquibase is an open-source database-independent library for tracking, managing, and applying database schema changes.
Before we go into detail about Liquibase, let us see the common problems we face as a developer while working with the application and the database scripts. Now, as a developer, we have version control tools for maintaining the application code but we do not have any tools which actually maintain the version control for your database changes. That’s the reason, once you have promoted the application code to the higher environment, sometimes the developer may further go to execute the database scripts which actually have the dependency for that particular application.
That is the reason we are going to face some problems when we are promoting our application code to the production environment. So to eradicate such kind of problems with respect to the database scripts, we are introducing a tool called Liquibase.
Liquibase will actually act as version control for your database Scripts. Liwuibase is a source control tool for your database. It also supports branching and merging. Like other versioning tools, it supports multiple developers working on it at the same time.
Liquibase is a source control version tool for your database scripts.
Liquibase basically works based on the log element that is the Database change log. The database change log file can be in form of XML/JSON/YAML/SQL. Now once you have prepared the database change logs, now you have to move to the changeset. ChangeSet is nothing but SQL that contains the changes that are to be done in the database.
ChangeSet takes two fields ie. id and author. If the id and author match the actual database then further operations are carried out otherwise it throws an exception.
Sample change log file for XML format:
<?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"
xmlns:pro="http://www.liquibase.org/xml/ns/pro"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-latest.xsd
http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd
http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-latest.xsd">
<preConditions>
<runningAs username="liquibase"/>
</preConditions>
<changeSet id="1" author="nvoxland">
<createTable tableName="person">
<column name="id" type="int" autoIncrement="true">
<constraints primaryKey="true" nullable="false"/>
</column>
<column name="firstname" type="varchar(50)"/>
<column name="lastname" type="varchar(50)">
<constraints nullable="false"/>
</column>
<column name="state" type="char(2)"/>
</createTable>
</changeSet>
<changeSet id="2" author="nvoxland">
<addColumn tableName="person">
<column name="username" type="varchar(8)"/>
</addColumn>
</changeSet>
<changeSet id="3" author="nvoxland">
<addLookupTable
existingTableName="person" existingColumnName="state"
newTableName="state" newColumnName="id" newColumnDataType="char(2)"/>
</changeSet>
</databaseChangeLog>
Sample change log file for JSON format:
{
"databaseChangeLog": [
{
"preConditions": [
{
"runningAs": {
"username": "liquibase"
}
}
]
},
{
"changeSet": {
"id": "1",
"author": "nvoxland",
"changes": [
{
"createTable": {
"tableName": "person",
"columns": [
{
"column": {
"name": "id",
"type": "int",
"autoIncrement": true,
"constraints": {
"primaryKey": true,
"nullable": false
},
}
},
{
"column": {
"name": "firstname",
"type": "varchar(50)"
}
},
{
"column": {
"name": "lastname",
"type": "varchar(50)",
"constraints": {
"nullable": false
},
}
},
{
"column": {
"name": "state",
"type": "char(2)"
}
}
]
}
}
]
}
},
{
"changeSet": {
"id": "2",
"author": "nvoxland",
"changes": [
{
"addColumn": {
"tableName": "person",
"columns": [
{
"column": {
"name": "username",
"type": "varchar(8)"
}
}
]
}
}
]
}
},
{
"changeSet": {
"id": "3",
"author": "nvoxland",
"changes": [
{
"addLookupTable": {
"existingTableName": "person",
"existingColumnName": "state",
"newTableName": "state",
"newColumnName": "id",
"newColumnDataType": "char(2)",
}
}
]
}
}
]
}
In the same way, you can provide any number of the changelog. To know more about change log patterns you can simply visit the Official Liquibase change log documentation.
Once you are done with preparing the database changelogs, now liquibase needs to identify which type of database you want to execute these scripts. Now in order to identify the database type, whenever we are using a maven/Gradle, you are going to define what type of database it is in the configurations/properties file.
You will define what type of database it is, JDBC Driver, the DB URL, and the password. Once the database is identified, it looks for the database changelogs and identifies the changeset. Now it will prepare the SQL statements according to the identified database and the properties given in the JSON/XML etc file.
Whenever it is executing the scripts, if it is the first time it is executing then liquibase will create two tables and maintain the version control for your database changes.
The first table will be DATABASECHANGELOG, it will contain whatever scripts you are executing. The second table is DATABASECHANGELOGLOCK, it is basically used to maintain the atomic transactions i.e only one person at a time can modify the database scripts at a time.
The next time you execute any database scripts, the tables are not created from the scratch. Only updation will happen in the already created tables.
This is all about the working of Liquibase.
After knowing a briefly about liquibase, we can say that liquibase is beneficial to use to keep the database version control in check, to generate various reports and many more advantages.
To read more about Liquibase, please stay tuned and keep visiting Knoldus Blogs.
Reference links – Liquibase Documentation
Original article source at: https://blog.knoldus.com/
1670050140
Liquibase is one of the most protean tools for database migration. It operates on almost all SQL database outlets. So whether you use MySQL, SQL Garçon, Oracle, Firebird, or a combination of these or any other common database platforms, it’s got you covered. It also runs in any CI/ CD channel so long as you can run Java, install the connectors, and connect to your databases. This means you can use Liquibase to emplace database changes along with your new features that are released behind point toggles. It takes a bit of planning — especially when you are releasing clashing or destructive changes. Luckily, Liquibase has some mechanisms to make database changes less discouraging. It functions all the DDL procedures and several veritably usable refactorings. occasionally you have to be careful about when you run those database changes on your product cases since they can lock your tables for quite a while. In this post, you will learn how to use Liquibase to make all effects database migration easier.
<databaseChangeLog> <changeSet id=”1 author=”Chetan Bhagat”>″
<createTable tableName=”event”>
<column name=”id” type=”int”>
<constraints primaryKey=”true” nullable=”false”/>
</column>
<column name=”name” type=”varchar(50)”>
<constraints nullable=” false”/>
</column>
<column name=”active” type=”boolean” defaultValueBoolean=”true”/>
</createTable>
</changeSet>
<changeSet author=”sought” id=”tag database-example”>
<tagDatabase tag=”0.1 />″
</changeSet>
</databaseChangeLog>
<changeSet id=”changeRollback” author=”Chetan Bhagat”>
<createTable tableName=”event”>
<column name=”id” type=”int”/>
</createTable>
<rollback>
<dropTable tableName=”event”/>
</rollback>
</changeSet>
- For generating change log files from an existing database, we used the command line tool.
- Run ./liquibase –changeLogFile=changelog.xml generateChangeLog
- Here is an example of the Liquibase command:
./liquibase --changeLogFile=data.xml --diffTypes=data generateChangeLog
Be ready for OutOfMemoryError
Best Practices Some of the best practices we follow are as mentioned below:
• Organize Change Log File
• We organize our application version files in separate changeLog files and we have a master ChangeLog File.
<?xml version=”1.0 encoding=”UTF-8 ?>″ ″ <databaseChangeLog> <include
file=”com/myapp/db/changelog/db.changelog-1.0.0.xml”/> <include
file=”com/myapp/db/changelog/db.changelog-1.0.1.xml”/> <include
file=”com/myapp/db/changelog/db.changelog-1.0.2.xml”/> </databaseChangeLog>
• Change Set ID
• Use Liquibase from the initial stage of development.
Liquibase can smoothly be ingrained and managed through its Java APIs.
spring.liquibase.enabled=true
spring.application.name=sample-liquibase-app
spring.liquibase.change-log=classpath:db/changelog/changelog-master.xml
spring.datasource.url=jdbc:mysql://localhost:3306/liquibase
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.username=root
spring.datasource.password=pass
./liquibase <command>
In this Blog, we demonstrated different ways to use Liquibase for different database changes.
To learn more about Manage Your Database Schema, you can visit this link.
To read more tech blogs, feel free to visit Knoldus Blogs.
Original article source at: https://blog.knoldus.com/
1670042428
The purpose of this blog is to show you the process of using Liquibase as a piece of your Spring Boot workflow. Springboot makes it easy to create standalone, production-maven Spring-based applications.
Liquibase is an open-source database that has an independent library for tracking, managing, and applying database schema changes. Liquibase was started in 2006 and it is used to allow easier tracking of database changes, especially in an agile software development environment.
Make sure that you have Java Development Kit (JDK 8, 11, or 16).
Use Liquibase with spring-boot to create and configure single spring applications and automate your database updates. Spring-boot with build systems like Maven permits you to create Java applications initiated by running java -jar or war deployments.
The Liquibase Spring Boot integration makes the application database updated together with the application code by applying Spring Boot auto-configuration and features.
To use Liquibase and Spring Boot:
pom.xml
.Insert the below information in Spring Initializr.
After selecting the above options, the project window needs to look similar to the below screenshot:
Finally, Click GENERATE to upload your project template as a zip file. After you require to extract it and open it in your IDE.
spring.datasource.url=jdbc:mysql://localhost:3306/liquibase;
DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username = ${dbName}
spring.datasource.password = ${dbPass}
spring.liquibase.change-log=classpath:db/changelog/db.changelog-master.xml
pom.xml
based on the example:<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.4</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.knoldus</groupId>
<artifactId>spring-boot-liquibase-example</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-liquibase-example</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
<?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"
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog
http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-3.1.xsd">
<changeSet id="01" author="Sankata">
<createTable tableName="books"
remarks="A table to contain all books">
<column name="id" type="int" autoIncrement="true">
<constraints nullable="false" unique="true" primaryKey="true"/>
</column>
<column name="name" type="varchar(255)">
<constraints nullable="false" unique="true"/>
</column>
<column name="author" type="int">
<constraints nullable="false"/>
</column>
</createTable>
<createTable tableName="authors"
remarks="A table to contain all the authors">
<column name="id" type="int" autoIncrement="true">
<constraints nullable="false" primaryKey="true"/>
</column>
<column name="name" type="varchar(100)">
<constraints nullable="false"/>
</column>
</createTable>
<addForeignKeyConstraint baseTableName="books" baseColumnNames="author"
constraintName="author_fk"
referencedTableName="authors" referencedColumnNames="id"/>
</changeSet>
</databaseChangeLog>
mvn compile package
Original article source at: https://blog.knoldus.com/
1669873993
First, Liquibase is an open-source database schema change management tool that makes it simple for you to handle database change revisions.
Regardless of your database platform, changes are defined in a platform-neutral language. In essence, you maintain a running list of modifications. And, Liquibase uses its execution engine to make those modifications for you. It requires the appropriate JDBC driver to update the database because it runs on Java. You must also have the most recent JRE installed, of course. You can run Liquibase manually or from any deployment pipeline because it operates from a shell or command line. To maintain consistency and prevent corruption due to wrongly modified changelogs, Liquibase tracks changes using its own tables in your schema. To prevent you from mistakenly executing two updates at once, it “locks” your database while operating.
The changeLog files are the foundation of Liquibase usage. A changelog file is an XML document that records every change that needs to be made to update the database. Tag is parsing when we run the Liquibase migrator, the databaseChangeLog>. We can add changeSet> tags to the databaseChangeLog> tag to organize database changes. The ‘id’ and ‘author’ attributes as well as the name of the changelog file classpath serve to identify each changeSet specifically.
Example:
<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-3.5.xsd">
</databaseChangeLog>
We will examine how to edit the database to add, update, and remove tables, columns, and data. Now that we have a simple changeLog file. By the principle of “one change per changeSet,” it is counseles to create a new changeSet for each distinct insert, update, and delete action. As a result, we use Liquibase to execute a version-based database migration while updating an existing database.
For the most part, you require some fundamental Data Definition Language (DDL) functions to define data structures in databases.
1) Create schema: Since Liquibase is planned to manage objects within the application’s schema, there is no “create schema” tag. However, we can incorporate the creation of a schema into our migration by using a unique SQL statement inside a “SQL” tag.
<changeSet author="asdf" id="1234">
<sql dbms="h2" endDelimiter=";">
CREATE SCHEMA schema
</sql>
</changeSet>
2) Create Table: The following code is added to the databaseChangeLog tag when a table is created:
<changeSet author="asdf" id="1234">
<createTable tableName="newTable">
<column type="INT" name="newColumn"/>
</createTable>
</changeSet>
3) Drop Table: We must mention the table’s name and the schema when deleting a table. We also remove all constraints referring to primary and unique keys in the drop table when cascadeConstraints are set to true. It will after delete the corresponding records in the child table or tables. The database will return an error and won’t drop the table if there is a referential integrity constraint but we don’t set the cascadeConstraints to true.
<changeSet author="asdf" id="1234">
<dropTable tableName="newTable" schemaName="public"
cascadeConstraints="true"/>
</changeSet>
4) Change existing data structure with alter table: The table can be changed by adding, renaming, and dropping columns as well as changing the data type. To change the table’s name, use the tag renameTable.
4) a. Rename Table: We must specify the new table name, the old table name, and the schema name inside the tag “renameTable”.
<changeSet author="asdf" id="1234">
<renameTable newTableName="newName" oldTableName="table"
schemaName='schema'/>
</changeSet>
4) b. Rename Column: A column’s data type, new column name, old column name, schema name, and table name must all be provided to rename a column.
<changeSet author="asdf" id="1234">
<renameColumn columnDataType="varchar(255)" newColumnName="newColumn"
oldColumnName="column" schemaName="schema" tableName="table"/>
</changeSet>
4) c. Add Column: The schema name, table name, and the name and type of the new column must be included in the inner tag <column> of the tag <addColumn>.
<changeSet author="asdf" id="1234">
<addColumn schemaName="schema" tableName="table">
<column name="newColumn" type="varchar(255)"/>
</addColumn>
</changeSet>
4) d. Drop column: Column name, table name, and schema name must all be specified to delete a column.
<changeSet author="asdf" id="1234">
<dropColumn columnName="column" tableName="table", schemaName="schema"/>
</changeSet>
4) e. Modify the data type: We need the column name, new data type, schema name, and table name when changing a data type.
<changeSet author="asdf" id="1234">
<modifyDataType columnName="column" newDataType="int" schemaName="schema"
tableName="table"/>
</changeSet>
To configure and run Liquibase with Maven, we need to add the following configuration to our pom.xml file:
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>x.x.x</version>
</dependency>
Maven also enables us to automatically generate a changelog file from:
mvn liquibase:generateChangeLog
mvn liquibase:diff
In conclusion, in this blog, we have learned about how can we manage databases with the help of Liquibase. I will be covering more topics on Liquibase in my future blogs, stay connected. Happy learning
For more, you can refer to the Liquibase documentation: https://docs.liquibase.com/home.html
For a more technical blog, you can refer to the Knoldus blog: https://blog.knoldus.com/
Original article source at: https://blog.knoldus.com/
1669802722
With Liquibase, you can specify the database modification you desire using SQL or a number of other database-independent forms, such as XML, YAML, and JSON. It is incredibly simple for developers to submit updates to many database types by abstracting the database logic.
The changeLog file is how Liquibase functions most frequently. One or more changeSets are contained in this text file. A hierarchy of files is created when a changeLog contains further changeLogs.
A changeSet is the basic unit of change for a database. The change instruction itself and the related metadata are the two types of data that make up a changeSet. Although an instruction may contain one or more atomic changes (such as adding a table, changing a column, adding a constraint, inserting rows, etc.), when it is executed, it treats all of those changes as belonging to a single changeSet.
Liquibase uses the DATABASECHANGELOG table instead of a difference-based method because of a number of advantages. The most important aspect of database schema changes is their meaning. There are several techniques to change the database schema. Some of them could modify or modify related data. It allows for the specific procedure by which alterations are to be performed to be described.
Another aspect preventing Liquibase from being built on differences is performance. It is unable to compare data rapidly enough to make modifications because of large databases.
As Liquibase is a cross-platform application, it can be used with Linux, Windows, or Mac OS. Both the workstation and the server can use it.
It was created using Java. Java 1.6 or later is necessary for Liquibase’s most recent version (3.5.3).
JDBC driver is used to establish a connection to the database. For instance, the Oracle Database 12.1.0.1 JDBC Driver can be utilized with Oracle Database Express Edition 11g Release.
One of Liquibase’s numerous benefits is that it is compatible with a variety of database types. The compatible databases are listed in the following table.
By utilizing extensions, Liquibase may support different database architectures.
Liquibase can be run in a number of different methods, such as an Apache Ant task or a direct Java call, but the command line is the method that is most frequently used because it allows for full Liquibase functionality.
ChangeLogs can be written in many different formats. Liquibase supports the following file formats.
The fact that the XML, YAML, and JSON formats enable all Liquibase functionalities is their main benefit. There are several restrictions associated with using the SQL format, such as the absence of automatic rollback generation.
The ability to undo modifications is one of Liquibase’s most practical important features. This can be carried out automatically or with the aid of a predefined SQL script.
It is possible to use the automatically created rollback for commands like “CREATE TABLE,” “CREATE VIEW,” “ADD COLUMN,” and others. However, some changes, like “DROP TABLE,” cannot be reversed. In that situation, rollback must be manually defined.
Liquibase enables you to specify the desired database modification using SQL or a variety of other database-independent forms, such as XML, YAML, and JSON. To make it incredibly simple to publish updates to several database types, developers can abstract the database logic.
Original article source at: https://blog.knoldus.com/
1638624191
Many organizations have implemented DevOps in their applications, that’s true. But, at the same time, their database change process hasn’t realized any of these benefits and is still left in the dark ages. But what if you could automate that too? Yeah, you guessed right — it can be done using Liquibase. In this tutorial, we'll learn How to Automate Database Script Deployment Using Liquibase and Git
1623775440
So, Hello World… After almost a year of development, the first version of JPA Buddy has finally been released! This is a free tool that is supposed to become your faithful coding assistant for projects with JPA and everything related: Hibernate, Spring Data, Liquibase, and other mainstream stacks.
“Why would I use it?” - this is a fair question towards any new tool or framework. In short, if you use JPA for data persistence, JPA Buddy will help you to be more efficient. In this article, I’ll present an overview of the tool. I hope it will take its fair place among the most loved tools for Java developers, ones who use JPA, Spring, Liquibase, and of course the most advanced Java IDE - IntelliJ IDEA.
#java #tools #hibernate #jpa #spring data #liquibase
1621239814
The extension provides an example [changelog.yaml] file that demonstrates a changelog applied to Spanner using the[ Liquibase CLI] and helps applying Liquibase [best practices] on Cloud Spanner.
Among the current [limitations], there are unsupported Spanner features, such as views and stored procedures, and DML limits for the number of rows affected during DML, with the recommendation to use partitioned DML. Using the [Spanner JDBC driver], this can be configured using the AUTOCOMMIT_DML_MODE.
Liquibase is not the only open source database version control tool currently supported by Cloud Spanner.
#database #liquibase #cloud spanner #added support for liquibase
1602428611
I believe that there is no need for describing why the usage of database migration tools is vital for modern days apps that utilize relational database engines. I will only say that they can make our life much easier and help us to automatize a complex and repetitive process. Through the course of this article, I will provide some more insights into similarities and differences between two of the most common open-source migration tools, namely Flyway and Liquibase. I will start with quick descriptions of both tools.
It is described by its creators, a company called Redgate, as an open-source database migration tool that prefers simplicity and convention over configuration. As of now, it supports most of the currently used database engines, such as Postgres, Oracle, SQL Server, DB2, H2, MariaDB, and many others. It also supports some cloud base database services like Amazon RDS or Google Cloud SQL or Heroku. The script can be written in pure SQL (many dialects are supported) or in Java (mostly for more complex transformations). It has a command line client but it also provides Maven and Gradle plugins. What is more, it has Java API which also works for Android. For those of you who use .NET and C#, there is a Flyway counterpart named Evolve so if you are interested you can check this one up. The link to its GitHub page will be at the end of the article.
It started in 2006 and is an open-source tool for database migrations. It is based on the concept ofchangelogsandchangesetsfiles which can be written in SQL, XML, YAML, JSON. There we store all the changes which we want to make in our database structure. These files can be further used to apply those changes to any other database instance. Liquibase supports subsequent databases: Postgres, Oracle, DB2, H2, MariaDB, SQL Server, SQLite, and many others. Many cloud-based databases are also supported for example Azure SQL, Amazon RDS, Amazon Aurora. You can run Liquibase migration scripts from shell, using build tools such as Maven Gradle or even Ant. Moreover, you can generate pure SQL queries that can be further executed by your DBA-s/Ops/DevOps team or anyone who is taking care of your database.
Now that we described both tools, we can move to describing similarities and differences between them. Let’s start with the similarities between these two tools.
#tools #database #integration #migration #opensource #comparison #discussion #liquibase #flyway
1601480520
If you are a back-end developer, you are often faced with having to migrate your database schema with each new release.
The framework called Liquibase can make it easier for you when you need to upgrade your database schema.
In this article, I’ll explain how Liquibase can be used in a Java project, with Spring/Hibernate, to version the database schema.
Changelog
Liquibase works with Changelog files (the list of all the changes, in order, that need to execute to update the database).
There are 4 supported formats for these Changelogs: SQL, XML, YAML, and JSON.
#liquibase #database #versioning #plugins #java #hackernoon-top-story #database-schema-versioning #database-schema-migration
1596858840
Liquibase is an open source and extensible change management project that supports a variety of databases including Snowflake, MySQL, and PostgreSQL via JDBC. Liquibase allows users to easily define changes in SQL, XML, JSON, and YAML. These changes are then managed in a version control system so the changes can be documented, ordered, and standardized. For more information on the features and benefits of Liquibase, check out their documentation site.
In this blog post we’ll show you how to:
New to distributed SQL or YugabyteDB? Read on.
Distributed SQL databases are becoming popular with organizations interested in moving data infrastructure to the cloud or cloud native environments. This is often motivated by the desire to reduce TCO or move away from the horizontal scaling limitations of monolithic RDBMS like Oracle, PostgreSQL, MySQL, and SQL Server. The basic characteristics of Distributed SQL are:
For a deeper discussion about what Distributed SQL is, check out, “What is Distributed SQL?”
YugabyteDB is an open source, high-performance distributed SQL database built on a scalable and fault-tolerant design inspired by Google Spanner. YugabyteDB is PostgreSQL wire compatible, cloud native, offers deep integration with GraphQL projects, plus supports advanced RDBMS features like stored procedures, triggers, and UDFs.
Got questions? Make sure to ask them in our YugabyteDB Slack channel. Ok, let’s dive in…
In this section we are going to install YugabyteDB on the cluster. The complete steps are documented here. We’ll assume you already have a GKE cluster up and running as a starting point.
The first thing to do is to add the charts repository.
$ helm repo add yugabytedb https://charts.yugabyte.com
Now, fetch the updates.
$ helm repo update
Create a namespace. In this case we’ll call it yb-demo.
$ kubectl create namespace yb-demo
Expected output:
namespace/yb-demo created
We are now ready to install YugabyteDB. In the command below we’ll be specifying values for a resource constrained environment.
$ helm install yb-demo yugabytedb/yugabyte \
--set resource.master.requests.cpu=1,resource.master.requests.memory=1Gi,\
resource.tserver.requests.cpu=1,resource.tserver.requests.memory=1Gi,\
enableLoadBalancer=True --namespace yb-demo --wait
To check the status of the cluster, execute the below command:
$ kubectl get services --namespace yb-demo
Note the external-IP for yb-tserver-service which we are going to use to establish a connection between YugabyteDB and Liquibase. From the screenshot above we can see that the IP is 34.72.XX.XX
and the YSQL port is 5433
.
#databases #distributed sql #google cloud platform #how to #kubernetes #liquibase