fix(db): align migrations with entity model and idempotency

This commit is contained in:
2026-07-06 18:16:24 +08:00
parent 774c0ff8dd
commit 91d96e0819
4 changed files with 18 additions and 5 deletions

View File

@@ -3,6 +3,7 @@ package com.sino.mci.config;
import javax.sql.DataSource; import javax.sql.DataSource;
import org.flywaydb.core.Flyway; import org.flywaydb.core.Flyway;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@@ -12,11 +13,14 @@ import org.springframework.context.annotation.Configuration;
public class FlywayConfig { public class FlywayConfig {
@Bean(initMethod = "migrate") @Bean(initMethod = "migrate")
public Flyway flyway(DataSource dataSource) { public Flyway flyway(
DataSource dataSource,
@Value("${spring.flyway.locations:classpath:db/migration}") String[] locations,
@Value("${spring.flyway.baseline-on-migrate:true}") boolean baselineOnMigrate) {
return Flyway.configure() return Flyway.configure()
.dataSource(dataSource) .dataSource(dataSource)
.locations("classpath:db/migration") .locations(locations)
.baselineOnMigrate(true) .baselineOnMigrate(baselineOnMigrate)
.load(); .load();
} }
} }

View File

@@ -16,7 +16,8 @@ CREATE TABLE IF NOT EXISTS mci_platform_role (
role_name VARCHAR(128) NOT NULL COMMENT '角色名称', role_name VARCHAR(128) NOT NULL COMMENT '角色名称',
permissions JSON COMMENT '权限列表', permissions JSON COMMENT '权限列表',
status TINYINT NOT NULL DEFAULT 1, status TINYINT NOT NULL DEFAULT 1,
create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
update_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='平台角色表'; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='平台角色表';
CREATE TABLE IF NOT EXISTS mci_user ( CREATE TABLE IF NOT EXISTS mci_user (
@@ -41,3 +42,7 @@ CREATE TABLE IF NOT EXISTS mci_user_role (
create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, create_time DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY uk_user_role (user_id, role_id) UNIQUE KEY uk_user_role (user_id, role_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户角色关系表'; ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COMMENT='用户角色关系表';
ALTER TABLE mci_user_role
ADD CONSTRAINT fk_user_role_user_id FOREIGN KEY (user_id) REFERENCES mci_user (id),
ADD CONSTRAINT fk_user_role_role_id FOREIGN KEY (role_id) REFERENCES mci_platform_role (id);

View File

@@ -1,4 +1,4 @@
INSERT INTO mci_platform_role (role_code, role_name, permissions) VALUES INSERT IGNORE INTO mci_platform_role (role_code, role_name, permissions) VALUES
('admin', '租户管理员', '["*"]'), ('admin', '租户管理员', '["*"]'),
('operator', '运营操作员', '["send:write", "channel:read", "channel:write", "record:read"]'), ('operator', '运营操作员', '["send:write", "channel:read", "channel:write", "record:read"]'),
('viewer', '只读用户', '["record:read", "channel:read"]'); ('viewer', '只读用户', '["record:read", "channel:read"]');

View File

@@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType; import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField; import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId; import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableLogic;
import lombok.Getter; import lombok.Getter;
import lombok.Setter; import lombok.Setter;
@@ -24,4 +25,7 @@ public abstract class BaseEntity implements Serializable {
@TableField(fill = FieldFill.INSERT_UPDATE) @TableField(fill = FieldFill.INSERT_UPDATE)
private LocalDateTime updateTime; private LocalDateTime updateTime;
@TableLogic
private Integer deleted;
} }