-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed.go
More file actions
218 lines (190 loc) · 6.06 KB
/
seed.go
File metadata and controls
218 lines (190 loc) · 6.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
//go:build ignore
// Seeder para gvnotes. Puebla la base de datos con datos de prueba.
//
// Uso:
//
// go run seed.go [--reset]
//
// Flags:
//
// --reset Elimina todos los notebooks y notas existentes antes de insertar
package main
import (
"context"
"database/sql"
"embed"
"flag"
"fmt"
"log"
db "gvnotes/db/generated"
"gvnotes/internal/config"
"github.com/google/uuid"
)
//go:embed db/migrations
var migrations embed.FS
func main() {
reset := flag.Bool("reset", false, "Elimina notebooks y notas existentes antes de insertar")
flag.Parse()
sqlDB, err := config.OpenDB(migrations)
if err != nil {
log.Fatalf("abrir base de datos: %v", err)
}
defer sqlDB.Close()
if *reset {
if err := resetData(sqlDB); err != nil {
log.Fatalf("reset: %v", err)
}
fmt.Println("✓ Datos existentes eliminados")
}
q := db.New(sqlDB)
ctx := context.Background()
if err := seed(ctx, q); err != nil {
log.Fatalf("seed: %v", err)
}
fmt.Println("\n✓ Seed completado")
}
// resetData elimina todos los notebooks (cascade borra las notas) y notas raíz.
func resetData(sqlDB *sql.DB) error {
_, err := sqlDB.Exec(`DELETE FROM notes WHERE notebook_id IS NULL`)
if err != nil {
return err
}
_, err = sqlDB.Exec(`DELETE FROM notebooks WHERE parent_id IS NULL`)
return err
}
func seed(ctx context.Context, q db.Querier) error {
// -------------------------------------------------------------------------
// Nivel raíz: 20 notebooks + 15 notas sueltas = 35 items
// -------------------------------------------------------------------------
rootNotebooks, err := seedRootNotebooks(ctx, q, 20)
if err != nil {
return fmt.Errorf("notebooks raíz: %w", err)
}
if err := seedRootNotes(ctx, q, 15); err != nil {
return fmt.Errorf("notas raíz: %w", err)
}
// -------------------------------------------------------------------------
// Notebook "grande": 50 notas para probar desbordamiento de lista
// -------------------------------------------------------------------------
bigNB, err := createNotebook(ctx, q, nil, "📚 Notebook Grande (50 notas)", 100)
if err != nil {
return fmt.Errorf("notebook grande: %w", err)
}
if err := seedNotesInNotebook(ctx, q, bigNB.ID, 50, "Nota larga"); err != nil {
return fmt.Errorf("notas notebook grande: %w", err)
}
fmt.Printf(" ✓ Notebook grande: %q → 50 notas\n", bigNB.Title)
// -------------------------------------------------------------------------
// Notebooks con sub-notebooks y notas anidadas (reutiliza los 3 primeros)
// -------------------------------------------------------------------------
nested := []struct {
title string
subNBs int
notes int
}{
{"Trabajo", 5, 10},
{"Personal", 4, 8},
{"Proyectos", 6, 12},
}
for i, spec := range nested {
if i >= len(rootNotebooks) {
break
}
nb := rootNotebooks[i]
if _, err := q.UpdateNotebookTitle(ctx, db.UpdateNotebookTitleParams{
ID: nb.ID,
Title: spec.title,
}); err != nil {
return err
}
for j := 0; j < spec.subNBs; j++ {
sub, err := createNotebook(ctx, q, &nb.ID, fmt.Sprintf("%s / Sub %d", spec.title, j+1), int64(j))
if err != nil {
return err
}
if err := seedNotesInNotebook(ctx, q, sub.ID, 3, "Nota"); err != nil {
return err
}
}
if err := seedNotesInNotebook(ctx, q, nb.ID, spec.notes, "Nota"); err != nil {
return err
}
fmt.Printf(" ✓ %q: %d sub-notebooks · %d notas\n", spec.title, spec.subNBs, spec.notes)
}
total := len(rootNotebooks) + 1 // +1 notebook grande
fmt.Printf("\nResumen raíz:\n")
fmt.Printf(" Notebooks: %d\n", total)
fmt.Printf(" Notas sueltas: 15\n")
fmt.Printf(" Total items en raíz: %d\n", total+15)
return nil
}
func seedRootNotebooks(ctx context.Context, q db.Querier, n int) ([]db.Notebook, error) {
names := []string{
"Trabajo", "Personal", "Proyectos", "Investigación", "Archivado",
"En progreso", "Revisión", "Borradores", "Publicado", "Privado",
"Compartido", "Favoritos", "Temporal", "Reciente", "Antiguo",
"Crítico", "Opcional", "Experimental", "Estable", "Deprecado",
}
notebooks := make([]db.Notebook, 0, n)
for i := 0; i < n; i++ {
title := names[i%len(names)]
if i >= len(names) {
title = fmt.Sprintf("%s %d", title, i/len(names)+1)
}
nb, err := createNotebook(ctx, q, nil, title, int64(i))
if err != nil {
return nil, err
}
notebooks = append(notebooks, nb)
}
fmt.Printf(" ✓ %d notebooks en raíz\n", n)
return notebooks, nil
}
func seedRootNotes(ctx context.Context, q db.Querier, n int) error {
topics := []string{
"Reunión de equipo", "Ideas del proyecto", "Lista de tareas", "Recordatorio",
"Investigación inicial", "Feedback del cliente", "Plan de release", "Bug report",
"Propuesta técnica", "Resumen semanal", "Notas de standup", "Brainstorm",
"Decisiones del sprint", "Métricas del producto", "Retrospectiva",
}
for i := 0; i < n; i++ {
title := topics[i%len(topics)]
if i >= len(topics) {
title = fmt.Sprintf("%s %d", title, i/len(topics)+1)
}
if _, err := createNote(ctx, q, nil, title, int64(i)); err != nil {
return err
}
}
fmt.Printf(" ✓ %d notas sueltas en raíz\n", n)
return nil
}
func seedNotesInNotebook(ctx context.Context, q db.Querier, notebookID string, n int, prefix string) error {
for i := 0; i < n; i++ {
if _, err := createNote(ctx, q, ¬ebookID, fmt.Sprintf("%s %d", prefix, i+1), int64(i)); err != nil {
return err
}
}
return nil
}
func createNotebook(ctx context.Context, q db.Querier, parentID *string, title string, position int64) (db.Notebook, error) {
return q.CreateNotebook(ctx, db.CreateNotebookParams{
ID: uuid.New().String(),
ParentID: parentID,
Title: title,
Position: position,
})
}
func createNote(ctx context.Context, q db.Querier, notebookID *string, title string, position int64) (db.Note, error) {
content := fmt.Sprintf(
`{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","text":"Contenido de ejemplo para: %s"}]}]}`,
title,
)
return q.CreateNote(ctx, db.CreateNoteParams{
ID: uuid.New().String(),
NotebookID: notebookID,
Title: title,
Content: content,
Position: position,
})
}