From bc76b0c7f9fa31ab9fd7151b318cea5f46680d75 Mon Sep 17 00:00:00 2001 From: luoweijian <1329394916@qq.com> Date: Fri, 11 Sep 2026 02:22:21 +0800 Subject: [PATCH] =?UTF-8?q?ticket01=E4=BF=AE=E6=AD=A3=E8=A1=A5=E5=85=85:?= =?UTF-8?q?=20opp=5Fid=20=E6=94=BE=E5=AE=BD=E4=B8=BA=E5=8F=AF=E7=A9=BA?= =?UTF-8?q?=E4=BB=85=E5=9C=A8=E5=88=97=E4=BB=8D=20NOT=20NULL=20=E6=97=B6?= =?UTF-8?q?=E6=89=A7=E8=A1=8C=EF=BC=88=E5=B9=82=E7=AD=89=EF=BC=8C=E9=81=BF?= =?UTF-8?q?=E5=85=8D=E5=85=B1=E4=BA=AB=E5=BA=93=E6=AF=8F=E6=AC=A1=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E9=87=8D=E5=A4=8D=20ALTER=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/SchemeCardOwnerMigrationRunner.java | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/crm-opportunity/src/main/java/com/crm/opportunity/config/SchemeCardOwnerMigrationRunner.java b/crm-opportunity/src/main/java/com/crm/opportunity/config/SchemeCardOwnerMigrationRunner.java index 5b8f70c..8655118 100644 --- a/crm-opportunity/src/main/java/com/crm/opportunity/config/SchemeCardOwnerMigrationRunner.java +++ b/crm-opportunity/src/main/java/com/crm/opportunity/config/SchemeCardOwnerMigrationRunner.java @@ -69,9 +69,12 @@ public class SchemeCardOwnerMigrationRunner implements CommandLineRunner { log.info("[scheme-card-owner-migration] 存量方案卡回填商机归属 {} 行", backfilled); // 放宽旧列为可空:实体已不映射 opp_id,新插卡不带该列——NOT NULL 会让 A3 新建卡 // 首次 INSERT 即失败(真实环境实测)。旧行值保留供审计,新行 NULL。 - jdbcTemplate.execute("ALTER TABLE " + TABLE + " MODIFY COLUMN opp_id bigint NULL" - + " COMMENT '(已废弃·owner 化解耦遗留)历史所属商机ID,新行恒 NULL'"); - log.info("[scheme-card-owner-migration] 旧列 opp_id 已放宽为可空"); + // 仅在列仍 NOT NULL 时执行(幂等:已放宽的库启动零 DDL,避免共享库反复 ALTER) + if (!columnNullable(conn, TABLE, "opp_id")) { + jdbcTemplate.execute("ALTER TABLE " + TABLE + " MODIFY COLUMN opp_id bigint NULL" + + " COMMENT '(已废弃·owner 化解耦遗留)历史所属商机ID,新行恒 NULL'"); + log.info("[scheme-card-owner-migration] 旧列 opp_id 已放宽为可空"); + } } if (!indexExists(conn, TABLE, "uk_osc_owner_customer")) { @@ -99,6 +102,13 @@ public class SchemeCardOwnerMigrationRunner implements CommandLineRunner { } } + private boolean columnNullable(Connection conn, String table, String column) throws SQLException { + DatabaseMetaData meta = conn.getMetaData(); + try (ResultSet rs = meta.getColumns(null, null, table, column)) { + return rs.next() && "YES".equalsIgnoreCase(rs.getString("IS_NULLABLE")); + } + } + private boolean indexExists(Connection conn, String table, String indexName) throws SQLException { DatabaseMetaData meta = conn.getMetaData(); try (ResultSet rs = meta.getIndexInfo(null, null, table, false, false)) {