Use flyway to initialize sqlite database
This commit is contained in:
9
pom.xml
9
pom.xml
@@ -147,7 +147,7 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter</artifactId>
|
||||
<artifactId>mybatis-plus-spring-boot3-starter</artifactId>
|
||||
<version>3.5.4.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
@@ -160,11 +160,6 @@
|
||||
<artifactId>dynamic-datasource-spring-boot3-starter</artifactId>
|
||||
<version>4.2.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.flywaydb</groupId>
|
||||
<artifactId>flyway-core</artifactId>
|
||||
<version>${flyway.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.flywaydb</groupId>
|
||||
<artifactId>flyway-mysql</artifactId>
|
||||
@@ -176,7 +171,7 @@
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.baomidou</groupId>
|
||||
<artifactId>mybatis-plus-boot-starter-test</artifactId>
|
||||
<artifactId>mybatis-plus-spring-boot3-starter-test</artifactId>
|
||||
<version>3.5.4.1</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
@@ -48,10 +48,6 @@ fun main(args: Array<String>) {
|
||||
if (File("data/db/sqlite.db").exists() && !File("data/db/sqlite.db").delete()) {
|
||||
logger.error("Can not recreate database 'data/db/sqlite.db', please try again later.")
|
||||
}
|
||||
|
||||
FatWebApiApplication::class.java.getResourceAsStream("/db/sqlite.db")?.let {
|
||||
File("data/db/sqlite.db").writeBytes(it.readAllBytes())
|
||||
}
|
||||
}
|
||||
|
||||
if (File("application-config.yml").exists() || File("data/application-config.yml").exists()) {
|
||||
|
||||
43
src/main/kotlin/top/fatweb/api/config/FlywayConfig.kt
Normal file
43
src/main/kotlin/top/fatweb/api/config/FlywayConfig.kt
Normal file
@@ -0,0 +1,43 @@
|
||||
package top.fatweb.api.config
|
||||
|
||||
import com.baomidou.dynamic.datasource.DynamicRoutingDataSource
|
||||
import jakarta.annotation.PostConstruct
|
||||
import org.flywaydb.core.Flyway
|
||||
import org.springframework.context.annotation.Configuration
|
||||
import org.springframework.context.annotation.DependsOn
|
||||
import top.fatweb.api.properties.FlywayProperties
|
||||
import javax.sql.DataSource
|
||||
|
||||
/**
|
||||
* Flyway configuration
|
||||
*
|
||||
* @author FatttSnake, fatttsnake@gmail.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@DependsOn("flywayProperties")
|
||||
@Configuration
|
||||
class FlywayConfig(
|
||||
private val dataSource: DataSource
|
||||
) {
|
||||
@PostConstruct
|
||||
fun migrateOrder() {
|
||||
val ds = dataSource as DynamicRoutingDataSource
|
||||
ds.dataSources.forEach { (k: String, v: DataSource?) ->
|
||||
val flyway = Flyway.configure()
|
||||
.dataSource(v)
|
||||
.locations(*FlywayProperties.locations.map { "$it/$k" }.toTypedArray())
|
||||
.baselineOnMigrate(FlywayProperties.baselineOnMigrate)
|
||||
.table(FlywayProperties.table)
|
||||
.outOfOrder(FlywayProperties.outOfOrder)
|
||||
.validateOnMigrate(FlywayProperties.validateOnMigrate)
|
||||
.encoding(FlywayProperties.encoding)
|
||||
.sqlMigrationPrefix(FlywayProperties.sqlMigrationPrefix)
|
||||
.sqlMigrationSeparator(FlywayProperties.sqlMigrationSeparator)
|
||||
.sqlMigrationSuffixes(*FlywayProperties.sqlMigrationSuffixes.toTypedArray())
|
||||
.baselineVersion(FlywayProperties.baselineVersion)
|
||||
.load()
|
||||
flyway.migrate()
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package top.fatweb.api.entity.system
|
||||
import com.baomidou.mybatisplus.annotation.TableField
|
||||
import com.baomidou.mybatisplus.annotation.TableId
|
||||
import com.baomidou.mybatisplus.annotation.TableName
|
||||
import com.fasterxml.jackson.annotation.JsonFormat
|
||||
import java.io.Serializable
|
||||
import java.time.LocalDateTime
|
||||
|
||||
@@ -48,6 +49,7 @@ class SysLog : Serializable {
|
||||
* @since 1.0.0
|
||||
* @see LocalDateTime
|
||||
*/
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
|
||||
@TableField("operate_time")
|
||||
var operateTime: LocalDateTime? = null
|
||||
|
||||
@@ -121,6 +123,7 @@ class SysLog : Serializable {
|
||||
* @since 1.0.0
|
||||
* @see LocalDateTime
|
||||
*/
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
|
||||
@TableField("start_time")
|
||||
var startTime: LocalDateTime? = null
|
||||
|
||||
@@ -131,6 +134,7 @@ class SysLog : Serializable {
|
||||
* @since 1.0.0
|
||||
* @see LocalDateTime
|
||||
*/
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss.SSS")
|
||||
@TableField("end_time")
|
||||
var endTime: LocalDateTime? = null
|
||||
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
package top.fatweb.api.properties
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties
|
||||
import org.springframework.stereotype.Component
|
||||
|
||||
/**
|
||||
* Flyway properties
|
||||
*
|
||||
* @author FatttSnake, fatttsnake@gmail.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
@Component
|
||||
@ConfigurationProperties("spring.flyway")
|
||||
object FlywayProperties {
|
||||
/**
|
||||
* Locations of migrations scripts. Can contain the special "{vendor}" placeholder to
|
||||
* use vendor-specific locations.
|
||||
*
|
||||
* @author FatttSnake, fatttsnake@gmail.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
var locations = listOf("classpath:db/migration")
|
||||
|
||||
/**
|
||||
* Name of the schema history table that will be used by Flyway.
|
||||
*
|
||||
* @author FatttSnake, fatttsnake@gmail.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
var table = "flyway_schema_history"
|
||||
|
||||
/**
|
||||
* Whether to allow migrations to be run out of order.
|
||||
*
|
||||
* @author FatttSnake, fatttsnake@gmail.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
var outOfOrder = false
|
||||
|
||||
/**
|
||||
* Whether to automatically call validate when performing a migration.
|
||||
*
|
||||
* @author FatttSnake, fatttsnake@gmail.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
var validateOnMigrate = true
|
||||
|
||||
/**
|
||||
* Encoding of SQL migrations.
|
||||
*
|
||||
* @author FatttSnake, fatttsnake@gmail.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
var encoding = Charsets.UTF_8
|
||||
|
||||
/**
|
||||
* File name prefix for SQL migrations.
|
||||
*
|
||||
* @author FatttSnake, fatttsnake@gmail.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
var sqlMigrationPrefix = "V"
|
||||
|
||||
/**
|
||||
* File name separator for SQL migrations.
|
||||
*
|
||||
* @author FatttSnake, fatttsnake@gmail.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
var sqlMigrationSeparator = "__"
|
||||
|
||||
/**
|
||||
* File name suffix for SQL migrations.
|
||||
*
|
||||
* @author FatttSnake, fatttsnake@gmail.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
var sqlMigrationSuffixes = listOf(".sql")
|
||||
|
||||
/**
|
||||
* Whether to automatically call baseline when migrating a non-empty schema.
|
||||
*
|
||||
* @author FatttSnake, fatttsnake@gmail.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
var baselineOnMigrate = true
|
||||
|
||||
/**
|
||||
* Version to tag an existing schema with when executing baseline.
|
||||
*
|
||||
* @author FatttSnake, fatttsnake@gmail.com
|
||||
* @since 1.0.0
|
||||
*/
|
||||
var baselineVersion = "0"
|
||||
}
|
||||
@@ -49,11 +49,14 @@ class SysLogServiceImpl(
|
||||
it.operateUsername =
|
||||
it.operateUserId?.let { it1 -> userService.getOne(it1)?.username }
|
||||
}
|
||||
val userIds = sysLogIPage.records.map { it.operateUserId }
|
||||
userService.list(KtQueryWrapper(User()).select(User::id, User::username).`in`(User::id, userIds)).forEach { user ->
|
||||
sysLogIPage.records.forEach {
|
||||
if (it.operateUserId == user.id) {
|
||||
it.operateUsername = user.username
|
||||
if (sysLogIPage.records.isNotEmpty()) {
|
||||
val userIds = sysLogIPage.records.map { it.operateUserId }
|
||||
|
||||
userService.list(KtQueryWrapper(User()).select(User::id, User::username).`in`(User::id, userIds)).forEach { user ->
|
||||
sysLogIPage.records.forEach {
|
||||
if (it.operateUserId == user.id) {
|
||||
it.operateUsername = user.username
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,16 +17,16 @@ spring:
|
||||
sqlite:
|
||||
type: com.zaxxer.hikari.HikariDataSource
|
||||
driver-class-name: org.sqlite.JDBC
|
||||
url: jdbc:sqlite:data/db/sqlite.db?date_string_format=yyyy-MM-dd'T'HH:mm:ss.SSS
|
||||
url: jdbc:sqlite:data/db/sqlite.db
|
||||
|
||||
|
||||
jackson:
|
||||
date-format: yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
|
||||
time-zone: GMT
|
||||
flyway:
|
||||
enabled: false
|
||||
baseline-on-migrate: true
|
||||
baseline-version: 0
|
||||
clean-disabled: false
|
||||
|
||||
mybatis-plus:
|
||||
global-config:
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
drop table if exists t_sys_log;
|
||||
|
||||
create table t_sys_log -- 系统日志表
|
||||
(
|
||||
id bigint not null,
|
||||
log_type varchar(50) not null, -- 日志类型
|
||||
operate_user_id bigint not null, -- 操作用户
|
||||
operate_time datetime not null default (strftime('%Y-%m-%d %H:%M:%f','now')), -- 操作时间
|
||||
request_uri varchar(500) default null, -- 请求 URI
|
||||
request_method varchar(10) default null, -- 请求方式
|
||||
request_params text, -- 请求参数
|
||||
request_ip varchar(20) not null, -- 请求 IP
|
||||
request_server_address varchar(50) not null, -- 请求服务器地址
|
||||
exception int not null default 0, -- 是否异常
|
||||
exception_info text, -- 异常信息
|
||||
start_time datetime not null, -- 开始时间
|
||||
end_time datetime not null, -- 结束时间
|
||||
execute_time bigint default null, -- 执行时间
|
||||
user_agent varchar(500) default null -- 用户代理
|
||||
);
|
||||
Binary file not shown.
Reference in New Issue
Block a user