better_seeder 0.2.4 → 0.2.5

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7efa6650656240adf416711cabab9debe997812e5e666472e3391c59ad74ce44
4
- data.tar.gz: 504062b646d4800f75dbb20ef7e221fc0da730bf9aa207026795341e9e1c50e3
3
+ metadata.gz: 1d6d632652800d470d6214fa647d32b9fa48cab4c95ba88c7ff986a22093c33f
4
+ data.tar.gz: e9b16e216cd24c4f547f29f7f2fa50e690b5f31617145a84631f594186c60c6a
5
5
  SHA512:
6
- metadata.gz: bc24811f8a5ea605435701ee6ceda23927c1dad77327e054cd9b31756065037ef75a77f6b02bd1c2558f92870746de38366814bea90cd5597aba29a0b85b1e11
7
- data.tar.gz: 34c2cfc41ca466c7c4e7e4db290a47fc0ffd92503b2fc65c567c716f7123138e2de4f2ce485f4b1c5dc1e56f40f3b8b205b4d96d51deec3da5d35ffc6bc124c0
6
+ metadata.gz: 59358b3b0441b582abd6ec8601c97bc62e1e69b536b40e1f8137a3c1ba5b4495d66a862973da908d43ee5326c9aeebeab83cd594c665b42e10e42894380ee5a4
7
+ data.tar.gz: 3c99f4e4411e64ffb2626bd5016193792afd62122816e241f3f69f4825e8961c562850bd04eaf37e0fec90d03e26799c75a13ac24b4b4b45f168670ddc1b87e1
data/README.md CHANGED
@@ -1,7 +1,22 @@
1
+ ![BetterSeeder Logo](logo.png)
1
2
  # BetterSeeder
2
3
 
3
4
  BetterSeeder is a Rails gem designed to simplify and centralize your application's seeding process. It offers a flexible system to generate dynamic data, validate it using Dry-schema, enforce uniqueness constraints, load data into the database, and export it in various formats (SQL, CSV, JSON). The configuration is managed through a Rails initializer, while model-specific logic is defined in dedicated structure files.
4
5
 
6
+ ## Statistics
7
+
8
+ Below are two images displaying key statistics from the seeding process:
9
+
10
+ - **Initial Generation Statistics:**
11
+ This chart represents metrics from the very first generation run.
12
+
13
+ ![Initial Generation Statistics](generate.png)
14
+
15
+ - **Reload Data Statistics:**
16
+ This chart shows the metrics after reloading data (from the SQL file) into the database.
17
+
18
+ ![Reload Data Statistics](reload.png)
19
+
5
20
  ---
6
21
 
7
22
  ## Features
@@ -24,6 +39,12 @@ BetterSeeder is a Rails gem designed to simplify and centralize your application
24
39
  - **Rails Generator**
25
40
  A custom Rails generator scaffolds a structure file template for your models, ensuring a consistent configuration format.
26
41
 
42
+ - **Child Record Generation**
43
+ Now, BetterSeeder supports generating multiple child records per parent record. In your model’s seed configuration, you can define a `childs` section that specifies the number of child records to generate for each parent, along with attribute arrays to assign distinct values for each child. This allows for a total record count equal to *(parent count) × (childs count)*.
44
+
45
+ - **Preflight Data**
46
+ You can now define a `preflight` method in your structure file. The records returned by this method are inserted first before generating any additional records, ensuring that specific data is loaded as part of the seeding process. The total record count (defined in `seed_config[:count]`) includes these preflight records.
47
+
27
48
  ---
28
49
 
29
50
  ## Installation
@@ -65,7 +86,7 @@ If these values are set in the initializer, they will be used; otherwise, the ge
65
86
 
66
87
  To streamline the setup, execute the following command in your Rails console:
67
88
 
68
- ```ruby
89
+ ```bash
69
90
  BetterSeeder.install
70
91
  ```
71
92
 
@@ -85,12 +106,18 @@ For each model, create a structure file that centralizes the logic for generatin
85
106
 
86
107
  - **`seed_config`**
87
108
  Returns a hash with model-specific seeding settings:
88
- - `file_name`: The output file name (without extension)
89
- - `columns: { excluded: [...] }`: Columns to exclude from the generated data
90
- - `generate_data`: Boolean flag indicating whether to generate data dynamically (if false, existing records are used)
91
- - `count`: The number of records to generate (default: 10)
92
- - `load_data`: Boolean flag indicating whether the generated records should be inserted into the database
93
- - `parent`: For child models, specifies the parent model(s) used for injecting foreign keys
109
+ - `file_name`: The output file name (without extension)
110
+ - `columns: { excluded: [...] }`: Columns to exclude from the generated data
111
+ - `generate_data`: Boolean flag indicating whether to generate data dynamically (if false, existing records are used)
112
+ - `count`: The number of parent records to generate (default: 10)
113
+ - `load_data`: Boolean flag indicating whether the generated records should be inserted into the database
114
+ - `parent`: For child models, specifies the parent model(s) used for injecting foreign keys
115
+ - **`childs` (Optional):** A hash to define child record generation:
116
+ - `count`: The number of child records to generate for each parent.
117
+ - `attributes`: A hash mapping attribute names to an array of values. Each child record will receive a distinct value from the provided array.
118
+
119
+ - **`preflight` (Optional)**
120
+ If defined, this method should return an array of records to be inserted before generating dynamic records. These preloaded records count toward the total defined by `count`.
94
121
 
95
122
  - **`unique_keys` (Optional)**
96
123
  Returns an array of column groups (each group is an array of symbols) that must be unique. For example:
@@ -132,10 +159,20 @@ module MyNamespace
132
159
  load_data: true,
133
160
  parents: [
134
161
  { model: ::MyNamespace::MyModelParent, column: :column_id }
135
- ]
162
+ ],
163
+ childs: {
164
+ count: 3,
165
+ attributes: {
166
+ some_attribute: ['value1', 'value2', 'value3']
167
+ }
168
+ }
136
169
  }
137
170
  end
138
171
 
172
+ def self.preflight
173
+ [{ name: 'preloaded record', email: 'preloaded@example.com', created_at: Time.zone.now }]
174
+ end
175
+
139
176
  def self.unique_keys
140
177
  [[:email]]
141
178
  end
@@ -155,13 +192,16 @@ When you call `BetterSeeder.magic` with a configuration that contains an array o
155
192
  2. **Retrieve Seeding Configurations**
156
193
  Invoke the model's `seed_config` method to obtain its specific settings.
157
194
 
158
- 3. **Generate or Retrieve Records**
195
+ 3. **Preflight Data**
196
+ If a `preflight` method is defined in the structure file, its returned records will be inserted first. These records count toward the total number of records defined in `seed_config[:count]`.
197
+
198
+ 4. **Generate or Retrieve Records**
159
199
  Use the `structure` method to generate data dynamically (or fetch existing records) and validate them using `seed_schema` if defined. Uniqueness is enforced via `unique_keys`.
160
200
 
161
- 4. **Handle Parent/Child Relationships**
162
- Automatically inject foreign keys into child models using records from parent models.
201
+ 5. **Handle Parent/Child Relationships**
202
+ Automatically inject foreign keys into child models using records from parent models. With the new functionality, if a `childs` configuration is present, the gem generates child records for each parent record. For example, if `count` is set to 200 and `childs[:count]` is set to 2, a total of 400 records will be generated. Each child record receives distinct attribute values from the specified arrays.
163
203
 
164
- 5. **Load and Export Data**
204
+ 6. **Load and Export Data**
165
205
  If enabled (`load_data: true`), the generated records are inserted into the database and exported in the specified format (SQL, CSV, or JSON). Export files are saved in the directory specified by `BetterSeeder.configuration.preload_path`.
166
206
 
167
207
  ### Example Usage
@@ -257,6 +297,6 @@ BetterSeeder provides a modular, configurable, and extensible system for seeding
257
297
 
258
298
  - **Centralized Configuration:** Manage settings through a simple Rails initializer.
259
299
  - **Modular Structure Files:** Define data generation, validation, and configuration logic on a per-model basis.
260
- - **Seamless Data Handling:** Efficiently generate, validate, load, and export seed data while supporting complex relationships.
300
+ - **Seamless Data Handling:** Efficiently generate, validate, load, and export seed data while supporting complex relationships—now with enhanced child record generation for a more realistic data model.
261
301
 
262
302
  For further details or to contribute, please refer to the official repository or documentation.
@@ -16,7 +16,6 @@ module BetterSeeder
16
16
 
17
17
  load structure_file
18
18
 
19
- # Costruisce il nome della classe di structure: es. "Media::Participant" => "Media::ParticipantStructure"
20
19
  structure_class_name = "#{model_name}Structure"
21
20
  begin
22
21
  structure_class = Object.const_get(structure_class_name)
@@ -27,18 +26,26 @@ module BetterSeeder
27
26
  end
28
27
 
29
28
  seed_config = structure_class.respond_to?(:seed_config) ? structure_class.seed_config : {}
29
+ total_count = seed_config[:count] || 10
30
30
 
31
31
  generated_records = []
32
+
33
+ # Se il metodo preflight è definito, usalo per ottenere dati predefiniti
34
+ if structure_class.respond_to?(:preflight)
35
+ preflight_data = structure_class.preflight
36
+ generated_records.concat(preflight_data)
37
+ end
38
+
39
+ remaining_count = total_count - generated_records.size
40
+
32
41
  if seed_config.key?(:childs)
33
- # Logica per il modello child: il numero totale di record = count * childs_count
34
- parent_count = seed_config[:count] || 10
42
+ # Modalità child: per ogni "record padre", generare childs_count record
35
43
  childs_count = seed_config.dig(:childs, :count) || 10
36
-
37
- parent_count.times do |_i|
44
+ # Calcolo: i record generati saranno i preflight + (remaining_count * childs_count)
45
+ remaining_count.times do |_i|
38
46
  childs_count.times do |child_index|
39
47
  new_record = nil
40
48
  loop do
41
- # Passo l'indice del record figlio per far variare gli attributi definiti in childs[:attributes]
42
49
  new_record = build_record(model_name, structure_class, child_index, child_mode: true)
43
50
  new_record = inject_parent_keys(model_name, new_record, structure_class)
44
51
  break if validate_record(new_record, structure_class) &&
@@ -48,9 +55,8 @@ module BetterSeeder
48
55
  end
49
56
  end
50
57
  else
51
- # Logica standard per i modelli parent (o modelli senza childs)
52
- count = options[:count] || (seed_config[:count] || 10)
53
- count.times do |index|
58
+ # Modalità standard: genera remaining_count record
59
+ remaining_count.times do |index|
54
60
  new_record = nil
55
61
  loop do
56
62
  new_record = build_record(model_name, structure_class, index)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module BetterSeeder
4
- VERSION = '0.2.4'
4
+ VERSION = '0.2.5'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: better_seeder
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.2.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - alessio_bussolari
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2025-03-03 00:00:00.000000000 Z
11
+ date: 2025-03-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-schema