Files
ExperionCrawler/fastTable/step3.md

3.4 KiB

STEP 3 — DB 초기화: 테이블 생성 + TimescaleDB hypertable

사전 확인 (작업 전 반드시 수행)

  1. src/Infrastructure/Database/ExperionDbContext.cs 파일을 열어 InitializeAsync() 메서드를 찾는다.
  2. 아래 항목을 확인하고 기록한다:
    • STEP 2가 완료되어 DbSet이 존재하는가? → 미완료면 STEP 2 먼저 수행
    • InitializeAsync() 메서드가 존재하는가? (없으면 구현 위치를 확인)
    • CREATE TABLE IF NOT EXISTS fast_session SQL이 이미 있는가? → 있으면 이 STEP 건너뜀
    • create_hypertable('fast_record'...) SQL이 이미 있는가? → 있으면 이 STEP 건너뜀
    • 파일 상단에 using System.Text.Json; import가 있는가? → 없으면 추가

작업 내용

파일: src/Infrastructure/Database/ExperionDbContext.cs
위치: ExperionDbService.InitializeAsync() 메서드 내부, 기존 초기화 코드 마지막 줄 아래

// ── fast_session / fast_record 테이블 생성 ────────────────────────────────
await _ctx.Database.ExecuteSqlRawAsync("""
    CREATE TABLE IF NOT EXISTS fast_session (
        id             SERIAL PRIMARY KEY,
        name           TEXT        NOT NULL,
        started_at     TIMESTAMPTZ NOT NULL,
        ended_at       TIMESTAMPTZ,
        status         TEXT        NOT NULL DEFAULT 'Pending',
        sampling_ms    INTEGER     NOT NULL,
        duration_sec   INTEGER     NOT NULL,
        tag_list       JSONB       NOT NULL DEFAULT '[]',
        row_count      INTEGER     NOT NULL DEFAULT 0,
        retention_days INTEGER,
        pinned         BOOLEAN     NOT NULL DEFAULT FALSE
    )
    """);

await _ctx.Database.ExecuteSqlRawAsync("""
    CREATE TABLE IF NOT EXISTS fast_record (
        id          SERIAL PRIMARY KEY,
        session_id  INTEGER     NOT NULL REFERENCES fast_session(id) ON DELETE CASCADE,
        recorded_at TIMESTAMPTZ NOT NULL,
        tagname     TEXT        NOT NULL,
        value       TEXT
    )
    """);

// TimescaleDB hypertable 생성 (recorded_at 기준, chunk_interval = 1 day)
await _ctx.Database.ExecuteSqlRawAsync("""
    SELECT create_hypertable('fast_record', 'recorded_at', if_not_exists => TRUE)
    """);

await _ctx.Database.ExecuteSqlRawAsync("""
    SELECT set_chunk_time_interval('fast_record', INTERVAL '1 day')
    """);

사후 확인 (작업 후 반드시 수행)

  1. ExperionDbContext.cs 파일을 다시 열어 추가된 SQL 블록을 읽는다.
  2. 아래 항목을 하나씩 확인한다:
    • CREATE TABLE IF NOT EXISTS fast_session SQL이 존재하는가?
    • CREATE TABLE IF NOT EXISTS fast_record SQL이 존재하는가?
    • fast_recordREFERENCES fast_session(id) ON DELETE CASCADE 가 있는가?
    • create_hypertable('fast_record', 'recorded_at', if_not_exists => TRUE) 가 있는가?
    • set_chunk_time_interval('fast_record', INTERVAL '1 day') 가 있는가?
  3. dotnet build src/Infrastructure 실행 → 에러/경고 0개 확인
  4. (가능하면) 앱을 실제로 실행해 DB에 fast_session, fast_record 테이블이 생성되는지 확인

완료 조건

  • dotnet build src/Infrastructure 결과: 에러 0, 경고 0 (기존 경고 포함)
  • SQL 5개 블록 모두 InitializeAsync 내에 존재
  • (선택) DB에서 \d fast_record 실행 시 hypertable 확인