# flyway-demo **Repository Path**: code-demo-space/flyway-demo ## Basic Information - **Project Name**: flyway-demo - **Description**: flyway使用示例 - **Primary Language**: Java - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-04-15 - **Last Updated**: 2021-08-31 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # flyway-demo ## flyway介绍 Flyway官网:https://flywaydb.org ### 什么时Flyway Flyway是一个Version control for your database(数据库的版本管理)工具。 ### Flyway安装 #### linux下安装 ``` shell wget -qO- https://repo1.maven.org/maven2/org/flywaydb/flyway-commandline/7.7.3/flyway-commandline-7.7.3-linux-x64.tar.gz | tar xvz && sudo ln -s `pwd`/flyway-7.7.3/flyway /usr/local/bin ``` #### Docker安装 ```shell docker pull flyway/flyway:7.7.3 ``` # FlyWay使用 ## 原理 ### 表 - flyway_schema_history :此表将用于跟踪数据库的状态。 ##### flyway_schema_history示例 | installed_rank | version | description | type | script | checksum | installed_by | installed_on | execution_time | success | | :------------- | :------ | :------------ | :--- | :-------------------- | :--------- | :----------- | :-------------------- | :------------- | :------ | | 1 | 1 | Initial Setup | SQL | V1__Initial_Setup.sql | 1996767037 | axel | 2016-02-04 22:23:00.0 | 546 | true | | 2 | 2 | First Changes | SQL | V2__First_Changes.sql | 1279644856 | axel | 2016-02-06 09:18:00.0 | 127 | true | | 3 | 2.1 | Refactoring | JDBC | V2_1__Refactoring | | axel | 2016-02-10 17:45:05.4 | 251 | true | ## 使用 ### Flyway API 引入依赖: ``` xml org.flywaydb flyway-core 7.7.3 ``` 创建Flyway实例: ```java String dbUrl = "jdbc:postgresql://39.100.155.156:5432/flyway_db"; String dbUserName = "flyway"; String dbPassword = "flyway"; // 创建Flyway实例 Flyway flyway = Flyway.configure().dataSource(dbUrl, dbUserName, dbPassword).load(); // 开始更新管理 flyway.migrate(); ``` 创建文件夹:src/main/resources/db/migration 创建第一个版本文件:src/main/resources/db/migration/V1__Create_person_table.sql ```sql create table PERSON ( ID int not null, NAME varchar(100) not null ); ``` 执行程序: 可以看到日志: ``` 16:17:45.443 [main] INFO org.flywaydb.core.internal.license.VersionPrinter - Flyway Community Edition 7.1.1 by Redgate 16:17:45.748 [main] INFO org.flywaydb.core.internal.database.base.DatabaseType - Database: jdbc:postgresql://39.100.155.156:5432/flyway_db (PostgreSQL 10.15) 16:17:45.926 [main] DEBUG org.flywaydb.core.internal.scanner.Scanner - Filtering out resource: db/migration/V1__Create_person_table.sql (filename: V1__Create_person_table.sql) 16:17:46.083 [main] INFO org.flywaydb.core.internal.command.DbValidate - Successfully validated 1 migration (execution time 00:00.176s) 16:17:46.269 [main] INFO org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory - Creating Schema History table "public"."flyway_schema_history" ... 16:17:46.288 [main] DEBUG org.flywaydb.core.internal.sqlscript.DefaultSqlScriptExecutor - Executing SQL: CREATE TABLE "public"."flyway_schema_history" ( "installed_rank" INT NOT NULL, "version" VARCHAR(50), "description" VARCHAR(200) NOT NULL, "type" VARCHAR(20) NOT NULL, "script" VARCHAR(1000) NOT NULL, "checksum" INTEGER, "installed_by" VARCHAR(100) NOT NULL, "installed_on" TIMESTAMP NOT NULL DEFAULT now(), "execution_time" INTEGER NOT NULL, "success" BOOLEAN NOT NULL ) 16:17:46.309 [main] DEBUG org.flywaydb.core.internal.sqlscript.DefaultSqlScriptExecutor - Executing SQL: ALTER TABLE "public"."flyway_schema_history" ADD CONSTRAINT "flyway_schema_history_pk" PRIMARY KEY ("installed_rank") 16:17:46.326 [main] DEBUG org.flywaydb.core.internal.sqlscript.DefaultSqlScriptExecutor - Executing SQL: CREATE INDEX "flyway_schema_history_s_idx" ON "public"."flyway_schema_history" ("success") 16:17:46.490 [main] INFO org.flywaydb.core.internal.command.DbMigrate - Migrating schema "public" to version "1 - Create person table" 16:17:46.526 [main] DEBUG org.flywaydb.core.internal.sqlscript.DefaultSqlScriptExecutor - Executing SQL: create table PERSON ( ID int not null, NAME varchar(100) not null ) 16:17:46.540 [main] DEBUG org.flywaydb.core.internal.command.DbMigrate - Successfully completed migration of schema "public" to version "1 - Create person table" 16:17:46.616 [main] DEBUG org.flywaydb.core.internal.schemahistory.JdbcTableSchemaHistory - Schema History table "public"."flyway_schema_history" successfully updated to reflect changes 16:17:46.738 [main] INFO org.flywaydb.core.internal.command.DbMigrate - Successfully applied 1 migration to schema "public" (execution time 00:00.359s) ``` ## demo介绍 #### 介绍 flyway使用示例 #### 软件架构 软件架构说明 #### 安装教程 1. xxxx 2. xxxx 3. xxxx #### 使用说明 1. xxxx 2. xxxx 3. xxxx #### 参与贡献 1. Fork 本仓库 2. 新建 Feat_xxx 分支 3. 提交代码 4. 新建 Pull Request #### 特技 1. 使用 Readme\_XXX.md 来支持不同的语言,例如 Readme\_en.md, Readme\_zh.md 2. Gitee 官方博客 [blog.gitee.com](https://blog.gitee.com) 3. 你可以 [https://gitee.com/explore](https://gitee.com/explore) 这个地址来了解 Gitee 上的优秀开源项目 4. [GVP](https://gitee.com/gvp) 全称是 Gitee 最有价值开源项目,是综合评定出的优秀开源项目 5. Gitee 官方提供的使用手册 [https://gitee.com/help](https://gitee.com/help) 6. Gitee 封面人物是一档用来展示 Gitee 会员风采的栏目 [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/)