Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I specify a single file to be seed only

I am using knex for seeding and I have a folder called development where I have all the seeds files.

What I would want is: How to seed single file.

The command I am using is: knex seed:run --env=development But this command is to seed all the files and I get duplicate rows on db.

Suppose I have created seed files yesterday, and I seed them, today I want to add another seed file, which I only want to seed this file, not the files from yesterday.

An example from Laravel is: php artisan db:seed --class=ProductTableSeeder

Thanks

like image 991
Lulzim Avatar asked Apr 28 '15 12:04

Lulzim


3 Answers

For those of you checking this in 2019+

According to the knex documentation

To run a specific seed file, execute:

$ knex seed:run --specific=seed-filename.js
like image 70
Madison Lai Avatar answered Sep 30 '22 13:09

Madison Lai


Don't work: knex seed:run --specific=seed-filename.js

  1. create in DB knex_seed_lock table
  2. Add this to seed file:

const file_name = path.basename(__filename)
  const seedIsExist = await knex('knex_seeds_lock').where({ file_name }).first()

  if (seedIsExist) {
    return
  }
  1. And add this to the end of the file:

await knex('knex_seeds_lock').insert({ file_name })

As a result, in the database you will get all the seed ​​that you already ran earlier

like image 23
Artem Bielykh Avatar answered Sep 30 '22 12:09

Artem Bielykh


Just move your scripts to another folder except the desired script, run the seed and copy the scripts back.

The seed API only has two commands, make and run. This is from the docs.

runknex.seed.run([config])

Runs all seed files for the current environment.

So all scripts will be executed on each run

like image 34
devconcept Avatar answered Sep 30 '22 12:09

devconcept