sonamu 0.8.24 → 0.8.26
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/api/__tests__/config.test.js +189 -0
- package/dist/api/config.d.ts.map +1 -1
- package/dist/api/config.js +7 -2
- package/dist/api/sonamu.d.ts.map +1 -1
- package/dist/api/sonamu.js +14 -10
- package/dist/auth/index.d.ts +1 -0
- package/dist/auth/index.d.ts.map +1 -1
- package/dist/auth/index.js +2 -1
- package/dist/auth/knex-adapter.d.ts +23 -0
- package/dist/auth/knex-adapter.d.ts.map +1 -0
- package/dist/auth/knex-adapter.js +163 -0
- package/dist/auth/plugins/wrappers/admin.d.ts +2 -2
- package/dist/bin/__tests__/ts-loader-register.test.js +45 -0
- package/dist/bin/cli.js +47 -9
- package/dist/bin/ts-loader-register.js +3 -29
- package/dist/bin/ts-loader-registration.d.ts +2 -0
- package/dist/bin/ts-loader-registration.d.ts.map +1 -0
- package/dist/bin/ts-loader-registration.js +42 -0
- package/dist/cone/cone-generator.js +3 -3
- package/dist/database/puri-subset.test-d.js +9 -1
- package/dist/database/puri-subset.types.d.ts +1 -1
- package/dist/database/puri-subset.types.d.ts.map +1 -1
- package/dist/database/puri-subset.types.js +1 -1
- package/dist/testing/fixture-generator.js +5 -5
- package/dist/ui/ai-client.js +2 -2
- package/dist/ui/api.d.ts.map +1 -1
- package/dist/ui/api.js +14 -14
- package/dist/ui/cdd-service.d.ts +15 -18
- package/dist/ui/cdd-service.d.ts.map +1 -1
- package/dist/ui/cdd-service.js +246 -222
- package/dist/ui/cdd-types.d.ts +41 -68
- package/dist/ui/cdd-types.d.ts.map +1 -1
- package/dist/ui/cdd-types.js +2 -2
- package/dist/ui-web/assets/index-CKo0Z2Iu.css +1 -0
- package/dist/ui-web/assets/{index-CxiydzeC.js → index-DK-2aacv.js} +83 -83
- package/dist/ui-web/index.html +2 -2
- package/package.json +6 -2
- package/src/api/__tests__/config.test.ts +225 -0
- package/src/api/config.ts +10 -4
- package/src/api/sonamu.ts +16 -13
- package/src/auth/index.ts +1 -0
- package/src/auth/knex-adapter.ts +208 -0
- package/src/bin/__tests__/ts-loader-register.test.ts +62 -0
- package/src/bin/cli.ts +52 -9
- package/src/bin/ts-loader-register.ts +2 -32
- package/src/bin/ts-loader-registration.ts +55 -0
- package/src/cone/cone-generator.ts +2 -2
- package/src/database/puri-subset.test-d.ts +102 -0
- package/src/database/puri-subset.types.ts +1 -1
- package/src/skills/commands/sonamu-skills.md +20 -0
- package/src/skills/sonamu/SKILL.md +179 -137
- package/src/skills/sonamu/ai-agents.md +69 -69
- package/src/skills/sonamu/api.md +147 -147
- package/src/skills/sonamu/auth-migration.md +220 -220
- package/src/skills/sonamu/auth-plugins.md +83 -83
- package/src/skills/sonamu/auth.md +106 -106
- package/src/skills/sonamu/cdd.md +65 -200
- package/src/skills/sonamu/cone.md +138 -138
- package/src/skills/sonamu/config.md +191 -191
- package/src/skills/sonamu/create-sonamu.md +66 -66
- package/src/skills/sonamu/database.md +158 -158
- package/src/skills/sonamu/entity-basic.md +292 -293
- package/src/skills/sonamu/entity-relations.md +246 -246
- package/src/skills/sonamu/entity-validation-checklist.md +124 -124
- package/src/skills/sonamu/fixture-cli.md +231 -231
- package/src/skills/sonamu/framework-change.md +37 -37
- package/src/skills/sonamu/frontend.md +223 -223
- package/src/skills/sonamu/i18n.md +82 -82
- package/src/skills/sonamu/migration.md +77 -77
- package/src/skills/sonamu/model.md +222 -222
- package/src/skills/sonamu/naite.md +86 -86
- package/src/skills/sonamu/project-init.md +228 -228
- package/src/skills/sonamu/puri.md +122 -122
- package/src/skills/sonamu/scaffolding.md +154 -154
- package/src/skills/sonamu/skill-contribution.md +124 -124
- package/src/skills/sonamu/subset.md +46 -46
- package/src/skills/sonamu/tasks.md +82 -82
- package/src/skills/sonamu/testing-devrunner.md +147 -147
- package/src/skills/sonamu/testing.md +673 -673
- package/src/skills/sonamu/upsert.md +79 -79
- package/src/skills/sonamu/vector.md +67 -67
- package/src/testing/fixture-generator.ts +4 -4
- package/src/ui/ai-client.ts +1 -1
- package/src/ui/api.ts +18 -17
- package/src/ui/cdd-service.ts +264 -254
- package/src/ui/cdd-types.ts +40 -75
- package/dist/ui-web/assets/index-BrQKU3j9.css +0 -1
- package/src/skills/sonamu/workflow.md +0 -317
|
@@ -1,74 +1,74 @@
|
|
|
1
1
|
---
|
|
2
2
|
name: sonamu-database
|
|
3
|
-
description: Sonamu
|
|
3
|
+
description: Sonamu database configuration, Seed Data management, troubleshooting. Docker port conflicts, DB connection configuration. Use when database issues occur.
|
|
4
4
|
---
|
|
5
5
|
|
|
6
|
-
#
|
|
6
|
+
# Database Configuration
|
|
7
7
|
|
|
8
|
-
## Docker DB
|
|
8
|
+
## Starting Docker DB
|
|
9
9
|
|
|
10
10
|
```bash
|
|
11
11
|
cd packages/api
|
|
12
12
|
pnpm docker:up
|
|
13
13
|
```
|
|
14
14
|
|
|
15
|
-
## 3-Tier DB
|
|
15
|
+
## 3-Tier DB Structure
|
|
16
16
|
|
|
17
|
-
Sonamu
|
|
17
|
+
Sonamu uses a 3-tier database structure. It is important to understand the role of each DB and the data flow.
|
|
18
18
|
|
|
19
19
|
```
|
|
20
|
-
production/development master (
|
|
20
|
+
production/development master (actual DB)
|
|
21
21
|
↓ (fixture fetch)
|
|
22
22
|
project_fixture (fixture DB)
|
|
23
23
|
↓ (fixture sync)
|
|
24
24
|
project_test (test DB)
|
|
25
25
|
```
|
|
26
26
|
|
|
27
|
-
### DB
|
|
27
|
+
### Role of Each DB
|
|
28
28
|
|
|
29
|
-
| DB |
|
|
30
|
-
|
|
31
|
-
| `project` |
|
|
32
|
-
| `project_fixture` |
|
|
33
|
-
| `project_test` |
|
|
29
|
+
| DB | Purpose | Data Source | Command |
|
|
30
|
+
|----|---------|------------|---------|
|
|
31
|
+
| `project` | Production/development actual DB | Real user data | Created directly |
|
|
32
|
+
| `project_fixture` | Reference data store for testing | Fetched from production or generated with gen | `pnpm sonamu fixture gen/fetch` |
|
|
33
|
+
| `project_test` | Test execution environment | Synced from fixture | `pnpm sonamu fixture sync` |
|
|
34
34
|
|
|
35
|
-
###
|
|
35
|
+
### Data Flow
|
|
36
36
|
|
|
37
|
-
**1. fixture fetch (
|
|
37
|
+
**1. fixture fetch (fetch real data)**
|
|
38
38
|
```bash
|
|
39
39
|
pnpm sonamu fixture fetch --include User --limit 10
|
|
40
40
|
```
|
|
41
41
|
- production/development master → fixture DB
|
|
42
|
-
-
|
|
43
|
-
-
|
|
42
|
+
- Copies actual production data for testing
|
|
43
|
+
- Related data (FKs) is also fetched
|
|
44
44
|
|
|
45
|
-
**2. fixture gen (
|
|
45
|
+
**2. fixture gen (generate dummy data)**
|
|
46
46
|
```bash
|
|
47
47
|
pnpm sonamu fixture gen --include Department --count 5
|
|
48
48
|
```
|
|
49
|
-
- fixture DB
|
|
50
|
-
-
|
|
51
|
-
-
|
|
49
|
+
- Generated inside fixture DB using faker
|
|
50
|
+
- Automatically resolves reference relationships (FKs)
|
|
51
|
+
- Supports Korean data generation
|
|
52
52
|
|
|
53
|
-
**3. fixture sync (
|
|
53
|
+
**3. fixture sync (sync test DB)**
|
|
54
54
|
```bash
|
|
55
55
|
pnpm sonamu fixture sync
|
|
56
56
|
```
|
|
57
57
|
- fixture DB → test DB
|
|
58
|
-
-
|
|
59
|
-
-
|
|
58
|
+
- Sync to the latest state before running tests
|
|
59
|
+
- Each test is isolated with a transaction that auto-rolls back
|
|
60
60
|
|
|
61
|
-
###
|
|
61
|
+
### Notes
|
|
62
62
|
|
|
63
|
-
**CRITICAL: sourceDb vs targetDb
|
|
63
|
+
**CRITICAL: Prevent sourceDb vs targetDb confusion**
|
|
64
64
|
|
|
65
|
-
- `fixture gen`: sourceDb=fixture, targetDb=fixture (
|
|
65
|
+
- `fixture gen`: sourceDb=fixture, targetDb=fixture (generated inside fixture)
|
|
66
66
|
- `fixture fetch`: sourceDb=production, targetDb=fixture (production → fixture)
|
|
67
|
-
-
|
|
67
|
+
- Incorrect configuration causes FK reference errors
|
|
68
68
|
|
|
69
|
-
|
|
69
|
+
**Example (correct configuration)**:
|
|
70
70
|
```typescript
|
|
71
|
-
// fixture gen:
|
|
71
|
+
// fixture gen: reference and save within fixture DB
|
|
72
72
|
const fixtureDb = createKnexInstance(Sonamu.dbConfig.fixture);
|
|
73
73
|
const generator = new FixtureGenerator(fixtureDb, fixtureDb, "fixture", EntityManager);
|
|
74
74
|
|
|
@@ -78,182 +78,182 @@ const fixtureDb = createKnexInstance(Sonamu.dbConfig.fixture);
|
|
|
78
78
|
const generator = new FixtureGenerator(sourceDb, fixtureDb, "fixture", EntityManager);
|
|
79
79
|
```
|
|
80
80
|
|
|
81
|
-
|
|
81
|
+
**Note**: For detailed Fixture CLI command usage, see `fixture-cli.md`
|
|
82
82
|
|
|
83
83
|
---
|
|
84
84
|
|
|
85
|
-
## Seed Data
|
|
85
|
+
## Seed Data Management
|
|
86
86
|
|
|
87
|
-
|
|
87
|
+
Base data for testing (seed data) is managed by adding it to dump files.
|
|
88
88
|
|
|
89
|
-
###
|
|
89
|
+
### Overall Workflow
|
|
90
90
|
|
|
91
|
-
Seed data
|
|
91
|
+
Seed data management proceeds in 2 phases:
|
|
92
92
|
|
|
93
|
-
|
|
|
94
|
-
|
|
95
|
-
| **Phase 1** |
|
|
96
|
-
| **Phase 2** |
|
|
93
|
+
| Phase | Purpose | Target DB |
|
|
94
|
+
|-------|---------|-----------|
|
|
95
|
+
| **Phase 1** | Prepare seed for development/testing | `project_test`, `project_fixture` |
|
|
96
|
+
| **Phase 2** | Apply seed to the actual DB | `project` (actual DB) |
|
|
97
97
|
|
|
98
98
|
---
|
|
99
99
|
|
|
100
|
-
### Phase 1:
|
|
100
|
+
### Phase 1: Prepare Seed for Development/Testing
|
|
101
101
|
|
|
102
|
-
|
|
102
|
+
The step of preparing dummy data for testing during development.
|
|
103
103
|
|
|
104
|
-
#### 1-1.
|
|
104
|
+
#### 1-1. Generate Initial Dump (Table Structure Only)
|
|
105
105
|
|
|
106
106
|
```bash
|
|
107
107
|
pnpm dump
|
|
108
108
|
```
|
|
109
109
|
|
|
110
|
-
|
|
111
|
-
- CREATE TABLE
|
|
112
|
-
- CREATE SEQUENCE
|
|
110
|
+
The `database/scripts/dump.sql` generated at this point contains:
|
|
111
|
+
- CREATE TABLE statements
|
|
112
|
+
- CREATE SEQUENCE statements
|
|
113
113
|
- ALTER TABLE ... PRIMARY KEY
|
|
114
114
|
- ALTER TABLE ... FOREIGN KEY
|
|
115
|
-
- **INSERT
|
|
115
|
+
- **No INSERT statements** (no data yet)
|
|
116
116
|
|
|
117
|
-
#### 1-2.
|
|
117
|
+
#### 1-2. Add INSERT Statements to Dump File
|
|
118
118
|
|
|
119
|
-
`database/scripts/dump.sql`
|
|
119
|
+
Open `database/scripts/dump.sql` and add INSERT statements **before the FK CONSTRAINT** section.
|
|
120
120
|
|
|
121
|
-
|
|
121
|
+
**Important: Write in order considering FK dependency order**
|
|
122
122
|
```sql
|
|
123
|
-
--
|
|
123
|
+
-- Independent tables first
|
|
124
124
|
INSERT INTO public.institutions (id, created_at, name, code) VALUES
|
|
125
|
-
(1, '2024-01-01 00:00:00+09', '
|
|
125
|
+
(1, '2024-01-01 00:00:00+09', 'Headquarters', 'HQ');
|
|
126
126
|
|
|
127
|
-
--
|
|
127
|
+
-- Reference tables (referencing institutions)
|
|
128
128
|
INSERT INTO public.departments (id, created_at, name, code, institution_id) VALUES
|
|
129
|
-
(1, '2024-01-01 00:00:00+09', '
|
|
129
|
+
(1, '2024-01-01 00:00:00+09', 'Research', 'RND', 1);
|
|
130
130
|
|
|
131
|
-
--
|
|
131
|
+
-- Set sequence values (after INSERT)
|
|
132
132
|
SELECT pg_catalog.setval('public.institutions_id_seq', 1, true);
|
|
133
133
|
SELECT pg_catalog.setval('public.departments_id_seq', 1, true);
|
|
134
134
|
```
|
|
135
135
|
|
|
136
|
-
#### 1-3.
|
|
136
|
+
#### 1-3. Apply to Test DB
|
|
137
137
|
|
|
138
138
|
```bash
|
|
139
139
|
pnpm seed
|
|
140
140
|
```
|
|
141
141
|
|
|
142
|
-
`database/scripts/seed.sh
|
|
143
|
-
- `SOURCE_DB="${DATABASE_NAME}_test"` → dump.sql
|
|
142
|
+
`database/scripts/seed.sh` runs and:
|
|
143
|
+
- `SOURCE_DB="${DATABASE_NAME}_test"` → applies dump.sql to the test DB
|
|
144
144
|
|
|
145
|
-
#### 1-4.
|
|
145
|
+
#### 1-4. Sync to Fixture DB
|
|
146
146
|
|
|
147
147
|
```bash
|
|
148
148
|
pnpm sonamu fixture sync
|
|
149
149
|
```
|
|
150
150
|
|
|
151
|
-
test DB
|
|
151
|
+
Copies test DB data to the fixture DB.
|
|
152
152
|
|
|
153
153
|
---
|
|
154
154
|
|
|
155
|
-
### Phase 2:
|
|
155
|
+
### Phase 2: Apply Seed to Actual DB
|
|
156
156
|
|
|
157
157
|
**⚠️ CRITICAL WARNING:**
|
|
158
|
-
-
|
|
159
|
-
-
|
|
160
|
-
-
|
|
158
|
+
- This step inserts data into the actual DB (`project`)
|
|
159
|
+
- Existing data may be overwritten
|
|
160
|
+
- **You must confirm with the user before proceeding**
|
|
161
161
|
|
|
162
|
-
**Claude Code
|
|
162
|
+
**Claude Code Rules:**
|
|
163
163
|
```
|
|
164
|
-
|
|
165
|
-
1.
|
|
166
|
-
2.
|
|
167
|
-
3.
|
|
164
|
+
Before applying seed to the actual DB:
|
|
165
|
+
1. Ask the user: "Would you like to apply seed data to the actual database (project)?"
|
|
166
|
+
2. Only proceed when the user explicitly approves
|
|
167
|
+
3. Never run without approval
|
|
168
168
|
```
|
|
169
169
|
|
|
170
|
-
#### 2-1.
|
|
170
|
+
#### 2-1. Verify Current State
|
|
171
171
|
|
|
172
172
|
```bash
|
|
173
|
-
#
|
|
173
|
+
# Data must already be in test/fixture DBs
|
|
174
174
|
PGPASSWORD=1234 psql -h 0.0.0.0 -U postgres -d project_test -c "SELECT COUNT(*) FROM users;"
|
|
175
175
|
```
|
|
176
176
|
|
|
177
|
-
#### 2-2.
|
|
177
|
+
#### 2-2. Generate Final Dump
|
|
178
178
|
|
|
179
179
|
```bash
|
|
180
|
-
# test/fixture
|
|
180
|
+
# Generate a dump that includes test/fixture data
|
|
181
181
|
pnpm dump
|
|
182
182
|
```
|
|
183
183
|
|
|
184
|
-
|
|
184
|
+
This dump **includes INSERT statements** (the data added in step 1-2).
|
|
185
185
|
|
|
186
|
-
#### 2-3. seed.sh
|
|
186
|
+
#### 2-3. Modify seed.sh File
|
|
187
187
|
|
|
188
|
-
`database/scripts/seed.sh
|
|
188
|
+
Open `database/scripts/seed.sh` and change FIXTURE_DB:
|
|
189
189
|
|
|
190
190
|
```bash
|
|
191
|
-
#
|
|
191
|
+
# Before (development/testing phase)
|
|
192
192
|
FIXTURE_DB="${DATABASE_NAME}_fixture"
|
|
193
193
|
|
|
194
|
-
#
|
|
194
|
+
# After (seeding actual DB)
|
|
195
195
|
FIXTURE_DB="${DATABASE_NAME}"
|
|
196
196
|
```
|
|
197
197
|
|
|
198
|
-
#### 2-4.
|
|
198
|
+
#### 2-4. Run Seed on Actual DB
|
|
199
199
|
|
|
200
|
-
**⚠️
|
|
200
|
+
**⚠️ Run only after user approval:**
|
|
201
201
|
|
|
202
202
|
```bash
|
|
203
203
|
pnpm seed
|
|
204
204
|
```
|
|
205
205
|
|
|
206
|
-
|
|
206
|
+
Seed data is now applied to the actual DB (`project`).
|
|
207
207
|
|
|
208
|
-
#### 2-5.
|
|
208
|
+
#### 2-5. Verify
|
|
209
209
|
|
|
210
210
|
```bash
|
|
211
|
-
#
|
|
211
|
+
# Verify data in actual DB
|
|
212
212
|
PGPASSWORD=1234 psql -h 0.0.0.0 -U postgres -d project -c "SELECT * FROM departments LIMIT 5;"
|
|
213
213
|
```
|
|
214
214
|
|
|
215
|
-
#### 2-6. seed.sh
|
|
215
|
+
#### 2-6. Restore seed.sh (Important!)
|
|
216
216
|
|
|
217
|
-
|
|
217
|
+
After the actual DB seed is complete, restore seed.sh to its original state so the next development cycle uses the test DB:
|
|
218
218
|
|
|
219
219
|
```bash
|
|
220
220
|
# database/scripts/seed.sh
|
|
221
|
-
FIXTURE_DB="${DATABASE_NAME}_fixture" #
|
|
221
|
+
FIXTURE_DB="${DATABASE_NAME}_fixture" # restored
|
|
222
222
|
```
|
|
223
223
|
|
|
224
224
|
---
|
|
225
225
|
|
|
226
|
-
###
|
|
226
|
+
### Summary: Phase 1 vs Phase 2
|
|
227
227
|
|
|
228
|
-
|
|
|
229
|
-
|
|
230
|
-
|
|
|
231
|
-
| **
|
|
232
|
-
|
|
|
228
|
+
| Item | Phase 1 (Development/Testing) | Phase 2 (Actual DB) |
|
|
229
|
+
|------|-------------------------------|---------------------|
|
|
230
|
+
| **Timing** | Preparing test data during development | Preparing actual data after development is complete |
|
|
231
|
+
| **Dump count** | 1 time (table structure) | 2 times (includes data) |
|
|
232
|
+
| **Target DB** | `project_test` → `project_fixture` | `project` |
|
|
233
233
|
| **seed.sh** | `FIXTURE_DB="${DATABASE_NAME}_fixture"` | `FIXTURE_DB="${DATABASE_NAME}"` |
|
|
234
|
-
|
|
|
234
|
+
| **User approval** | Not required | **Required** |
|
|
235
235
|
|
|
236
236
|
---
|
|
237
237
|
|
|
238
|
-
###
|
|
238
|
+
### Legacy Workflow (Phase 1 Simple Version)
|
|
239
239
|
|
|
240
240
|
```bash
|
|
241
|
-
# 1.
|
|
241
|
+
# 1. Directly add base data to test DB (using psql or Sonamu UI)
|
|
242
242
|
PGPASSWORD=1234 psql -h 0.0.0.0 -U postgres -d project_test
|
|
243
243
|
|
|
244
|
-
# 2. dump
|
|
244
|
+
# 2. Generate dump
|
|
245
245
|
pnpm dump
|
|
246
246
|
|
|
247
|
-
# 3. fixture DB
|
|
247
|
+
# 3. Apply to fixture DB
|
|
248
248
|
pnpm seed
|
|
249
249
|
|
|
250
|
-
# 4. (
|
|
250
|
+
# 4. (Optional) sonamu fixture sync
|
|
251
251
|
pnpm sonamu fixture sync
|
|
252
252
|
```
|
|
253
253
|
|
|
254
|
-
###
|
|
254
|
+
### Position for Adding Seed Data in Dump File
|
|
255
255
|
|
|
256
|
-
**pg_dump --inserts
|
|
256
|
+
**pg_dump --inserts output order (based on miomock):**
|
|
257
257
|
|
|
258
258
|
```sql
|
|
259
259
|
-- 1~40: SET statements & Extensions
|
|
@@ -273,9 +273,9 @@ ALTER SEQUENCE public.companies_id_seq OWNED BY public.companies.id;
|
|
|
273
273
|
-- 480~564: ALTER TABLE ... DEFAULT (Name: xxx id; Type: DEFAULT)
|
|
274
274
|
ALTER TABLE ONLY public.companies ALTER COLUMN id SET DEFAULT nextval(...);
|
|
275
275
|
|
|
276
|
-
-- ⭐ 574~1770: INSERT INTO ... ← SEED DATA
|
|
277
|
-
INSERT INTO public.companies VALUES (1, '2025-11-25 00:17:02+09', '
|
|
278
|
-
INSERT INTO public.departments VALUES (1, '2024-01-01 01:00:00+09', '
|
|
276
|
+
-- ⭐ 574~1770: INSERT INTO ... ← SEED DATA INSERTION POSITION
|
|
277
|
+
INSERT INTO public.companies VALUES (1, '2025-11-25 00:17:02+09', 'Tech Corp');
|
|
278
|
+
INSERT INTO public.departments VALUES (1, '2024-01-01 01:00:00+09', 'Dev Team', 1, NULL, DEFAULT);
|
|
279
279
|
INSERT INTO public.employees VALUES (1, '2024-01-01 01:00:00+09', 1, 3, 'EMP001', 75000.00, ...);
|
|
280
280
|
|
|
281
281
|
-- 1775~1862: SELECT pg_catalog.setval (Name: xxx_id_seq; Type: SEQUENCE SET)
|
|
@@ -287,86 +287,86 @@ ALTER TABLE ONLY public.companies ADD CONSTRAINT companies_pkey PRIMARY KEY (id)
|
|
|
287
287
|
-- 2010~2017: CREATE INDEX
|
|
288
288
|
CREATE INDEX projects_name_description_pgroonga_index ON public.projects ...;
|
|
289
289
|
|
|
290
|
-
--
|
|
290
|
+
-- 2024~: ALTER TABLE ... FOREIGN KEY (FK constraint - data must exist before this!)
|
|
291
291
|
ALTER TABLE ONLY public.departments
|
|
292
292
|
ADD CONSTRAINT departments_company_id_foreign FOREIGN KEY (company_id) REFERENCES public.companies(id);
|
|
293
293
|
```
|
|
294
294
|
|
|
295
|
-
### CRITICAL: Seed Data
|
|
295
|
+
### CRITICAL: Seed Data Placement Rule
|
|
296
296
|
|
|
297
|
-
**
|
|
297
|
+
**Seed data must be added before FK CONSTRAINTs.**
|
|
298
298
|
|
|
299
|
-
|
|
|
300
|
-
|
|
301
|
-
| FK CONSTRAINT
|
|
302
|
-
| FK CONSTRAINT
|
|
299
|
+
| Position | Result |
|
|
300
|
+
|----------|--------|
|
|
301
|
+
| Before FK CONSTRAINT | OK - FK check after data insertion |
|
|
302
|
+
| After FK CONSTRAINT | FAIL - FK violation because referenced table has no data |
|
|
303
303
|
|
|
304
|
-
###
|
|
304
|
+
### Table Dependency Order
|
|
305
305
|
|
|
306
|
-
|
|
306
|
+
Seed data INSERT order must follow **FK dependencies**:
|
|
307
307
|
|
|
308
308
|
```sql
|
|
309
|
-
-- 1.
|
|
310
|
-
INSERT INTO public.institutions (id, name, code) VALUES (1, '
|
|
309
|
+
-- 1. Independent tables first (tables without FKs)
|
|
310
|
+
INSERT INTO public.institutions (id, name, code) VALUES (1, 'Headquarters', 'HQ');
|
|
311
311
|
|
|
312
|
-
-- 2.
|
|
313
|
-
INSERT INTO public.departments (id, name, institution_id) VALUES (1, '
|
|
312
|
+
-- 2. Tables referencing step 1
|
|
313
|
+
INSERT INTO public.departments (id, name, institution_id) VALUES (1, 'Research', 1);
|
|
314
314
|
|
|
315
|
-
-- 3. 1
|
|
316
|
-
INSERT INTO public.users (id, name, institution_id, department_id) VALUES (1, '
|
|
315
|
+
-- 3. Tables referencing steps 1 and 2
|
|
316
|
+
INSERT INTO public.users (id, name, institution_id, department_id) VALUES (1, 'Admin', 1, 1);
|
|
317
317
|
```
|
|
318
318
|
|
|
319
|
-
###
|
|
319
|
+
### Setting Sequence Values
|
|
320
320
|
|
|
321
|
-
seed data
|
|
321
|
+
After adding seed data, the sequence current values must also be updated:
|
|
322
322
|
|
|
323
323
|
```sql
|
|
324
|
-
--
|
|
325
|
-
SELECT pg_catalog.setval('public.users_id_seq', 10, true); --
|
|
324
|
+
-- Set sequence to after the maximum id in seed data
|
|
325
|
+
SELECT pg_catalog.setval('public.users_id_seq', 10, true); -- next id starts from 11
|
|
326
326
|
SELECT pg_catalog.setval('public.departments_id_seq', 5, true);
|
|
327
327
|
```
|
|
328
328
|
|
|
329
|
-
###
|
|
329
|
+
### Example: Minimal Seed Data
|
|
330
330
|
|
|
331
331
|
```sql
|
|
332
|
-
-- institutions (
|
|
332
|
+
-- institutions (independent)
|
|
333
333
|
INSERT INTO public.institutions (id, created_at, name, code) VALUES
|
|
334
|
-
(1, '2024-01-01 00:00:00+09', '
|
|
334
|
+
(1, '2024-01-01 00:00:00+09', 'Headquarters', 'HQ');
|
|
335
335
|
|
|
336
|
-
-- departments (institutions
|
|
336
|
+
-- departments (references institutions)
|
|
337
337
|
INSERT INTO public.departments (id, created_at, name, code, institution_id, is_active) VALUES
|
|
338
|
-
(1, '2024-01-01 00:00:00+09', '
|
|
338
|
+
(1, '2024-01-01 00:00:00+09', 'Research', 'RND', 1, true);
|
|
339
339
|
|
|
340
|
-
--
|
|
340
|
+
-- Set sequences
|
|
341
341
|
SELECT pg_catalog.setval('public.institutions_id_seq', 1, true);
|
|
342
342
|
SELECT pg_catalog.setval('public.departments_id_seq', 1, true);
|
|
343
343
|
```
|
|
344
344
|
|
|
345
345
|
---
|
|
346
346
|
|
|
347
|
-
##
|
|
347
|
+
## Resolving Port Conflicts
|
|
348
348
|
|
|
349
|
-
|
|
349
|
+
If a port-already-in-use error occurs when running `pnpm docker:up`:
|
|
350
350
|
|
|
351
|
-
### 1
|
|
351
|
+
### Step 1: Check Running Containers
|
|
352
352
|
|
|
353
353
|
```bash
|
|
354
354
|
docker ps --format "table {{.Names}}\t{{.Ports}}"
|
|
355
355
|
```
|
|
356
356
|
|
|
357
|
-
### 2
|
|
357
|
+
### Step 2: Compare Container Names
|
|
358
358
|
|
|
359
|
-
|
|
359
|
+
**Check the current project's container name:**
|
|
360
360
|
```bash
|
|
361
|
-
# packages/api/.env
|
|
361
|
+
# Check CONTAINER_NAME in packages/api/.env
|
|
362
362
|
cat packages/api/.env | grep CONTAINER_NAME
|
|
363
363
|
```
|
|
364
364
|
|
|
365
|
-
### 3
|
|
365
|
+
### Step 3: Handle by Situation
|
|
366
366
|
|
|
367
|
-
####
|
|
367
|
+
#### If Container Names are the Same
|
|
368
368
|
|
|
369
|
-
|
|
369
|
+
A container from the same project was started earlier. Bring it down and bring it back up:
|
|
370
370
|
|
|
371
371
|
```bash
|
|
372
372
|
cd packages/api
|
|
@@ -374,67 +374,67 @@ pnpm docker:down
|
|
|
374
374
|
pnpm docker:up
|
|
375
375
|
```
|
|
376
376
|
|
|
377
|
-
####
|
|
377
|
+
#### If Container Names are Different
|
|
378
378
|
|
|
379
|
-
|
|
379
|
+
Another project is using the same port. The new project's port must be changed.
|
|
380
380
|
|
|
381
|
-
|
|
381
|
+
**Two files to modify:**
|
|
382
382
|
|
|
383
383
|
1. `packages/api/.env`
|
|
384
384
|
```bash
|
|
385
|
-
#
|
|
385
|
+
# Before
|
|
386
386
|
DB_PORT=5432
|
|
387
387
|
|
|
388
|
-
#
|
|
388
|
+
# After (use an unused port between 5433–5439)
|
|
389
389
|
DB_PORT=5433
|
|
390
390
|
```
|
|
391
391
|
|
|
392
392
|
2. `packages/api/database/docker-compose.yml`
|
|
393
393
|
```yaml
|
|
394
|
-
#
|
|
394
|
+
# Before
|
|
395
395
|
ports:
|
|
396
396
|
- "5432:5432"
|
|
397
397
|
|
|
398
|
-
#
|
|
398
|
+
# After
|
|
399
399
|
ports:
|
|
400
400
|
- "5433:5432"
|
|
401
401
|
```
|
|
402
402
|
|
|
403
403
|
3. `packages/api/src/sonamu.config.ts`
|
|
404
404
|
```typescript
|
|
405
|
-
//
|
|
405
|
+
// Before
|
|
406
406
|
port: 5432,
|
|
407
407
|
|
|
408
|
-
//
|
|
408
|
+
// After
|
|
409
409
|
port: 5433,
|
|
410
410
|
```
|
|
411
411
|
|
|
412
|
-
|
|
413
|
-
- PostgreSQL
|
|
414
|
-
-
|
|
415
|
-
- `docker ps
|
|
412
|
+
**Port Selection Guide:**
|
|
413
|
+
- PostgreSQL default port: 5432
|
|
414
|
+
- Available range: 5433 ~ 5439
|
|
415
|
+
- Check currently used ports with `docker ps` and choose a number that is not duplicated
|
|
416
416
|
|
|
417
|
-
###
|
|
417
|
+
### Restart After Changes
|
|
418
418
|
|
|
419
419
|
```bash
|
|
420
420
|
pnpm docker:up
|
|
421
421
|
```
|
|
422
422
|
|
|
423
|
-
## DB
|
|
423
|
+
## DB Connection Configuration Files
|
|
424
424
|
|
|
425
|
-
|
|
|
426
|
-
|
|
427
|
-
| `packages/api/.env` |
|
|
428
|
-
| `packages/api/database/docker-compose.yml` | Docker
|
|
429
|
-
| `packages/api/src/sonamu.config.ts` | Sonamu DB
|
|
425
|
+
| File | Purpose |
|
|
426
|
+
|------|---------|
|
|
427
|
+
| `packages/api/.env` | Environment variables (DB_HOST, DB_PORT, DB_USER, etc.) |
|
|
428
|
+
| `packages/api/database/docker-compose.yml` | Docker container configuration |
|
|
429
|
+
| `packages/api/src/sonamu.config.ts` | Sonamu DB connection configuration |
|
|
430
430
|
|
|
431
|
-
## .env
|
|
431
|
+
## .env Default Settings
|
|
432
432
|
|
|
433
433
|
```bash
|
|
434
434
|
DB_HOST=0.0.0.0
|
|
435
435
|
DB_PORT=5432
|
|
436
436
|
DB_USER=postgres
|
|
437
437
|
DB_PASSWORD=1234
|
|
438
|
-
CONTAINER_NAME=[
|
|
439
|
-
DATABASE_NAME=[
|
|
438
|
+
CONTAINER_NAME=[project-name]-container
|
|
439
|
+
DATABASE_NAME=[project-name]
|
|
440
440
|
```
|