Home >Backend Development >PHP Tutorial >PHP Master | Versioning Your Database with Liquibase
Core points
Most of the applications we develop are managed using some version control system. But what about the databases these applications use? We make changes to development, testing, and production databases more often. This approach may work for applications with only one or two developers, but in large teams with multiple developers, sharing changes with everyone becomes difficult. In this article, we will discuss Liquibase, an open source tool for managing and versioning database schema changes. It helps us organize incremental database changes into different changesets and apply them to the database. Liquibase is not the only database versioning/migration tool. There are many solutions, such as Doctrine 2 migrations, Rails AR migrations, DBDeploy and more. The first two options are excellent solutions, but they are platform-specific. DBDeploy is relatively simple, but it is not as feature-rich as Liquibase. Liquibase solves many unsolved issues with other database migration tools, such as supporting multiple developers, different DBMS systems, branches, and more. Furthermore, a serious disadvantage of most tools is that they are not perceived to change. Instead of focusing on the changes made, they compare two snapshots of the database schema to generate the migration script. Therefore, for example, renaming a column is considered a drop add operation, which can lead to data loss. Liquibase is able to perceive changes. Let's see how to use Liquibase in your project.
How does Liquibase work
If you are using a Mac with brew, installing Liquibase is easy. Just run brew install Liquibase
and it's done. The same is true for Ubuntu, sudo apt-get install liquibase
can be done. Liquibase binary is a cross-platform Java application, which means you can download JARs and use them for Windows, Mac, or Linux. It's better to save it in the project folder so that anyone in the project can use it without any installation. When using Liquibase, you store database changes in an XML file, commonly known as a changelog file. Changes can be saved in a single file or in multiple files and then included in the main changelog file. A second option is recommended because it allows for greater flexibility when organization changes. In the change log file, you organize changes by different change sets. A change set can contain one or more changes to apply to the database. Each changeset can be uniquely identified using the id and author attributes and the classpath of the changelog file. Liquibase Creates a table (databasechangelog) in your database to track changes that have been successfully applied. Liquibase runs each changeset one by one and checks whether they have been applied by comparing checksums in the databasechangelog table. If it has not run or has a runAlways tag on it, it will apply the changes.
Beginner
For demonstration, I created a database called application on my local MySQL server, along with a change log file. You can save it in the project folder or elsewhere, but the changelog file should be under version control. This is the first version of our changelog file, without a change set.
<code class="language-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" xsi:schemalocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd"> </databasechangelog></code>
Navigate to the location where you saved the changelog file in the command line and run the following command:
<code class="language-bash">liquibase --driver=com.mysql.jdbc.Driver \ --classpath=../lib/mysql-connector-java-5.1.21-bin.jar \ --changeLogFile=db.changelog.xml \ --url="jdbc:mysql://localhost/application" \ --username=dbuser \ --password=secret \ update</code>
If Liquibase can connect to the database with the given username and password, it should create two tables in the application database, DATABASECHANGELOG and DATABASECHANGELOGLOCK, and display the following output:
(The following content is omitted because it is repeated with the original text and necessary rewrites and adjustments have been made to avoid repeated output.)
(The rest of the article also requires similar rewriting. While maintaining the consistency of the content, adjust the sentence structure and words to avoid duplication.)
The above is the detailed content of PHP Master | Versioning Your Database with Liquibase. For more information, please follow other related articles on the PHP Chinese website!