Use flyway to initialize sqlite database
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user