|
| 1 | +package dev.felnull.pointed.teams.database; |
| 2 | + |
| 3 | +import dev.felnull.pointed.core.database.Db; |
| 4 | +import dev.felnull.pointed.core.database.Names; |
| 5 | + |
| 6 | +import java.sql.Connection; |
| 7 | +import java.sql.SQLException; |
| 8 | +import java.sql.Statement; |
| 9 | +import java.util.logging.Logger; |
| 10 | + |
| 11 | +import static dev.felnull.pointed.core.database.TableInitializer.*; |
| 12 | + |
| 13 | +public class TeamTableInitializer { |
| 14 | + private static final Logger LOGGER = Logger.getLogger("Pointed"); |
| 15 | + public static void initTables() { |
| 16 | + try (Connection conn = Db.get().getConnection(); |
| 17 | + Statement stmt = conn.createStatement()) { |
| 18 | + |
| 19 | + // ========== 1) 骨格だけCREATE(最小列) ========== |
| 20 | + // team_meta(骨格) |
| 21 | + stmt.executeUpdate("CREATE TABLE IF NOT EXISTS " + Names.t("team_meta") + " (" + |
| 22 | + " subject_id BIGINT NOT NULL," + |
| 23 | + " PRIMARY KEY (subject_id)" + |
| 24 | + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4"); |
| 25 | + |
| 26 | + // ========== 2) 不足カラムを追加 ========== |
| 27 | + // team_meta |
| 28 | + addColumnIfNotExists(conn, "team_meta", "color_code", "VARCHAR(16) NOT NULL"); |
| 29 | + addColumnIfNotExists(conn, "team_meta", "sort_order", "INT NOT NULL DEFAULT 0"); |
| 30 | + addColumnIfNotExists(conn, "team_meta", "active", "TINYINT(1) NOT NULL DEFAULT 1"); |
| 31 | + |
| 32 | + // ========== 3) インデックス・制約 ========== |
| 33 | + |
| 34 | + // subjects(id) へのFK。subjects 削除時に team_meta も自動削除 |
| 35 | + ensureForeignKey(conn, "team_meta", "fk_tm_subject", |
| 36 | + "subject_id", "subjects", "id", "CASCADE", "CASCADE"); |
| 37 | + |
| 38 | + // 並び順・有効/無効フィルタの補助INDEX |
| 39 | + ensureIndex(conn, "team_meta", "idx_tm_sort", new String[]{"sort_order"}); |
| 40 | + ensureIndex(conn, "team_meta", "idx_tm_active", new String[]{"active"}); |
| 41 | + |
| 42 | + LOGGER.info("[Pointed] テーブル初期化完了!"); |
| 43 | + |
| 44 | + } catch (SQLException e) { |
| 45 | + LOGGER.warning("[Pointed] テーブル初期化中にエラー: " + e.getMessage()); |
| 46 | + } |
| 47 | + } |
| 48 | +} |
0 commit comments