当前位置:   article > 正文

Flyway自学之路-02(使用命令行操作Flyway)_flyway安装

flyway安装

1.去官网下载并安装命令行的包

https://flywaydb.org/documentation/commandline/

2.解压文件,放在自定义目录下

这里我放在D:\software\java\flyway目录下

3.将该路径添加至环境变量path中

4.测试是否安装成功

执行cmd,输入flyway,出现以下界面说明配置成功。

5.简单配置一个建表SQL

在D:\software\java\flyway\flyway-6.4.2\conf 可以找到配置文件,然后对其进行配置。主要是以下几个配置项需要配置。

  1. #
  2. # Copyright 2010-2020 Boxfuse GmbH
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. #
  16. # JDBC url to use to connect to the database
  17. # Examples
  18. # --------
  19. # Most drivers are included out of the box.
  20. # * = JDBC driver must be downloaded and installed in /drivers manually
  21. # ** = TNS_ADMIN environment variable must point to the directory of where tnsnames.ora resides
  22. # Aurora MySQL : jdbc:mysql://<instance>.<region>.rds.amazonaws.com:<port>/<database>?<key1>=<value1>&<key2>=<value2>...
  23. # Aurora PostgreSQL : jdbc:postgresql://<instance>.<region>.rds.amazonaws.com:<port>/<database>?<key1>=<value1>&<key2>=<value2>...
  24. # CockroachDB : jdbc:postgresql://<host>:<port>/<database>?<key1>=<value1>&<key2>=<value2>...
  25. # DB2* : jdbc:db2://<host>:<port>/<database>
  26. # Derby : jdbc:derby:<subsubprotocol>:<database><;attribute=value>
  27. # Firebird : jdbc:firebirdsql://<host>[:<port>]/<database>?<key1>=<value1>&<key2>=<value2>...
  28. # H2 : jdbc:h2:<file>
  29. # HSQLDB : jdbc:hsqldb:file:<file>
  30. # Informix* : jdbc:informix-sqli://<host>:<port>/<database>:informixserver=dev
  31. # MariaDB : jdbc:mariadb://<host>:<port>/<database>?<key1>=<value1>&<key2>=<value2>...
  32. # MySQL : jdbc:mysql://<host>:<port>/<database>?<key1>=<value1>&<key2>=<value2>...
  33. # Oracle : jdbc:oracle:thin:@//<host>:<port>/<service>
  34. # Oracle (TNS)** : jdbc:oracle:thin:@<tns_entry>
  35. # PostgreSQL : jdbc:postgresql://<host>:<port>/<database>?<key1>=<value1>&<key2>=<value2>...
  36. # SAP HANA* : jdbc:sap://<host>:<port>/?databaseName=<database>
  37. # Snowflake* : jdbc:snowflake://<account>.snowflakecomputing.com/?db=<database>&warehouse=<warehouse>&role=<role>...
  38. # SQL Server : jdbc:sqlserver://<host>:<port>;databaseName=<database>
  39. # SQLite : jdbc:sqlite:<database>
  40. # Sybase ASE : jdbc:jtds:sybase://<host>:<port>/<database>
  41. # Redshift* : jdbc:redshift://<host>:<port>/<database>
  42. flyway.url=jdbc:mysql://localhost:3306/flyway?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=utf-8
  43. # Fully qualified classname of the JDBC driver (autodetected by default based on flyway.url)
  44. # lyway.driver=
  45. # User to use to connect to the database. Flyway will prompt you to enter it if not specified, and if the JDBC
  46. # connection is not using a password-less method of authentication.
  47. flyway.user=root
  48. # Password to use to connect to the database. Flyway will prompt you to enter it if not specified, and if the JDBC
  49. # connection is not using a password-less method of authentication.
  50. flyway.password=123456
  51. # The maximum number of retries when attempting to connect to the database. After each failed attempt,
  52. # Flyway will wait 1 second before attempting to connect again, up to the maximum number of times specified
  53. # by connectRetries. (default: 0)
  54. # flyway.connectRetries=
  55. # The SQL statements to run to initialize a new database connection immediately after opening it. (default: none)
  56. # flyway.initSql=
  57. # The default schema managed by Flyway. This schema name is case-sensitive. If not specified, but <i>flyway.schemas</i> is, Flyway uses the first schema
  58. # in that list. If that is also not specified, Flyway uses the default schema for the database connection.
  59. # Consequences:
  60. # - This schema will be the one containing the schema history table.
  61. # - This schema will be the default for the database connection (provided the database supports this concept).
  62. # flyway.defaultSchema=
  63. # Comma-separated list of schemas managed by Flyway. These schema names are case-sensitive. If not specified, Flyway uses
  64. # the default schema for the database connection. If <i>flyway.defaultSchema</i> is not specified, then the first of
  65. # this list also acts as default schema.
  66. # Consequences:
  67. # - Flyway will automatically attempt to create all these schemas, unless they already exist.
  68. # - The schemas will be cleaned in the order of this list.
  69. # - If Flyway created them, the schemas themselves will be dropped when cleaning.
  70. # flyway.schemas=
  71. # Name of Flyway's schema history table (default: flyway_schema_history)
  72. # By default (single-schema mode) the schema history table is placed in the default schema for the connection
  73. # provided by the datasource.
  74. # When the flyway.schemas property is set (multi-schema mode), the schema history table is placed in the first
  75. # schema of the list.
  76. # flyway.table=
  77. # The tablespace where to create the schema history table that will be used by Flyway. If not specified, Flyway uses
  78. # the default tablespace for the database connection.
  79. # This setting is only relevant for databases that do support the notion of tablespaces. Its value is simply
  80. # ignored for all others.
  81. # flyway.tablespace=
  82. # Comma-separated list of locations to scan recursively for migrations. (default: filesystem:<<INSTALL-DIR>>/sql)
  83. # The location type is determined by its prefix.
  84. # Unprefixed locations or locations starting with classpath: point to a package on the classpath and may contain
  85. # both SQL and Java-based migrations.
  86. # Locations starting with filesystem: point to a directory on the filesystem, may only
  87. # contain SQL migrations and are only scanned recursively down non-hidden directories.
  88. # Wildcards can be used to reduce duplication of location paths. (e.g. filesystem:migrations/*/oracle) Supported wildcards:
  89. # ** : Matches any 0 or more directories
  90. # * : Matches any 0 or more non-separator characters
  91. # ? : Matches any 1 non-separator character
  92. flyway.locations=filesystem:\\D:\software\java\flyway\flyway-6.4.2\sql
  93. # Comma-separated list of fully qualified class names of custom MigrationResolver to use for resolving migrations.
  94. # flyway.resolvers=
  95. # If set to true, default built-in resolvers (jdbc, spring-jdbc and sql) are skipped and only custom resolvers as
  96. # defined by 'flyway.resolvers' are used. (default: false)
  97. # flyway.skipDefaultResolvers=
  98. # Comma-separated list of directories containing JDBC drivers and Java-based migrations.
  99. # (default: <INSTALL-DIR>/jars)
  100. # flyway.jarDirs=
  101. # File name prefix for versioned SQL migrations (default: V)
  102. # Versioned SQL migrations have the following file name structure: prefixVERSIONseparatorDESCRIPTIONsuffix ,
  103. # which using the defaults translates to V1_1__My_description.sql
  104. # flyway.sqlMigrationPrefix=
  105. # The file name prefix for undo SQL migrations. (default: U)
  106. # Undo SQL migrations are responsible for undoing the effects of the versioned migration with the same version.
  107. # They have the following file name structure: prefixVERSIONseparatorDESCRIPTIONsuffix ,
  108. # which using the defaults translates to U1.1__My_description.sql
  109. # Flyway Pro and Flyway Enterprise only
  110. # flyway.undoSqlMigrationPrefix=
  111. # File name prefix for repeatable SQL migrations (default: R)
  112. # Repeatable SQL migrations have the following file name structure: prefixSeparatorDESCRIPTIONsuffix ,
  113. # which using the defaults translates to R__My_description.sql
  114. # flyway.repeatableSqlMigrationPrefix=
  115. # File name separator for Sql migrations (default: __)
  116. # Sql migrations have the following file name structure: prefixVERSIONseparatorDESCRIPTIONsuffix ,
  117. # which using the defaults translates to V1_1__My_description.sql
  118. # flyway.sqlMigrationSeparator=
  119. # Comma-separated list of file name suffixes for SQL migrations. (default: .sql)
  120. # SQL migrations have the following file name structure: prefixVERSIONseparatorDESCRIPTIONsuffix ,
  121. # which using the defaults translates to V1_1__My_description.sql
  122. # Multiple suffixes (like .sql,.pkg,.pkb) can be specified for easier compatibility with other tools such as
  123. # editors with specific file associations.
  124. # flyway.sqlMigrationSuffixes=
  125. # Whether to stream SQL migrations when executing them. (default: false)
  126. # Streaming doesn't load the entire migration in memory at once. Instead each statement is loaded individually.
  127. # This is particularly useful for very large SQL migrations composed of multiple MB or even GB of reference data,
  128. # as this dramatically reduces Flyway's memory consumption.
  129. # Flyway Pro and Flyway Enterprise only
  130. # flyway.stream=
  131. # Whether to batch SQL statements when executing them. (default: false)
  132. # Batching can save up to 99 percent of network roundtrips by sending up to 100 statements at once over the
  133. # network to the database, instead of sending each statement individually. This is particularly useful for very
  134. # large SQL migrations composed of multiple MB or even GB of reference data, as this can dramatically reduce
  135. # the network overhead. This is supported for INSERT, UPDATE, DELETE, MERGE and UPSERT statements.
  136. # All other statements are automatically executed without batching.
  137. # Flyway Pro and Flyway Enterprise only
  138. # flyway.batch=
  139. # Encoding of SQL migrations (default: UTF-8). Caution: changing the encoding after migrations have been run
  140. # will invalidate the calculated checksums and require a `flyway repair`.
  141. # flyway.encoding=
  142. # Whether placeholders should be replaced. (default: true)
  143. # flyway.placeholderReplacement=
  144. # Placeholders to replace in Sql migrations
  145. # flyway.placeholders.user=
  146. # flyway.placeholders.my_other_placeholder=
  147. # Prefix of every placeholder (default: ${ )
  148. # flyway.placeholderPrefix=
  149. # Suffix of every placeholder (default: } )
  150. # flyway.placeholderSuffix=
  151. # Target version up to which Flyway should consider migrations.
  152. # Defaults to 'latest'
  153. # Special values:
  154. # - 'current': designates the current version of the schema
  155. # - 'latest': the latest version of the schema, as defined by the migration with the highest version
  156. # flyway.target=
  157. # Whether to automatically call validate or not when running migrate. (default: true)
  158. # flyway.validateOnMigrate=
  159. # Whether to automatically call clean or not when a validation error occurs. (default: false)
  160. # This is exclusively intended as a convenience for development. even though we
  161. # strongly recommend not to change migration scripts once they have been checked into SCM and run, this provides a
  162. # way of dealing with this case in a smooth manner. The database will be wiped clean automatically, ensuring that
  163. # the next migration will bring you back to the state checked into SCM.
  164. # Warning ! Do not enable in production !
  165. # flyway.cleanOnValidationError=
  166. # Whether to disable clean. (default: false)
  167. # This is especially useful for production environments where running clean can be quite a career limiting move.
  168. # flyway.cleanDisabled=
  169. # The version to tag an existing schema with when executing baseline. (default: 1)
  170. # flyway.baselineVersion=
  171. # The description to tag an existing schema with when executing baseline. (default: << Flyway Baseline >>)
  172. # flyway.baselineDescription=
  173. # Whether to automatically call baseline when migrate is executed against a non-empty schema with no schema history
  174. # table. This schema will then be initialized with the baselineVersion before executing the migrations.
  175. # Only migrations above baselineVersion will then be applied.
  176. # This is useful for initial Flyway production deployments on projects with an existing DB.
  177. # Be careful when enabling this as it removes the safety net that ensures
  178. # Flyway does not migrate the wrong database in case of a configuration mistake! (default: false)
  179. # flyway.baselineOnMigrate=
  180. # Allows migrations to be run "out of order" (default: false).
  181. # If you already have versions 1 and 3 applied, and now a version 2 is found,
  182. # it will be applied too instead of being ignored.
  183. # flyway.outOfOrder=
  184. # Whether Flyway should output a table with the results of queries when executing migrations (default: true).
  185. # Flyway Pro and Flyway Enterprise only
  186. # flyway.outputQueryResults=
  187. # This allows you to tie in custom code and logic to the Flyway lifecycle notifications (default: empty).
  188. # Set this to a comma-separated list of fully qualified class names of org.flywaydb.core.api.callback.Callback
  189. # implementations.
  190. # flyway.callbacks=
  191. # If set to true, default built-in callbacks (sql) are skipped and only custom callback as
  192. # defined by 'flyway.callbacks' are used. (default: false)
  193. # flyway.skipDefaultCallbacks=
  194. # Ignore missing migrations when reading the schema history table. These are migrations that were performed by an
  195. # older deployment of the application that are no longer available in this version. For example: we have migrations
  196. # available on the classpath with versions 1.0 and 3.0. The schema history table indicates that a migration with
  197. # version 2.0 (unknown to us) has also been applied. Instead of bombing out (fail fast) with an exception, a
  198. # warning is logged and Flyway continues normally. This is useful for situations where one must be able to deploy
  199. # a newer version of the application even though it doesn't contain migrations included with an older one anymore.
  200. # Note that if the most recently applied migration is removed, Flyway has no way to know it is missing and will
  201. # mark it as future instead.
  202. # true to continue normally and log a warning, false to fail fast with an exception. (default: false)
  203. # flyway.ignoreMissingMigrations=
  204. # Ignore ignored migrations when reading the schema history table. These are migrations that were added in between
  205. # already migrated migrations in this version. For example: we have migrations available on the classpath with
  206. # versions from 1.0 to 3.0. The schema history table indicates that version 1 was finished on 1.0.15, and the next
  207. # one was 2.0.0. But with the next release a new migration was added to version 1: 1.0.16. Such scenario is ignored
  208. # by migrate command, but by default is rejected by validate. When ignoreIgnoredMigrations is enabled, such case
  209. # will not be reported by validate command. This is useful for situations where one must be able to deliver
  210. # complete set of migrations in a delivery package for multiple versions of the product, and allows for further
  211. # development of older versions.
  212. # true to continue normally, false to fail fast with an exception. (default: false)
  213. # flyway.ignoreIgnoredMigrations=
  214. # Ignore pending migrations when reading the schema history table. These are migrations that are available
  215. # but have not yet been applied. This can be useful for verifying that in-development migration changes
  216. # don't contain any validation-breaking changes of migrations that have already been applied to a production
  217. # environment, e.g. as part of a CI/CD process, without failing because of the existence of new migration versions.
  218. # (default: false)
  219. # flyway.ignorePendingMigrations=
  220. # Ignore future migrations when reading the schema history table. These are migrations that were performed by a
  221. # newer deployment of the application that are not yet available in this version. For example: we have migrations
  222. # available on the classpath up to version 3.0. The schema history table indicates that a migration to version 4.0
  223. # (unknown to us) has already been applied. Instead of bombing out (fail fast) with an exception, a
  224. # warning is logged and Flyway continues normally. This is useful for situations where one must be able to redeploy
  225. # an older version of the application after the database has been migrated by a newer one.
  226. # true to continue normally and log a warning, false to fail fast with an exception. (default: true)
  227. # flyway.ignoreFutureMigrations=
  228. # Whether to validate migrations and callbacks whose scripts do not obey the correct naming convention. A failure can be
  229. # useful to check that errors such as case sensitivity in migration prefixes have been corrected.
  230. # false to continue normally, true to fail fast with an exception (default: false)
  231. # flyway.validateMigrationNaming=
  232. # Whether to allow mixing transactional and non-transactional statements within the same migration.
  233. # Flyway attempts to run each migration within its own transaction
  234. # If Flyway detects that a specific statement cannot be run within a transaction, it won’t run that migration within a transaction
  235. # This option toggles whether transactional and non-transactional statements can be mixed within a migration run.
  236. # Enabling this means for 'mixed' migrations, the entire script will be run without a transaction
  237. # Note that this is only applicable for PostgreSQL, Aurora PostgreSQL, SQL Server and SQLite which all have
  238. # statements that do not run at all within a transaction.
  239. # This is not to be confused with implicit transaction, as they occur in MySQL or Oracle, where even though a
  240. # DDL statement was run within within a transaction, the database will issue an implicit commit before and after
  241. # its execution.
  242. # true if mixed migrations should be allowed. false if an error should be thrown instead. (default: false)
  243. # flyway.mixed=
  244. # Whether to group all pending migrations together in the same transaction when applying them
  245. # (only recommended for databases with support for DDL transactions).
  246. # true if migrations should be grouped. false if they should be applied individually instead. (default: false)
  247. # flyway.group=
  248. # The username that will be recorded in the schema history table as having applied the migration.
  249. # <<blank>> for the current database user of the connection. (default: <<blank>>).
  250. # flyway.installedBy=
  251. # Rules for the built-in error handler that let you override specific SQL states and errors codes in order to
  252. # force specific errors or warnings to be treated as debug messages, info messages, warnings or errors.
  253. # Each error override has the following format: STATE:12345:W.
  254. # It is a 5 character SQL state (or * to match all SQL states), a colon,
  255. # the SQL error code (or * to match all SQL error codes), a colon and finally
  256. # the desired behavior that should override the initial one.
  257. # The following behaviors are accepted:
  258. # - D to force a debug message
  259. # - D- to force a debug message, but do not show the original sql state and error code
  260. # - I to force an info message
  261. # - I- to force an info message, but do not show the original sql state and error code
  262. # - W to force a warning
  263. # - W- to force a warning, but do not show the original sql state and error code
  264. # - E to force an error
  265. # - E- to force an error, but do not show the original sql state and error code
  266. # Example 1: to force Oracle stored procedure compilation issues to produce
  267. # errors instead of warnings, the following errorOverride can be used: 99999:17110:E
  268. # Example 2: to force SQL Server PRINT messages to be displayed as info messages (without SQL state and error
  269. # code details) instead of warnings, the following errorOverride can be used: S0001:0:I-
  270. # Example 3: to force all errors with SQL error code 123 to be treated as warnings instead,
  271. # the following errorOverride can be used: *:123:W
  272. # Flyway Pro and Flyway Enterprise only
  273. # flyway.errorOverrides=
  274. # The file where to output the SQL statements of a migration dry run. If the file specified is in a non-existent
  275. # directory, Flyway will create all directories and parent directories as needed.
  276. # <<blank>> to execute the SQL statements directly against the database. (default: <<blank>>)
  277. # Flyway Pro and Flyway Enterprise only
  278. # flyway.dryRunOutput=
  279. # Whether to Flyway's support for Oracle SQL*Plus commands should be activated. (default: false)
  280. # Flyway Pro and Flyway Enterprise only
  281. # flyway.oracle.sqlplus=
  282. # Whether Flyway should issue a warning instead of an error whenever it encounters an Oracle SQL*Plus
  283. # statement it doesn't yet support. (default: false)
  284. # Flyway Pro and Flyway Enterprise only
  285. # flyway.oracle.sqlplusWarn=
  286. # Your Flyway license key (FL01...). Not yet a Flyway Pro or Enterprise Edition customer?
  287. # Request your Flyway trial license key st https://flywaydb.org/download/
  288. # to try out Flyway Pro and Enterprise Edition features free for 30 days.
  289. # Flyway Pro and Flyway Enterprise only
  290. # flyway.licenseKey=

然后在D:\software\java\flyway\flyway-6.4.2\sql 添加SQL文件

我添加了两个sql文件

具体如下所示

  1. /*
  2. Navicat Premium Data Transfer
  3. Source Server : localhost
  4. Source Server Type : MySQL
  5. Source Server Version : 80019
  6. Source Host : localhost:3306
  7. Source Schema : test
  8. Target Server Type : MySQL
  9. Target Server Version : 80019
  10. File Encoding : 65001
  11. Date: 24/05/2020 09:28:52
  12. */
  13. SET NAMES utf8mb4;
  14. SET FOREIGN_KEY_CHECKS = 0;
  15. -- ----------------------------
  16. -- Table structure for user
  17. -- ----------------------------
  18. DROP TABLE IF EXISTS `user`;
  19. CREATE TABLE `user` (
  20. `id` int(0) NOT NULL AUTO_INCREMENT,
  21. `name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
  22. `age` int(0) NOT NULL,
  23. PRIMARY KEY (`id`) USING BTREE
  24. ) ENGINE = InnoDB AUTO_INCREMENT = 14 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin COMMENT = '人员信息表' ROW_FORMAT = Dynamic;
  25. -- ----------------------------
  26. -- Records of user
  27. -- ----------------------------
  28. INSERT INTO `user` VALUES (1, 'zhangjie', 32);
  29. SET FOREIGN_KEY_CHECKS = 1;
  1. /*
  2. Navicat Premium Data Transfer
  3. Source Server : localhost
  4. Source Server Type : MySQL
  5. Source Server Version : 80019
  6. Source Host : localhost:3306
  7. Source Schema : test
  8. Target Server Type : MySQL
  9. Target Server Version : 80019
  10. File Encoding : 65001
  11. Date: 24/05/2020 09:35:26
  12. */
  13. SET NAMES utf8mb4;
  14. SET FOREIGN_KEY_CHECKS = 0;
  15. -- ----------------------------
  16. -- Table structure for person
  17. -- ----------------------------
  18. DROP TABLE IF EXISTS `person`;
  19. CREATE TABLE `person` (
  20. `id` int(0) NOT NULL AUTO_INCREMENT,
  21. `name` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
  22. `age` int(0) NOT NULL,
  23. PRIMARY KEY (`id`) USING BTREE
  24. ) ENGINE = InnoDB AUTO_INCREMENT = 3 CHARACTER SET = utf8mb4 COLLATE = utf8mb4_bin COMMENT = '人员信息表' ROW_FORMAT = Dynamic;
  25. -- ----------------------------
  26. -- Records of person
  27. -- ----------------------------
  28. INSERT INTO `person` VALUES (1, 'zhangjie', 32);
  29. INSERT INTO `person` VALUES (2, 'zhangsan', 27);
  30. SET FOREIGN_KEY_CHECKS = 1;

配置完成,执行flyway migrate命令即可。

然后去数据库看,表已经建好了

再次执行flyway migrate命令

因为当前sql文件与之前没有变化,所以不会执行任何sql。

PS:注意已经执行过的sql文件已经成为历史了,所以不可以修改不然会报错。只可以再新增sql文件进行执行。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/595668
推荐阅读
相关标签
  

闽ICP备14008679号