feat: add calibre, fire rate, armour damage, and body damage fields to Firearm model and update related response and migration scripts

This commit is contained in:
2026-04-09 13:28:28 +08:00
parent 0992635391
commit bd1f2441f3
3 changed files with 112 additions and 0 deletions
@@ -8,6 +8,10 @@ public record FirearmResponse(
String name,
FirearmType type,
String level,
String calibre,
Integer fireRate,
Integer armourDamage,
Integer bodyDamage,
String review
) {
public static FirearmResponse from(Firearm firearm) {
@@ -16,6 +20,10 @@ public record FirearmResponse(
firearm.getName(),
firearm.getType(),
firearm.getLevel(),
firearm.getCalibre(),
firearm.getFireRate(),
firearm.getArmourDamage(),
firearm.getBodyDamage(),
firearm.getReview()
);
}
@@ -36,6 +36,18 @@ public class Firearm {
@Column(name = "review", columnDefinition = "TEXT")
private String review;
@Column(name = "calibre")
private String calibre;
@Column(name = "fire_rate")
private Integer fireRate;
@Column(name = "armour_damage")
private Integer armourDamage;
@Column(name = "body_damage")
private Integer bodyDamage;
@OneToMany(mappedBy = "firearm", cascade = CascadeType.ALL, orphanRemoval = true)
private List<Modification> modifications = new ArrayList<>();
@@ -79,6 +91,38 @@ public class Firearm {
this.review = review;
}
public String getCalibre() {
return calibre;
}
public void setCalibre(String calibre) {
this.calibre = calibre;
}
public Integer getFireRate() {
return fireRate;
}
public void setFireRate(Integer fireRate) {
this.fireRate = fireRate;
}
public Integer getArmourDamage() {
return armourDamage;
}
public void setArmourDamage(Integer armourDamage) {
this.armourDamage = armourDamage;
}
public Integer getBodyDamage() {
return bodyDamage;
}
public void setBodyDamage(Integer bodyDamage) {
this.bodyDamage = bodyDamage;
}
public List<Modification> getModifications() {
return modifications;
}
@@ -0,0 +1,60 @@
-- 创建新表
CREATE TABLE firearm_new
(
id BIGSERIAL NOT NULL,
name VARCHAR(64) NOT NULL,
type INTEGER NOT NULL,
level VARCHAR(10) NOT NULL,
calibre VARCHAR(20) NOT NULL,
fire_rate INTEGER NOT NULL,
armour_damage INTEGER NOT NULL,
body_damage INTEGER NOT NULL,
review TEXT NULL,
CONSTRAINT firearm_new_pkey PRIMARY KEY (id)
);
-- 迁移数据
INSERT INTO firearm_new(id, name, type, level, calibre, fire_rate, armour_damage, body_damage,
review)
SELECT id,
name,
type,
level,
calibre,
0,
armour_damage,
body_damage,
review
FROM firearm;
-- 处理外键(关键步骤)
-- 先删除指向旧表的外键约束
ALTER TABLE modification
DROP CONSTRAINT fk_modification_firearm;
-- 重命名旧表和索引
ALTER TABLE firearm
RENAME TO firearm_legacy;
ALTER INDEX firearm_pkey RENAME TO firearm_legacy_pkey;
-- 重命名新表和索引
ALTER TABLE firearm_new
RENAME TO firearm;
ALTER INDEX firearm_new_pkey RENAME TO firearm_pkey;
-- 重新建立外键,指向新的 firearm 表
ALTER TABLE modification
ADD CONSTRAINT fk_modification_firearm
FOREIGN KEY (firearm_id) REFERENCES firearm (id);
-- 序列所有权与名称修正
ALTER SEQUENCE firearm_id_seq RENAME TO firearm_legacy_id_seq;
ALTER SEQUENCE firearm_new_id_seq RENAME TO firearm_id_seq;
ALTER SEQUENCE firearm_id_seq OWNED BY firearm.id;
-- 更新序列计数器
SELECT setval('firearm_id_seq', coalesce(max(id), 1))
FROM firearm;
-- 删除旧表
DROP TABLE IF EXISTS firearm_legacy CASCADE;