Skip to content

Commit 6ed58df

Browse files
committed
fix: batch YAML fixture application and improve generator robustness
1 parent 07e5345 commit 6ed58df

3 files changed

Lines changed: 66 additions & 20 deletions

File tree

regresql/generator.go

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,13 +116,56 @@ func (bg *BaseGenerator) Name() string {
116116
return bg.name
117117
}
118118

119-
// getParam retrieves a parameter value with type assertion
119+
// getParam retrieves a parameter value with type assertion and numeric conversion
120120
func getParam[T any](params map[string]any, key string, defaultValue T) T {
121-
if val, exists := params[key]; exists {
122-
if typed, ok := val.(T); ok {
121+
val, exists := params[key]
122+
if !exists {
123+
return defaultValue
124+
}
125+
126+
// Direct type assertion
127+
if typed, ok := val.(T); ok {
128+
return typed
129+
}
130+
131+
// Handle numeric type conversions (YAML parses numbers as int, but we often want int64)
132+
var result any
133+
switch any(defaultValue).(type) {
134+
case int64:
135+
switch v := val.(type) {
136+
case int:
137+
result = int64(v)
138+
case int64:
139+
result = v
140+
case float64:
141+
result = int64(v)
142+
}
143+
case int:
144+
switch v := val.(type) {
145+
case int:
146+
result = v
147+
case int64:
148+
result = int(v)
149+
case float64:
150+
result = int(v)
151+
}
152+
case float64:
153+
switch v := val.(type) {
154+
case int:
155+
result = float64(v)
156+
case int64:
157+
result = float64(v)
158+
case float64:
159+
result = v
160+
}
161+
}
162+
163+
if result != nil {
164+
if typed, ok := result.(T); ok {
123165
return typed
124166
}
125167
}
168+
126169
return defaultValue
127170
}
128171

regresql/generators_basic.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ type (
4545
// SequenceGenerator generates sequential integers
4646
SequenceGenerator struct {
4747
BaseGenerator
48-
counter int64
4948
}
5049

5150
// IntGenerator generates random integers
@@ -102,21 +101,15 @@ type (
102101
func NewSequenceGenerator() *SequenceGenerator {
103102
return &SequenceGenerator{
104103
BaseGenerator: BaseGenerator{name: "sequence"},
105-
counter: 0,
106104
}
107105
}
108106

109107
func (g *SequenceGenerator) Generate(params map[string]any, column *ColumnInfo) (any, error) {
110108
start := getParam(params, "start", int64(1))
109+
index := getParam(params, "_index", int64(0))
111110

112-
if g.counter == 0 {
113-
g.counter = start
114-
}
115-
116-
value := g.counter
117-
g.counter++
118-
119-
return value, nil
111+
// Stateless: value is simply start + index
112+
return start + index, nil
120113
}
121114

122115
func (g *SequenceGenerator) Validate(params map[string]any, column *ColumnInfo) error {

regresql/snapshot_build.go

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,9 @@ func applyFixtures(db *sql.DB, root string, fixtures []string, verbose bool) ([]
185185
}
186186

187187
var applied []string
188+
var yamlFixtures []string
188189

190+
// First pass: execute SQL fixtures immediately, collect YAML fixtures
189191
for _, f := range fixtures {
190192
if isSQLFixture(f) {
191193
if verbose {
@@ -197,27 +199,35 @@ func applyFixtures(db *sql.DB, root string, fixtures []string, verbose bool) ([]
197199
applied = append(applied, f)
198200
} else {
199201
name := trimYAMLExt(f)
200-
if verbose {
202+
yamlFixtures = append(yamlFixtures, name)
203+
}
204+
}
205+
206+
// Second pass: apply all YAML fixtures in a single transaction
207+
// This ensures dependencies are resolved correctly and not re-applied
208+
if len(yamlFixtures) > 0 {
209+
if verbose {
210+
for _, name := range yamlFixtures {
201211
fmt.Printf(" Applying fixture: %s\n", name)
202212
}
203-
if err := applyYAMLFixture(fm, name); err != nil {
204-
return nil, fmt.Errorf("fixture %q: %w", name, err)
205-
}
206-
applied = append(applied, name)
207213
}
214+
if err := applyYAMLFixtures(fm, yamlFixtures); err != nil {
215+
return nil, err
216+
}
217+
applied = append(applied, yamlFixtures...)
208218
}
209219

210220
return applied, nil
211221
}
212222

213-
func applyYAMLFixture(fm *FixtureManager, name string) error {
223+
func applyYAMLFixtures(fm *FixtureManager, names []string) error {
214224
if err := fm.BeginTransaction(); err != nil {
215225
return err
216226
}
217227

218228
_ = fm.IntrospectSchema()
219229

220-
if err := fm.ApplyFixtures([]string{name}); err != nil {
230+
if err := fm.ApplyFixtures(names); err != nil {
221231
fm.Rollback()
222232
return err
223233
}

0 commit comments

Comments
 (0)