@darkj/create-db 1.0.0 → 1.1.1
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/LICENSE +21 -0
- package/README.md +24 -5
- package/dist/builders/readme.js +87 -0
- package/dist/generator.js +17 -4
- package/dist/index.js +18 -1
- package/package.json +34 -11
- package/templates/base/.claude/CLAUDE.md +44 -35
- package/templates/base/.claude/commands/analyze.md +15 -15
- package/templates/base/.claude/commands/backup.md +9 -9
- package/templates/base/.claude/commands/build.md +7 -7
- package/templates/base/.claude/commands/new-dictionary.md +29 -26
- package/templates/base/.claude/commands/new-migration.md +33 -32
- package/templates/base/.claude/rules/migrations.md +65 -58
- package/templates/base/.claude/rules/schema.md +134 -129
- package/templates/base/init.sql +2 -2
- package/templates/base/schema/init.sql +5 -5
- package/templates/base/scripts/migrate.ts +18 -16
- package/templates/base/scripts/rollback.ts +21 -21
- package/templates/base/README.md +0 -117
|
@@ -1,60 +1,61 @@
|
|
|
1
|
-
|
|
1
|
+
Create a new migration for this project following the folder format.
|
|
2
2
|
|
|
3
|
-
##
|
|
3
|
+
## Step 1 — Determine the next number
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
List the folders in `migrations/` in alphabetical order.
|
|
6
|
+
The next number is the last one + 1, zero-padded (e.g. if `004_...` exists, the next is `005`).
|
|
7
7
|
|
|
8
|
-
##
|
|
8
|
+
## Step 2 — Get information
|
|
9
9
|
|
|
10
|
-
|
|
11
|
-
-
|
|
12
|
-
-
|
|
10
|
+
If the user didn't provide it, ask:
|
|
11
|
+
- What change do you want to make?
|
|
12
|
+
- Is there context or a reason behind the change? (to decide whether to create a README.md)
|
|
13
13
|
|
|
14
|
-
|
|
14
|
+
If the user passed a SQL script directly, use it as the base for `up.sql` — order and clean it up if needed.
|
|
15
15
|
|
|
16
|
-
##
|
|
16
|
+
## Step 3 — Create the folder
|
|
17
17
|
|
|
18
|
-
|
|
19
|
-
-
|
|
20
|
-
-
|
|
18
|
+
Name: `NNN_YYYYMMDD_HHMM_short_purpose`
|
|
19
|
+
- Current date and time
|
|
20
|
+
- Purpose in snake_case, short (3-5 words max)
|
|
21
21
|
|
|
22
22
|
```
|
|
23
|
-
migrations/
|
|
23
|
+
migrations/NNN_YYYYMMDD_HHMM_short_purpose/
|
|
24
24
|
├── up.sql
|
|
25
25
|
├── down.sql
|
|
26
|
-
└── README.md ←
|
|
26
|
+
└── README.md ← only if there's clear context
|
|
27
27
|
```
|
|
28
28
|
|
|
29
|
-
##
|
|
29
|
+
## Step 4 — Write up.sql
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
-
|
|
33
|
-
-
|
|
34
|
-
-
|
|
35
|
-
-
|
|
31
|
+
Apply the checklist:
|
|
32
|
+
- New NOT NULL column? → needs a DEFAULT or a prior backfill
|
|
33
|
+
- Dropping a column with data? → migrate the data first, then DROP
|
|
34
|
+
- New FK? → the referenced table must already exist
|
|
35
|
+
- Index on a large table? → suggest CONCURRENTLY
|
|
36
36
|
|
|
37
|
-
##
|
|
37
|
+
## Step 5 — Write down.sql
|
|
38
38
|
|
|
39
|
-
SQL
|
|
40
|
-
|
|
39
|
+
SQL that reverses up.sql. It must leave the DB exactly in its previous state.
|
|
40
|
+
Always write it, even if the user didn't ask for it.
|
|
41
41
|
|
|
42
|
-
##
|
|
42
|
+
## Step 6 — Create README.md (only if there's context)
|
|
43
43
|
|
|
44
|
-
|
|
44
|
+
If the migration was planned or the user explained the reason:
|
|
45
45
|
```markdown
|
|
46
|
-
#
|
|
46
|
+
# short_purpose
|
|
47
47
|
|
|
48
|
-
{1-3
|
|
48
|
+
{1-3 lines about why, not what.}
|
|
49
49
|
```
|
|
50
50
|
|
|
51
|
-
|
|
51
|
+
If the user passed the script with no context: don't create README.md.
|
|
52
52
|
|
|
53
|
-
##
|
|
53
|
+
## Step 7 — Update schema/
|
|
54
54
|
|
|
55
|
-
|
|
55
|
+
Identify which `create.sql` files in `schema/` are now outdated and update them to reflect the
|
|
56
|
+
new state of the affected tables.
|
|
56
57
|
|
|
57
|
-
##
|
|
58
|
+
## Step 8 — Show the command to apply it
|
|
58
59
|
|
|
59
60
|
```bash
|
|
60
61
|
npm run migrate
|
|
@@ -3,109 +3,116 @@ paths:
|
|
|
3
3
|
- "migrations/**"
|
|
4
4
|
---
|
|
5
5
|
|
|
6
|
-
#
|
|
6
|
+
# Rules for working in migrations/
|
|
7
7
|
|
|
8
|
-
##
|
|
8
|
+
## Fundamental principle
|
|
9
9
|
|
|
10
|
-
`migrations/`
|
|
11
|
-
|
|
10
|
+
`migrations/` is **append-only** and **immutable**. Only new folders get added, at the end.
|
|
11
|
+
Never edit the `up.sql` of an already-applied migration — if there's a mistake, create a new
|
|
12
|
+
migration that fixes it.
|
|
12
13
|
|
|
13
|
-
##
|
|
14
|
+
## Structure of each migration
|
|
14
15
|
|
|
15
|
-
|
|
16
|
+
Each migration is a **folder**, not a loose file:
|
|
16
17
|
|
|
17
18
|
```
|
|
18
19
|
migrations/
|
|
19
|
-
└──
|
|
20
|
-
├── up.sql ←
|
|
21
|
-
├── down.sql ←
|
|
22
|
-
└── README.md ←
|
|
20
|
+
└── NNN_YYYYMMDD_HHMM_short_purpose/
|
|
21
|
+
├── up.sql ← changes to apply (required)
|
|
22
|
+
├── down.sql ← how to revert them (required)
|
|
23
|
+
└── README.md ← context and reason (only if it was planned or has clear context)
|
|
23
24
|
```
|
|
24
25
|
|
|
25
|
-
##
|
|
26
|
+
## Folder naming
|
|
26
27
|
|
|
27
28
|
```
|
|
28
|
-
|
|
29
|
+
NNN_YYYYMMDD_HHMM_short_purpose
|
|
29
30
|
```
|
|
30
31
|
|
|
31
|
-
- `NNN` —
|
|
32
|
-
- `YYYYMMDD` —
|
|
33
|
-
- `HHMM` —
|
|
34
|
-
- `
|
|
32
|
+
- `NNN` — sequential number with leading zeros (`001`, `002`, `003`...)
|
|
33
|
+
- `YYYYMMDD` — creation date
|
|
34
|
+
- `HHMM` — creation time
|
|
35
|
+
- `short_purpose` — short description in `snake_case`
|
|
35
36
|
|
|
36
|
-
|
|
37
|
+
Examples:
|
|
37
38
|
- `003_20260524_1430_add_phone_to_users`
|
|
38
39
|
- `004_20260525_0900_create_index_orders_status`
|
|
39
40
|
- `005_20260526_1100_rename_column_amount_to_total`
|
|
40
41
|
- `006_20260527_1600_drop_deprecated_sessions`
|
|
41
42
|
|
|
42
|
-
## up.sql —
|
|
43
|
+
## up.sql — the changes
|
|
43
44
|
|
|
44
|
-
SQL
|
|
45
|
+
SQL that transforms the DB from the previous state to the new one. This is what `npm run migrate` runs.
|
|
45
46
|
|
|
46
|
-
## down.sql —
|
|
47
|
+
## down.sql — the reverse
|
|
47
48
|
|
|
48
|
-
SQL
|
|
49
|
-
|
|
49
|
+
SQL that reverses `up.sql`. It must leave the DB exactly as it was before this migration was applied.
|
|
50
|
+
Always write it even if it's not planned to be used — it documents the opposite effect.
|
|
50
51
|
|
|
51
|
-
## README.md —
|
|
52
|
+
## README.md — context (optional)
|
|
52
53
|
|
|
53
|
-
|
|
54
|
-
|
|
54
|
+
Only create it if the migration was **planned** or has **known context** (business decision, bug
|
|
55
|
+
fix, coordinated refactor). If the user passes a script with no context, generate only `up.sql`
|
|
56
|
+
and `down.sql`, without `README.md`.
|
|
55
57
|
|
|
56
|
-
|
|
58
|
+
README.md format:
|
|
57
59
|
```markdown
|
|
58
|
-
#
|
|
60
|
+
# short_purpose
|
|
59
61
|
|
|
60
|
-
{1-3
|
|
62
|
+
{1-3 lines explaining why this change is being made, not what the SQL does.}
|
|
61
63
|
```
|
|
62
64
|
|
|
63
|
-
## Checklist
|
|
65
|
+
## Checklist before writing up.sql
|
|
64
66
|
|
|
65
|
-
1.
|
|
66
|
-
2.
|
|
67
|
-
3.
|
|
68
|
-
4.
|
|
69
|
-
5.
|
|
70
|
-
6.
|
|
67
|
+
1. Does the change affect existing data? → include `UPDATE`/backfill before the `ALTER`
|
|
68
|
+
2. Adding a `NOT NULL` column? → needs a `DEFAULT` or a prior backfill
|
|
69
|
+
3. Dropping a column with data? → migrate the data first, then `DROP`
|
|
70
|
+
4. New FK? → the referenced table must already exist
|
|
71
|
+
5. Creating an index on a large table? → consider `CREATE INDEX CONCURRENTLY`
|
|
72
|
+
6. Is the SQL idempotent where possible? → use `IF NOT EXISTS`, `IF EXISTS`, `CREATE OR REPLACE`
|
|
71
73
|
|
|
72
|
-
##
|
|
74
|
+
## How npm run migrate applies changes
|
|
73
75
|
|
|
74
|
-
1.
|
|
75
|
-
2.
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
76
|
+
1. Connects to the DB by reading `.env`
|
|
77
|
+
2. Creates the `migrations` tracking table if it doesn't exist (named to match the convention
|
|
78
|
+
TypeORM/NestJS uses, no leading underscore)
|
|
79
|
+
3. Lists the **folders** in `migrations/` in alphabetical order
|
|
80
|
+
4. Runs the `up.sql` of the folders not yet registered in the `migrations` table
|
|
81
|
+
5. Registers each **folder name** with a timestamp once applied
|
|
82
|
+
6. The whole run is **one transaction** — if any migration in the batch fails, every migration
|
|
83
|
+
applied earlier in that same run is rolled back too, not just the failing one
|
|
80
84
|
|
|
81
|
-
##
|
|
85
|
+
## Also update schema/
|
|
82
86
|
|
|
83
|
-
|
|
87
|
+
When creating a migration, also update the corresponding `create.sql` in `schema/` so it reflects
|
|
88
|
+
the current state. `schema/` is the current design; `migrations/` is how we got there.
|
|
84
89
|
|
|
85
|
-
##
|
|
90
|
+
## How to revert migrations (npm run rollback)
|
|
86
91
|
|
|
87
|
-
`rollback.ts`
|
|
92
|
+
`rollback.ts` deletes each `migrations` table record and then runs its `down.sql`, in that order,
|
|
93
|
+
so that if a `down.sql` drops the `migrations` table itself, the transaction still closes cleanly.
|
|
88
94
|
|
|
89
95
|
```bash
|
|
90
|
-
#
|
|
96
|
+
# Revert the last applied migration
|
|
91
97
|
npm run rollback
|
|
92
98
|
|
|
93
|
-
#
|
|
99
|
+
# Revert the last N migrations (in reverse order)
|
|
94
100
|
npm run rollback 3
|
|
95
101
|
```
|
|
96
102
|
|
|
97
|
-
|
|
98
|
-
1.
|
|
99
|
-
2.
|
|
100
|
-
3.
|
|
101
|
-
|
|
103
|
+
How it works:
|
|
104
|
+
1. Checks that the `migrations` table exists — if not, there's nothing to revert
|
|
105
|
+
2. Queries `migrations ORDER BY migration DESC` to get the last N
|
|
106
|
+
3. Wraps the whole batch in **one transaction**: for each one, `DELETE FROM migrations` → runs
|
|
107
|
+
`down.sql`; if any step fails, everything reverted so far in that run rolls back too
|
|
108
|
+
4. Commits once, only after every migration in the batch reverted successfully
|
|
102
109
|
|
|
103
|
-
|
|
110
|
+
**`down.sql` must exist.** If it doesn't, the script stops without reverting anything.
|
|
104
111
|
|
|
105
|
-
##
|
|
112
|
+
## Common error
|
|
106
113
|
|
|
107
|
-
|
|
114
|
+
If `npm run migrate` says it's up to date but the changes don't show up:
|
|
108
115
|
```sql
|
|
109
|
-
DELETE FROM
|
|
116
|
+
DELETE FROM migrations WHERE migration = 'NNN_YYYYMMDD_HHMM_name';
|
|
110
117
|
```
|
|
111
|
-
|
|
118
|
+
Then run `npm run migrate` again. Or create a new migration with the missing change.
|