phinx icon indicating copy to clipboard operation
phinx copied to clipboard

Suggestion to add a seed()

Open CMCDragonkai opened this issue 12 years ago • 53 comments

A suggestion to add a seed() function that is defined inside any migration classes. If it exists, Phinx will run the seed() only after it runs up().

I often need this to seed tables with initial data, which goes along with migration.

CMCDragonkai avatar Jan 04 '14 15:01 CMCDragonkai

:+1: I'm currently using some create_<table>_defaults migration for each table that needs this. Bonus points if phinx can migrate/rollback seeded rows!!

eridal avatar Jan 20 '14 17:01 eridal

:+1:

andersonamuller avatar Jan 30 '14 08:01 andersonamuller

I agree. Adding a functionality to create seed values will be great.

kevinisaac avatar Jun 05 '14 15:06 kevinisaac

Separate seeds like laravel / rails would be great. What do you think @robmorgan ?

todiadiyatmo avatar Jun 18 '14 04:06 todiadiyatmo

Sure, do you want to try contribute something?

Cheers,

Rob

On 18 Jun 2014, at 6:05 am, Todi Adiyatmo Wijoyo [email protected] wrote:

Separate seeds like laravel / rails would be great. What do you think @robmorgan ?

— Reply to this email directly or view it on GitHub.

robmorgan avatar Jun 18 '14 06:06 robmorgan

All database connections in phinx is using AdapterInterface : https://github.com/robmorgan/phinx/blob/0.3.x-dev/src/Phinx/Db/Adapter/AdapterInterface.php

The adapter interface enable the class to do anything with the table, it can addColumn , addIndex , etc. If we want implement a seeder, do you think it is better to divide the interface into 2 class ? Lets say BasicAdapterInterface and AdapterInterface so the adapter in the seeder has no access to table structure.

todiadiyatmo avatar Jun 18 '14 08:06 todiadiyatmo

If its table specific then it might be better to put this functionality in the Table class. Tell me first how you would envisage to call it then we can find the best place for it?

e.g:

public function up()
{
    $table = $this->table('something');
    $table->seed(array('col1' = > 'somedata'));
}

robmorgan avatar Jun 18 '14 09:06 robmorgan

What i mean is actualy i want to build new seed command like this


php phinx seed [ClassName] #default to DatabaseSeeder.php

So the seed will be separated from migration command. Each seed can call a seeder class or just run one class , for example :

class DatabaseSeeder extends Seeder
{

    function run()
    {
        $user = $this->table('user');

        $user->truncate();

        $user->insert($data);

        $this->calls('OtherSeeder');


    }

}

Just like in http://laravel.com/docs/migrations#database-seeding

todiadiyatmo avatar Jun 18 '14 13:06 todiadiyatmo

Ok this a bit more complex then, because you would need more commands to add, run and manage the seed functions.

robmorgan avatar Jun 18 '14 14:06 robmorgan

Actually i've made the symfony console command to archive this in my project, if you interested to integrate this into phinx i can try to create the pull request.

todiadiyatmo avatar Jun 19 '14 00:06 todiadiyatmo

Guys I've been thinking about this feature. For me its either a new method called seed() in each migration, Phinx can try to execute when migrating 'up'. It wouldn't handle removing data. There should be a flag to run to run it as well. We don't want this to happen in production.

The other method is more akin to the Rails way. I will introduce a new file(s) and commands. e.g: phinx seed. Which you can optionally run manually after migrating up first.

robmorgan avatar Jun 29 '14 14:06 robmorgan

I'am voting for the separate seed comands, because more framework using it. Maybe it will be accepted better in community.

todiadiyatmo avatar Jul 01 '14 06:07 todiadiyatmo

:+1: for the seed command

ecolinet avatar Jul 07 '14 16:07 ecolinet

:+1: for seed command

mcapodacqua avatar Jul 16 '14 23:07 mcapodacqua

:+1: for Seed command.

joshmanders avatar Aug 13 '14 23:08 joshmanders

:+1: for seed command

ferodss avatar Aug 14 '14 14:08 ferodss

:+1: for seed command

jaredk2g avatar Aug 25 '14 19:08 jaredk2g

:+1: for seed command

gyKa avatar Sep 19 '14 19:09 gyKa

:+1: the Rails way.

boedy avatar Oct 02 '14 19:10 boedy

:+1: the Rails way.

ghost avatar Oct 09 '14 20:10 ghost