General error: 5 database is locked on adding users
Created 7 years ago by emergingdzns

I'm running a console command to import users from a csv file. It will process the first user into the default_users_users table. But then it appears to get stuck. After a while it will throw this error:

[PDOException]                                        
  SQLSTATE[HY000]: General error: 5 database is locked

I'm using the UserSeeder as my example and everything I thought went together well as it imported the full list once, but then I realized it didn't add the passwords. So I wiped all the users except the admin user (directly in the database to make sure I started with clean records). I then added the $this->users->unguard() line to the function. Then when I re-started, the first user inserts fine, with the password, but then it gets stuck there for a long time before throwing the error above. I tried

Here's my code:

<?php namespace App\Console\Commands;

use Illuminate\Console\Command;
use Anomaly\UsersModule\Role\Contract\RoleRepositoryInterface;
use Anomaly\UsersModule\User\Contract\UserInterface;
use Anomaly\UsersModule\User\Contract\UserRepositoryInterface;
use Anomaly\UsersModule\User\UserActivator;
ini_set('auto_detect_line_endings',TRUE);

class ImportOwners extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'importusers';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Import Old users';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    protected $users;
    protected $activator;
    protected $roles;

    public function __construct(UserRepositoryInterface $users,
                                RoleRepositoryInterface $roles,
                                UserActivator $activator)
    {
        $this->users     = $users;
        $this->roles     = $roles;
        $this->activator = $activator;
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $this->users->unguard();
        $file = base_path().'/database/seeds/users.csv';
        $row = 0;
        $header = [];
        if (($handle = fopen($file, "r")) !== FALSE) {
            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                if ($row == 0) {
                    foreach($data as $idx => $key) {
                        $header[$key] = $idx;
                    }
                } else {
                    $email = $data[$header['email']];
                    if ($email != '') {
                        $existingUser = $this->users->findByEmail($email); // make sure user doesn't already exist.
                        if ($existingUser) {
                            $user = $existingUser; // just use the found user if it exists for that email
                        } else {
                            $pwd = bin2hex(openssl_random_pseudo_bytes(6)); // setting a random string passwor
                            $user = $this->users->create(
                                [
                                    'first_name' => $data[$header['first_name']],
                                    'last_name' => $data[$header['last_name']],
                                    'company_name' => str_replace('NULL','',$data[$header['company_name']]), // custom field added in control panel
                                    'email' => $data[$header['email']],
                                    'created_at' => $data[$header['created_at']],
                                    'password' => $pwd,
                                    'username' => str_replace(['@','.'],['_','_'],$email),
                                    'display_name' => ($data[$header['company_name']] != null ? $data[$header['company_name']]:$data[$header['first_name']]." ".$data[$header['last_name']]) // if company_name is provided in file, use it. Otherwise, use first and last
                                ]
                            );
                            print_r($user); // this never happens!
                        }
                        $this->activator->force($user);
                    } else {
                        echo "no email row".$row."\n"; // this never happens!
                    }
                }
                echo $row."\n"; // this never happens!
                $row++;
            }
            fclose($handle);
        }
    }
}

I tried removing the unguard line but it's still doing the same thing.

I'm at a stand-still on this. Any reason that the UserRepositoryInterface wouldn't return the $user object?

emergingdzns  —  7 years ago Best Answer

Ah ha! I changed the queue setting in scout.php config file to true. Then added QUEUE_DRIVER=database to the .env file and removed the SCOUT_DRIVER line. Now it's working.

fryiee  —  7 years ago

This is related to TNTSearch and Laravel Scout. Try setting SCOUT_DRIVER="null" to your .env and run your import again.

emergingdzns  —  7 years ago

Thanks @fryiee ! That helped a bit. I now am not having that particular issue, but now it's throwing another error I've never seen:

[ErrorException]                                                                                                                
  Missing argument 1 for Illuminate\Support\Manager::createDriver(), called in /path/to/my /site/vendor/laravel/framework/src/Illuminate/Support/Manager.php on line 87 and defined
emergingdzns  —  7 years ago

It seems this must be related to some listener or something somewhere that is attempting to do something when I use the users->create function. Could it be trying to automatically send an email?

emergingdzns  —  7 years ago

I just checked to make sure that it is set to automatically activate when the user is registered too so it shouldn't be trying to send anything out. Not sure why it's crapping out on the "driver".

fryiee  —  7 years ago

It may be related to evaluating null in the env. Check the Manager class.

emergingdzns  —  7 years ago

Yeah apparently setting to "null" is a problem for scout according to: https://www.neontsunami.com/posts/disabling-laravel-scout-in-test-environment

I've tried setting it to "null" and just null in the .env file and changed tntsearch in scout.php to 'null' but still no go. Same error.

Isn't there some way to simply stop it from doing anything with scout??

emergingdzns  —  7 years ago Best Answer

Ah ha! I changed the queue setting in scout.php config file to true. Then added QUEUE_DRIVER=database to the .env file and removed the SCOUT_DRIVER line. Now it's working.