orangehill / iseed

Laravel Inverse Seed Generator
BSD 2-Clause "Simplified" License
2.78k stars 384 forks source link

Is there a way to seed all the tables? #58

Open zakkojo opened 8 years ago

zakkojo commented 8 years ago

I didn't find it... But if there isn't it would be good to have a --allTables parameter to seed all the tables in a DB and a --excludeTable to eventually exclude some of them.

RhRU commented 8 years ago

Have you tried with php artisan iseed:make ?

jazlopez commented 8 years ago

@RhRU iseed:make does not work, you still need to figure out a way to pass table list as argument

dayanstef commented 8 years ago

+1 for this functionality, it would be really nice to have issed --all tables instead of typing all of them one by one.

dayanstef commented 8 years ago

Even better, why not simplifying the command simple as php artisan iseed if no extra table parameter sent - then it should apply on all existing tables from db this way you can apply the rest of the options with -- for the all tables as well

zakkojo commented 8 years ago

Is there a workaround for this?

jazlopez commented 8 years ago

I just saw the support for all tables. Nice workaround

bertalanimre commented 8 years ago

So what is the command to iseed all the tables?

jazlopez commented 8 years ago

have you tried passing no parameter at all

$php artisan iseed

I saw a PR https://github.com/orangehill/iseed/issues/73 being merged to support all tables though documentation is not updated.

bertalanimre commented 8 years ago

Yes, it said not enough parameters.

bertalanimre commented 7 years ago

Still waiting for an update about this information. I know the function is implemented, I just have no clue about the command itself. Also please update the README file please.

tihomiro commented 7 years ago

Hey guys, I realize that this is a most wanted feature, but as you all know, Laravel supports MySQL, Postgres, SQLite and SQL Server, so a solution that employs DB::select('SHOW TABLES') is not a viable one. Unfortunately, there's no "List Database Tables" command in Laravel schema builder or Eloquent, so I'm gonna have to reject both https://github.com/orangehill/iseed/pull/73 and https://github.com/orangehill/iseed/pull/76 PRs.

On a different note, I would love to have a Laravel package that has a showTables and getNextAutoincrementId methods, so if anyone of you guys have time to hook up something like this and support MySQL, Postgres, SQLite and SQL Server - I'd be more than willing to use that package to implement seedAll functionality in iSeed.

eduardodgarciac commented 7 years ago

Hi, I know this issue is closed but you can use this: Schema::getConnection()->getDoctrineSchemaManager()->listTableNames() instead of: DB::select('SHOW TABLES') In this way you will have support for all

tihomiro commented 7 years ago

Hey @eduardodgarciac - I wasn't aware of this functionality! Thanks for the tip, I'm reopening the issue and will implement this feature early January.

eduardodgarciac commented 7 years ago

Hi @tihomiro, I hope you can implement it. Also, I have another suggestion. I see some people (including me) want to use truncate instead of delete. So, you can add a key in the config like method or any name you want.

return array( 'path' => '/database/seeds', 'chunk_size' => 500, 'method' => 'delete' );

and in the stub file send a method var

DB::table('{{table}}')->{{method}}();

For the people with foreign keys problems, it can be solve with this:

Schema::disableForeignKeyConstraints(); $this->call(SomeTableSeeder::class); Schema::enableForeignKeyConstraints();

tihomiro commented 7 years ago

OK, I opened a new separate issue for this suggestion https://github.com/orangehill/iseed/issues/82 Thanks again!

wuelcas commented 7 years ago

Hi, no news for this feature?

weezqyd commented 7 years ago

try out this package if you are using Mysql or MariaDB works fine for creating migrations and seeds fro database tables

SchuBu commented 6 years ago

Hey guys, for MySQL i've a workaround for iseeding all the tables. I just created an own "helper command" called iseed:all:

namespace App\Console\Commands;
use Illuminate\Console\Command;

class iseedAll extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'iseed:all';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command plugin for iseed to seed all databases except migrations table';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $query =  \DB::select('SHOW TABLES WHERE Tables_in_laravel NOT LIKE "migrations"');
        $collection = new \Illuminate\Support\Collection($query);
        $tables = $collection->implode('Tables_in_laravel',',');
        $this->info('Calling iseed for all tables except migrations ...');
        $this->call('iseed', ["tables" => $tables]);
    }
}

the command automatically omits the "migrations" table.

so if you need to run iseed for all your tables you can now run "iseed:all". I just tested it for MySQL databases only ...

Have fun!

arvins-wittymanager commented 5 years ago

Thanks @SchuBu, I just changed Tables_in_laravel to Tablesin(DATABASE NAME) and it worked. Very useful.

luqmanrom commented 5 years ago

Modifying a bit from @SchuBu solution

<?php

namespace App\Console\Commands;
use Illuminate\Console\Command;

class iseedAll extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'iseed:all';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command plugin for iseed to seed all databases except migrations table';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $dbName = env('DB_DATABASE');

        $query =  \DB::select("SHOW TABLES WHERE 'Tables_in_$dbName' NOT LIKE 'migrations'");
        $collection = new \Illuminate\Support\Collection($query);
        $tables = $collection->implode("Tables_in_$dbName",',');

        $this->info('Calling iseed for all tables except migrations ...');
        $this->call('iseed', ["tables" => $tables]);
    }
}
SchuBu commented 5 years ago

Great idea, thanks for your contribution!

MACLima commented 5 years ago

May I?

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class iseedAllCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'iseed:all {--force}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command plugin for iseed to seed all databases except migrations table';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $dbName = env('DB_DATABASE');

        $query =  \DB::select("SHOW TABLES WHERE Tables_in_$dbName <> 'migrations'");
        $collection = new \Illuminate\Support\Collection($query);
        $tables = $collection->implode("Tables_in_$dbName",',');

        $this->info('Calling iseed for all tables except migrations ...');
        $this->call('iseed', [
            'tables' => $tables,
            '--force' => $this->option('force'),
        ]);
    }
}
gu1cortez commented 5 years ago

Hey guys!

Has anyone found a solution to run automatic --force YES on all tables at run time? Using the solution suggested here above?

fri3ndly commented 5 years ago

Thanks @MACLima - Used your version and modified to include an exclusion array

class iSeedAllCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'iseed:all {--force}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command plugin for iseed to seed all databases except migrations table';

    protected $exclusions = [
        'migrations',
        'audits',
        'jobs',
        'failed_jobs',
        'enquiries',
        'password_resets',
    ];

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $dbName = env('DB_DATABASE');

        $query = \DB::select("SHOW TABLES WHERE Tables_in_$dbName <> 'migrations'");
        $collection = new \Illuminate\Support\Collection($query);
        $tables = $collection->implode("Tables_in_$dbName", ',');

        $tables_array = explode(',', $tables);
        $allowed_tables = array_merge(array_diff($tables_array, $this->exclusions));

        $this->info('Calling iseed for all tables except: ' . implode(', ', $this->exclusions));

        $this->call('iseed', [
            'tables' => implode(',', $allowed_tables),
            '--force' => $this->option('force'),
        ]);
    }
}
isheraz commented 5 years ago

I had to change some things for the previous solutions to work. extended from @schBu's solution

<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class iseedAll extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'iseed:all';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command plugin for iseed to seed all databases except migrations table';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $dbName = env('DB_DATABASE');

        $query =  \DB::select("SHOW TABLES WHERE 'Tables_in_$dbName' NOT LIKE 'migrations'");
        $collection = new \Illuminate\Support\Collection($query);

        $tables = "";
        $collectionArray = $collection->toArray();
        $iteration = 0;
        foreach($collectionArray as $item){
            if( $iteration < (sizeof($collectionArray) - 2)  ){
                $tables .= array_values( (array) $item)[0].",";
            }else{
                $tables .= array_values( (array) $item)[0];
            }
            $iteration++;
        }

        $this->info('Calling iseed for all tables except migrations ...');
        $this->call('iseed', ["tables" => $tables]);
    }
}
Braunson commented 4 years ago

Note: if you have a prefix set on your connection, iseed will not work and throw an error as hasTable adds in the prefix.

So if your table is foobar_articles and you are listing the tables foobar_articles, foobar_posts will be returned, then w hen you run that through iseed, the hasTables method check in IseedCommand:209 will add the config set prefix.. so if your prefix is set to foobar it'll search for the table foobar_foobar_articles thus resulting in a thrown error that Tables does not exist.

Just a caveat for anyone having issues. Something like this modified script (from a few authors above in this thread) should work to remove the prefix from the table before it get's run via iseed!

<?php

namespace App\Console\Commands;
use Illuminate\Console\Command;

class iseedAll extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'iseed:all';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command plugin for iseed to seed all databases except migrations table';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $dbName = env('DB_DATABASE');

        $query =  \DB::select("SHOW TABLES WHERE 'Tables_in_$dbName' NOT LIKE 'migrations'");
        $collection = new \Illuminate\Support\Collection($query);

        $dbPrefix = \DB::getTablePrefix();
        $hasDBPrefix = (bool) ($dbPrefix != '');

        $tables = "";
        $collectionArray = $collection->toArray();
        $iteration = 0;
        foreach ($collectionArray as $item) {
            if ($iteration < (sizeof($collectionArray) - 2)) {
                $tablesName = array_values((array) $item)[0].",";
            } else {
                $tablesName .= array_values((array) $item)[0];
            }
            $tables .= str_replace($dbPrefix, '', $tablesName);

            $iteration++;
        }

        $this->info('Calling iseed for all tables except migrations ...');
        $this->call('iseed', ["tables" => $tables]);
    }
}
parmonov98 commented 3 years ago

Hey guys, for MySQL i've a workaround for iseeding all the tables. I just created an own "helper command" called iseed:all:

namespace App\Console\Commands;
use Illuminate\Console\Command;

class iseedAll extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'iseed:all';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command plugin for iseed to seed all databases except migrations table';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $query =  \DB::select('SHOW TABLES WHERE Tables_in_laravel NOT LIKE "migrations"');
        $collection = new \Illuminate\Support\Collection($query);
        $tables = $collection->implode('Tables_in_laravel',',');
        $this->info('Calling iseed for all tables except migrations ...');
        $this->call('iseed', ["tables" => $tables]);
    }
}

the command automatically omits the "migrations" table.

so if you need to run iseed for all your tables you can now run "iseed:all". I just tested it for MySQL databases only ...

Have fun!

        $collection = new \Illuminate\Support\Collection($query);
        $tables = $collection->implode('Tables_in_' . env('DB_DATABASE', 'laravel'),',');
        $this->info('Calling iseed for all tables except migrations ...');

        $this->call('iseed', [
            "tables" => $tables,
            '--force' => true
        ]);

for better experience

hkpanchani commented 3 years ago

add chunksize parameter while running iseed:all

mb-mysterdev commented 2 years ago

Note: if you have a prefix set on your connection, iseed will not work and throw an error as hasTable adds in the prefix.

So if your table is foobar_articles and you are listing the tables foobar_articles, foobar_posts will be returned, then w hen you run that through iseed, the hasTables method check in IseedCommand:209 will add the config set prefix.. so if your prefix is set to foobar it'll search for the table foobar_foobar_articles thus resulting in a thrown error that Tables does not exist.

Just a caveat for anyone having issues. Something like this modified script (from a few authors above in this thread) should work to remove the prefix from the table before it get's run via iseed!

<?php

namespace App\Console\Commands;
use Illuminate\Console\Command;

class iseedAll extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'iseed:all';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command plugin for iseed to seed all databases except migrations table';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $dbName = env('DB_DATABASE');

        $query =  \DB::select("SHOW TABLES WHERE 'Tables_in_$dbName' NOT LIKE 'migrations'");
        $collection = new \Illuminate\Support\Collection($query);

        $dbPrefix = \DB::getTablePrefix();
        $hasDBPrefix = (bool) ($dbPrefix != '');

        $tables = "";
        $collectionArray = $collection->toArray();
        $iteration = 0;
        foreach ($collectionArray as $item) {
            if ($iteration < (sizeof($collectionArray) - 2)) {
                $tablesName = array_values((array) $item)[0].",";
            } else {
                $tablesName .= array_values((array) $item)[0];
            }
            $tables .= str_replace($dbPrefix, '', $tablesName);

            $iteration++;
        }

        $this->info('Calling iseed for all tables except migrations ...');
        $this->call('iseed', ["tables" => $tables]);
    }
}

I don't know if you had the case too but for me the last tables were stuck to the penultimate as an iteration was missing so for people who had this problem the solution is to put $iteration = -1 instead of $iteration = 0


    public function handle()
    {
        $dbName = env('DB_DATABASE');

        $query =  \DB::select("SHOW TABLES WHERE 'Tables_in_$dbName' NOT LIKE 'migrations'");
        $collection = new \Illuminate\Support\Collection($query);

        $dbPrefix = \DB::getTablePrefix();
        $hasDBPrefix = (bool) ($dbPrefix != '');

        $tables = "";
        $collectionArray = $collection->toArray();
        $iteration = -1;
        foreach ($collectionArray as $item) {
            if ($iteration < (sizeof($collectionArray) - 2)) {
                $tablesName = array_values((array) $item)[0].",";
            } else {
                $tablesName .= array_values((array) $item)[0];
            }
            $tables .= str_replace($dbPrefix, '', $tablesName);

            $iteration++;
        }

        $this->info('Calling iseed for all tables except migrations ...');
        $this->call('iseed', ["tables" => $tables]);
    }
MohamedGamil commented 6 months ago

Here is an updated version of iseed:all command which includes fixes and ability to decide which tables to ignore using a class constant and fixes for tables names concatenation.

<?php

namespace App\Console\Commands;
use Illuminate\Console\Command;

class IseedAllCommand extends Command
{
    const IGNORED_TABLES = [
        "migrations",
    ];

    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'iseed:all';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Run iseed to generate all database tables except migrations';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $query = \DB::select("SHOW TABLES");
        $collectionArray = collect($query)->toArray();
        $dbPrefix = \DB::getTablePrefix();
        $tables = "";
        $iteration = 0;

        foreach ($collectionArray as $item) {
            $tablesName = array_values((array) $item)[0];

            if ($iteration <= (count($collectionArray) - 2)) {
                $tablesName .= ",";
            }

            $tables .= str_replace($dbPrefix, '', $tablesName);
            $iteration++;
        }

        $tables = array_filter(
            explode(",", $tables),
            fn($item) => false === in_array($item, static::IGNORED_TABLES, true) && strlen(trim($item)) > 0
        );

        $tables = implode(",", $tables);

        $this->info('Calling iseed for all tables except migrations ...');
        $this->call('iseed', [
            "tables" => $tables,
            "--classnameprefix" => "DataSeeder",
            "--chunksize" => "1000",
            "--noindex" => true,
            "--force" => true,
        ]);
    }
}
polishchukI commented 3 months ago

I have connections in my application, i've tested alltepes of commands - it does not works, how can i generate seed for ALL tables of SELECTED base?

Thx!