Skip to content

Commit e8077fe

Browse files
committed
feat: Add --skip option for pagination through large tables
Add --skip option to skip a specified number of rows before exporting. Useful for paginating through large datasets in combination with --max. Also fixed default direction to 'ASC' when using --orderby without explicit --direction. Based on PR #143 by @whoisryosuke.
1 parent 0a27365 commit e8077fe

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,21 @@ php artisan iseed users --skip-fk-checks
215215

216216
**Note**: This option generates MySQL-specific `SET FOREIGN_KEY_CHECKS` statements.
217217

218+
### skip
219+
Optional parameter which defines the number of rows to skip before exporting. This is useful for paginating through large tables in combination with the `--max` option.
220+
221+
Example:
222+
```
223+
php artisan iseed users --skip=1000
224+
```
225+
226+
When combined with `--max` for pagination:
227+
```
228+
php artisan iseed users --max=1000 --orderby=id
229+
php artisan iseed users --max=1000 --skip=1000 --orderby=id
230+
php artisan iseed users --max=1000 --skip=2000 --orderby=id
231+
```
232+
218233
### where
219234
Optional parameter which allows you to specify a SQL WHERE clause to filter the rows that will be included in the seed file. The WHERE clause should be provided as a string and will be applied directly to the SQL query.
220235

src/Orangehill/Iseed/Iseed.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,11 @@ public function readStubFile($file)
6666
* @param string $postrunEvent
6767
* @param bool $register
6868
* @param bool $skipFkChecks
69+
* @param int $skip
6970
* @return bool
7071
* @throws Orangehill\Iseed\TableNotFoundException
7172
*/
72-
public function generateSeed($table, $prefix = null, $suffix = null, $database = null, $max = 0, $chunkSize = 0, $exclude = null, $prerunEvent = null, $postrunEvent = null, $dumpAuto = true, $indexed = true, $orderBy = null, $direction = 'ASC', $whereClause = null, $register = true, $skipFkChecks = false)
73+
public function generateSeed($table, $prefix = null, $suffix = null, $database = null, $max = 0, $chunkSize = 0, $exclude = null, $prerunEvent = null, $postrunEvent = null, $dumpAuto = true, $indexed = true, $orderBy = null, $direction = 'ASC', $whereClause = null, $register = true, $skipFkChecks = false, $skip = null)
7374
{
7475
if (!$database) {
7576
$database = config('database.default');
@@ -83,7 +84,7 @@ public function generateSeed($table, $prefix = null, $suffix = null, $database =
8384
}
8485

8586
// Get the data
86-
$data = $this->getData($table, $max, $exclude, $orderBy, $direction, $whereClause);
87+
$data = $this->getData($table, $max, $exclude, $orderBy, $direction, $whereClause, $skip);
8788

8889
// Repack the data
8990
$dataArray = $this->repackSeedData($data);
@@ -144,7 +145,7 @@ public function getSeedPath()
144145
* @param string $table
145146
* @return Array
146147
*/
147-
public function getData($table, $max, $exclude = null, $orderBy = null, $direction = 'ASC', $whereClause = null)
148+
public function getData($table, $max, $exclude = null, $orderBy = null, $direction = 'ASC', $whereClause = null, $skip = null)
148149
{
149150
$result = \DB::connection($this->databaseName)->table($table);
150151

@@ -161,6 +162,10 @@ public function getData($table, $max, $exclude = null, $orderBy = null, $directi
161162
$result = $result->orderBy($orderBy, $direction);
162163
}
163164

165+
if ($skip) {
166+
$result = $result->skip($skip);
167+
}
168+
164169
if ($max) {
165170
$result = $result->limit($max);
166171
}

src/Orangehill/Iseed/IseedCommand.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ public function fire()
7272
$indexed = !$this->option('noindex');
7373
$register = !$this->option('noregister');
7474
$skipFkChecks = $this->option('skip-fk-checks');
75+
$skip = intval($this->option('skip'));
7576
$orderBy = $this->option('orderby');
76-
$direction = $this->option('direction');
77+
$direction = $this->option('direction') ?: 'ASC';
7778
$prefix = $this->option('classnameprefix');
7879
$suffix = $this->option('classnamesuffix');
7980
$whereClause = $this->option('where');
@@ -84,6 +85,9 @@ public function fire()
8485
if ($chunkSize < 1) {
8586
$chunkSize = null;
8687
}
88+
if ($skip < 1) {
89+
$skip = null;
90+
}
8791

8892
$tableIncrement = 0;
8993
foreach ($tables as $table) {
@@ -114,7 +118,8 @@ public function fire()
114118
$direction,
115119
$whereClause,
116120
$register,
117-
$skipFkChecks
121+
$skipFkChecks,
122+
$skip
118123
),
119124
$table
120125
);
@@ -140,7 +145,8 @@ public function fire()
140145
$direction,
141146
$whereClause,
142147
$register,
143-
$skipFkChecks
148+
$skipFkChecks,
149+
$skip
144150
),
145151
$table
146152
);
@@ -186,6 +192,7 @@ protected function getOptions()
186192
array('classnamesuffix', null, InputOption::VALUE_OPTIONAL, 'suffix for class and file name', null),
187193
array('where', null, InputOption::VALUE_OPTIONAL, 'where clause to filter records', null),
188194
array('skip-fk-checks', null, InputOption::VALUE_NONE, 'disable foreign key checks during seeding', null),
195+
array('skip', null, InputOption::VALUE_OPTIONAL, 'number of rows to skip', null),
189196
);
190197
}
191198

0 commit comments

Comments
 (0)