搜索

PythonReactSEOPDF
GitHub Skill

add-database-table

Create database migrations, define table schemas, and manage constant tables (enums). Use when: (1) creating a new database table, (2) running 'make migrate.create', (3) adding enum/status values, (4) modifying table structure. REQUIRED first step before add-domain-entity.

abyssparanoia☆ 18 仓库 Stars2 仓库 Forks

安装与使用

请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。

查看来源 ↗使用指南

技能说明

Add Database Table

Create database migrations and constant tables for new entities.

Quick Start

# 1. Create migration file
make migrate.create

# 2. Edit the generated SQL file in db/{database}/migrations/

# 3. Run migration and generate SQLBoiler models
make migrate.up

Overview

add-database-table ──> add-domain-entity ──> add-api-endpoint
       ^
   YOU ARE HERE

This skill is Step 1 of the CRUD implementation workflow.

Workflow

Step 1: Create Migration File

Run the migration creation command:

make migrate.create

Enter a descriptive name when prompted (e.g., create_examples_table).

A new file is created at: db/postgresql/migrations/{timestamp}_{name}.sql

Step 2: Define Constant Tables (if needed)

If the entity has status/enum fields, create the constant table first.

Migration SQL:

-- +goose Up
CREATE TABLE "example_statuses" (
    "id" VARCHAR(64) PRIMARY KEY
);

-- +goose Down
DROP TABLE IF EXISTS "example_statuses";

YAML Definition in db/postgresql/constants/constants.yaml:

- table: example_statuses
  values:
    - draft
    - published
    - archived
Step 3: Write Main Table Migration

Use the template below, adapting field names and types:

-- +goose Up
CREATE TABLE "examples" (
    "id"            VARCHAR(64)     PRIMARY KEY,
    "tenant_id"     VARCHAR(64)     NOT NULL,
    "name"          VARCHAR(256)    NOT NULL,
    "description"   TEXT            NOT NULL,
    "status"        VARCHAR(64)     NOT NULL,
    "created_at"    TIMESTAMPTZ     NOT NULL,
    "updated_at"    TIMESTAMPTZ     NOT NULL,

    CONSTRAINT "examples_fkey_tenant_id"
        FOREIGN KEY ("tenant_id") REFERENCES "tenants" ("id"),
    CONSTRAINT "examples_fkey_status"
        FOREIGN KEY ("status") REFERENCES "example_statuses" ("id")
);

CREATE INDEX "examples_idx_tenant_id" ON "examples" ("tenant_id");
CREATE INDEX "examples_idx_status" ON "examples" ("status");
CREATE INDEX "examples_idx_created_at" ON "examples" ("created_at" DESC);

-- +goose Down
DROP TABLE IF EXISTS "examples";

See references/sql-patterns.md for advanced patterns.

Step 4: Run Migration
make migrate.up

This command:

  • Executes pending migrations
  • Syncs constant table values from YAML
  • Generates SQLBoiler models in internal/infrastructure/{database}/internal/dbmodel/
Step 5: Verify

Confirm the SQLBoiler model was generated:

internal/infrastructure/postgresql/internal/dbmodel/examples.go

Quick Reference

Column Types
Go Type PostgreSQL Notes
string (ID) VARCHAR(64) Primary/foreign keys
string (short) VARCHAR(256) Names, titles
string (long) TEXT Descriptions
int INTEGER Counts, order
bool BOOLEAN Flags
time.Time TIMESTAMPTZ Always with timezone
null.Time TIMESTAMPTZ Nullable timestamps
map/struct JSONB Flexible data

See references/type-mappings.md for complete mappings.

Naming Conventions
Type Pattern Example
Foreign Key {table}_fkey_{column} examples_fkey_tenant_id
Index {table}_idx_{column} examples_idx_tenant_id
Unique {table}_uq_{columns} examples_uq_tenant_id_name
Unique Index {table}_uidx_{column} examples_uidx_email

Checklist

  • Migration file created with Up and Down sections
  • Constant table created (if entity has status/enum)
  • YAML constants defined in db/postgresql/constants/constants.yaml
  • Foreign key constraints added
  • Indexes created for foreign keys and common queries
  • make migrate.up executed successfully
  • SQLBoiler model generated in dbmodel/

Next Step

Proceed to add-domain-entity skill to create:

  • Domain model (internal/domain/model/)
  • Repository interface (internal/domain/repository/)
  • Repository implementation
  • Marshaller

使用示例

查看来源仓库中的使用示例与配置说明。

查看来源 ↗

来源历史

收录时间
2026-01-20
索引更新时间
2026-01-26
Branch
main

收录与同步日期属于本站记录。源码修改历史请查看来源仓库。

GitHub ↗

chat Comments (0)

chat_bubble_outline

No comments yet. Be the first to share your thoughts!