Giter VIP home page Giter VIP logo

test_liquibase_1's Introduction

Database Migration with Liquibase

This is a Spring Boot application with Liquibase to manage the database migration.

These are some steps to guide the whole procedure:

Add the Liquibase dependencies:

To add the Liquibase into the project, open the build.gradle file and add it to the classpath:

buildscript {
	ext {
		springBootVersion = '1.5.8.RELEASE'
	}
	repositories {
		mavenCentral()
		mavenLocal()
	}
	dependencies {
		classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
		classpath "org.mariadb.jdbc:mariadb-java-client-jre7:1.6.1"
		classpath "org.liquibase:liquibase-gradle-plugin:1.2.4"
	}
}

At the same file, add the Liquibase dependency:

dependencies {
	compile group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '1.5.8.RELEASE'

	compile group: 'org.liquibase', name: 'liquibase-core', version: '3.5.3'
}

Finally, apply the liquibase plugin:

apply plugin: 'liquibase'

Configure the Liquibase properties:

To avoid the Liquibase task execution during the application startup, open the application.properties file and add the following configuration on it:

liquibase.enabled=false

Create the Liquibase activity:

To create the Liquibase activity, open the build.gradle file and add a new section on it:

liquibase {
	activities {
		main {
			changeLogFile 'src/main/db/changelog.xml'
			url 'jdbc:mariadb://localhost:3306/testliquibase'
            username 'root'
            password 'root'

            referenceUrl 'jdbc:mariadb://localhost:3306/testliquibase_reference'
            referenceUsername 'root'
            referencePassword 'root'
		}
	}

	runList = 'main'
}

These are the parameters:

  • changeLogFile: the XML file where the change sets will be created by Liquibase;
  • url: the database destination address where the changes must be applied/compared;
  • username: the username of the destination database where the changes must be applied/compared;
  • password: the password of the destination database where the changes must be applied/compared;
  • referenceUrl: the address of the reference database where the changes where performed;
  • referenceUsername: the username of the reference database where the changes where performed;
  • referencePassword: the password of the reference database where the changes where performed.

Add an entity model:

Add an entity model into the application, for instance:

@Entity
public class Product {

    @Id
    @GeneratedValue(strategy= GenerationType.AUTO)
    private long id;

    @Column(length = 30, nullable = false)
    private String name;

    @Column(length = 30, nullable = false)
    private String model;

    @Column(nullable = false)
    private double price;
    
    //Getters and Setters
}

Execute the Liquibase tasks:

There are two main tasks to be performed in case of model creation or changes:

Populate the change log XML file:

To populate the change log XML file with the changes from the models, execute the diffChangeLog task. This task will populate the change log file with the last changes, comparing the reference database with the base database schemas. These a example related to the creation of the Product entity:

    <changeSet author="paulo.cesar (generated)" id="1516704417378-1">
        <createTable tableName="product">
            <column autoIncrement="true" name="id" type="BIGINT">
                <constraints primaryKey="true"/>
            </column>
            <column name="model" type="VARCHAR(30)">
                <constraints nullable="false"/>
            </column>
            <column name="name" type="VARCHAR(30)">
                <constraints nullable="false"/>
            </column>
            <column name="price" type="DOUBLE">
                <constraints nullable="false"/>
            </column>
        </createTable>
    </changeSet>

The id parameter at the changeSet section must be used to uniquely identify the change set.

When more changes where performed in certain entity model, execute the diffChangeLog task again, to append one more change set at this file.

Generate the SQL statements to perform the changes:

To generate the SQL statements to perform the changes, execute the updateSQL task. The SQL statements will be printed at execution log.

Update the DDL scripts:

Use the SQL statements generated by the updateSQL task to update the DDL scripts.

test_liquibase_1's People

Contributors

siecola avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.