青岛网站建设公司番禺网站建设

淄博森威计算机科技有限公司 2026/09/09 17:42:21

关于数据库迁移


什么是数据库迁移


数据库迁移(Database Migration)是指对数据库结构进行版本控制的过程,它允许开发团队跟踪数据库模式的变更,并确保在不同环境(开发、测试、生产)中数据库结构的一致性。

为什么需要数据库迁移


在传统的软件开发中,数据库变更通常通过手动执行SQL脚本完成,这种方式存在诸多问题:

版本不一致:不同环境的数据库结构可能不一致

协作困难:多人开发时容易产生冲突

回滚复杂:出现问题时难以快速回滚到之前的版本

缺乏历史记录:无法追踪数据库结构的变更历史

Liquibase

Liquibase 是一个开源的数据库迁移工具,它通过 changelog 文件来管理数据库的变更,支持多种格式(XML、JSON、YAML、SQL)的变更定义,能够与多种数据库系统协同工作。

主要特性:

支持多种数据库(MySQL、PostgreSQL、Oracle等)

多种变更定义格式

回滚支持

与Spring Boot无缝集成

丰富的变更类型支持

Spring Boot 集成 Liquibase

添加依赖

首先,在pom.xml中添加 Liquibase 依赖:

<!-- Liquibase Database Migration --> <dependency> <groupId>org.liquibase</groupId> <artifactId>liquibase-core</artifactId> </dependency>

Liquibase比起Flyway,对Springboot的支持更好。使用Springboot3也可以运行,Flyway则在Springboot3里面无法使用。

YML配置

# Liquibase Configuration liquibase: enabled: true change-log: classpath:db/changelog/db.changelog-master.xml drop-first: false default-schema: mqtt

定义Liquibase的XML

创建主变更日志文件

src/main/resources/db/changelog目录下创建主变更日志文件db.changelog-master.yaml

<?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-4.20.xsd"> <!-- Include all changelog files --> <include file="db/changelog/changes/v1.0-initial-schema.xml"/> <include file="db/changelog/changes/v1.1-sample-data.xml"/> </databaseChangeLog>

v1.0-initial-schema.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-4.20.xsd"> <!-- Create mqtt_connection table --> <changeSet id="1" author="mqtt-websocket-bridge"> <createTable tableName="mqtt_connection" remarks="MQTT Connection Table"> <column name="id" type="BIGINT" autoIncrement="true"> <constraints primaryKey="true" nullable="false"/> </column> <column name="client_id" type="VARCHAR(255)" remarks="MQTT Client ID"> <constraints nullable="false" unique="true"/> </column> <column name="broker_url" type="VARCHAR(255)" remarks="MQTT Broker URL"> <constraints nullable="false"/> </column> <column name="username" type="VARCHAR(255)" remarks="Username"/> <column name="password" type="VARCHAR(255)" remarks="Password"/> <column name="keep_alive" type="INT" defaultValue="60" remarks="Keep Alive Interval (seconds)"/> <column name="clean_session" type="TINYINT(1)" defaultValue="1" remarks="Clean Session Flag"/> <column name="protocol_version" type="VARCHAR(50)" defaultValue="MQTT 3.1.1" remarks="Protocol Version"/> <column name="tls_enabled" type="TINYINT(1)" defaultValue="0" remarks="TLS Enabled"/> <column name="status" type="INT" defaultValue="0" remarks="Connection Status (0: disconnected, 1: connected)"/> <column name="connected_at" type="DATETIME" remarks="Connection Timestamp"/> <column name="disconnected_at" type="DATETIME" remarks="Disconnection Timestamp"/> <column name="created_at" type="DATETIME" defaultValueComputed="CURRENT_TIMESTAMP" remarks="Create Time"/> <column name="updated_at" type="DATETIME" defaultValueComputed="CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" remarks="Update Time"/> <column name="deleted" type="INT" defaultValue="0" remarks="Logical Delete Flag"/> </createTable> <!-- Create indexes for mqtt_connection --> <createIndex indexName="idx_client_id" tableName="mqtt_connection"> <column name="client_id"/> </createIndex> <createIndex indexName="idx_status" tableName="mqtt_connection"> <column name="status"/> </createIndex> <createIndex indexName="idx_created_at" tableName="mqtt_connection"> <column name="created_at"/> </createIndex> </changeSet> <!-- Create mqtt_subscription table --> <changeSet id="2" author="mqtt-websocket-bridge"> <createTable tableName="mqtt_subscription" remarks="MQTT Subscription Table"> <column name="id" type="BIGINT" autoIncrement="true"> <constraints primaryKey="true" nullable="false"/> </column> <column name="client_id" type="VARCHAR(255)" remarks="MQTT Client ID"> <constraints nullable="false"/> </column> <column name="topic" type="VARCHAR(500)" remarks="MQTT Topic"> <constraints nullable="false"/> </column> <column name="qos" type="INT" defaultValue="0" remarks="Quality of Service (0, 1, 2)"/> <column name="subscribed_at" type="DATETIME" defaultValueComputed="CURRENT_TIMESTAMP" remarks="Subscription Timestamp"/> <column name="created_at" type="DATETIME" defaultValueComputed="CURRENT_TIMESTAMP" remarks="Create Time"/> <column name="updated_at" type="DATETIME" defaultValueComputed="CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP" remarks="Update Time"/> <column name="deleted" type="INT" defaultValue="0" remarks="Logical Delete Flag"/> </createTable> <!-- Create indexes for mqtt_subscription --> <createIndex indexName="idx_client_id" tableName="mqtt_subscription"> <column name="client_id"/> </createIndex> <createIndex indexName="idx_topic" tableName="mqtt_subscription"> <column name="topic"/> </createIndex> <createIndex indexName="idx_subscribed_at" tableName="mqtt_subscription"> <column name="subscribed_at"/> </createIndex> <!-- Create unique constraint --> <addUniqueConstraint tableName="mqtt_subscription" columnNames="client_id, topic" constraintName="uk_client_topic"/> </changeSet> <!-- Create mqtt_message table --> <changeSet id="3" author="mqtt-websocket-bridge"> <createTable tableName="mqtt_message" remarks="MQTT Message Table"> <column name="id" type="BIGINT" autoIncrement="true"> <constraints primaryKey="true" nullable="false"/> </column> <column name="client_id" type="VARCHAR(255)" remarks="MQTT Client ID"> <constraints nullable="false"/> </column> <column name="topic" type="VARCHAR(500)" remarks="MQTT Topic"> <constraints nullable="false"/> </column> <column name="payload" type="TEXT" remarks="Message Payload"/> <column name="qos" type="INT" defaultValue="0" remarks="Quality of Service (0, 1, 2)"/> <column name="retained" type="TINYINT(1)" defaultValue="0" remarks="Retained Message Flag"/> <column name="direction" type="VARCHAR(50)" remarks="Message Direction (INBOUND, OUTBOUND)"> <constraints nullable="false"/> </column> <column name="created_at" type="DATETIME" defaultValueComputed="CURRENT_TIMESTAMP" remarks="Create Time"/> <column name="deleted" type="INT" defaultValue="0" remarks="Logical Delete Flag"/> </createTable> <!-- Create indexes for mqtt_message --> <createIndex indexName="idx_client_id" tableName="mqtt_message"> <column name="client_id"/> </createIndex> <createIndex indexName="idx_topic" tableName="mqtt_message"> <column name="topic"/> </createIndex> <createIndex indexName="idx_direction" tableName="mqtt_message"> <column name="direction"/> </createIndex> <createIndex indexName="idx_created_at" tableName="mqtt_message"> <column name="created_at"/> </createIndex> </changeSet> </databaseChangeLog>

v1.1-sample-data.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-4.20.xsd"> <!-- Insert sample connection data --> <changeSet id="4" author="mqtt-websocket-bridge"> <insert tableName="mqtt_connection"> <column name="client_id" value="emqx_NJEONID"/> <column name="broker_url" value="tcp://localhost:1883"/> <column name="username" value=""/> <column name="password" value=""/> <column name="keep_alive" valueNumeric="60"/> <column name="clean_session" valueNumeric="1"/> <column name="protocol_version" value="MQTT 5"/> <column name="status" valueNumeric="0"/> </insert> </changeSet> </databaseChangeLog>
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

网站建设广告南通网站建设

BMAD-METHOD揭秘:如何用AI实现规划文档到开发任务的终极自动化【免费下载链接】BMAD-METHODBreakthrough Method for Agile Ai Drive

2026/06/30 13:15:35

网站建设设计龙岩网站建设

Podcast Bulk Downloader 终极指南:播客批量下载完全手册【免费下载链接】PodcastBulkDownloaderSimple software for downl

2026/06/30 13:58:38

佛山网站建设大型门户网站建设

原子级精度的制造工艺正在将硅这种经典计算的王者材料推向量子计算的最前沿。《自然》杂志刊登了一项令人瞩目的成果,来自澳大利亚硅量子计算公司(Silicon Quantum C

2026/06/30 13:24:05

烟台网站建设湛江网站建设

智能转换网页内容:高效管理信息的终极快速方案【免费下载链接】markdownloadA Firefox and Google Chrome extension to clip websi

2026/06/30 11:25:55

怀化网站建设怎么网站建设

Qwen3-VL多轮对话记忆保持:上下文连贯性测试结果公布在智能客服、远程协助和自动化操作日益普及的今天,一个AI模型能否“记住”你几分钟前说过的话、看过的图,

2026/06/30 11:41:27

贵阳网站建设丹阳网站建设

用好screen,告别断连烦恼:Linux终端会话管理实战精讲你有没有过这样的经历?深夜在远程服务器上编译一个大型项目,眼看着进度条快走完了&#

2026/06/30 10:33:51

晋江网站建设成都企业网站建设

RPM 包文件结构与资源指南在 Linux 系统中,RPM(Red Hat Package Manager)是广泛使用的软件包管理系统。下面将详细介绍 RPM 包文件结构、相关标签以及网络上可获取的

2026/06/30 14:11:39

网站建设方案大庆网站建设

命令行下载工具深度解析:curl与wget实战指南【免费下载链接】Bash-OnelinerA collection of handy Bash One-Liners and termi

2026/06/30 14:15:09

营销网站建设专业网站建设

重构开发工作流:三大云原生工具的架构融合实践【免费下载链接】code-server项目地址: https://gitcode.com/gh_mirrors/cod/code-server

2026/06/30 12:20:00