miguelopezv
miguelopezv

Reputation: 860

Laravel + SQLite = SQLSTATE[HY000]General Error: 8 attempt to write a readonly database

I'm trying to delete a db.sqlite file, create it again and then insert some info into this DB all in the same method. This is the method I'm using:

public function destroy()
{
    // Store all contents and delete the first one since this is created via seeder
    $contents = $this->contents->all()->toArray();
    array_shift($contents);

    // Delete db file, creates it from an example file and changes permissions
    system('rm -rf ../database/database.sqlite');
    system('cp ../database/database.sqlite.example ../database/database.sqlite');
    system('chmod 0777 ../database/');
    system('chmod 0777 ../database/database.sqlite');

    // Insert data
    foreach ($contents as $content) {
        $this->contents->create($content);
    }

    return response()->json(['message' => 'Data has been destroyed!']);
}

However when I try to run it i get the error described in the title:

SQLSTATE[HY000]General Error: 8 attempt to write a readonly database

I'm setting 777 permissions on the file and the folder as recommended by others questions' answers, so I have no idea why it isn't working.

Upvotes: 24

Views: 30629

Answers (6)

SlowSuperman
SlowSuperman

Reputation: 628

I had the same error. The task was to restore the DB during each test, but not to use migrations and seeders.

I solved it like that:

  • Ensure proper file permissions: 755 for parent folder and 644 for each .sqlite
  • DB::purge() + DB::reconnect() after restoring databases.
  • Physically delete the database file after each test (on tearDown). Despite I remove them during restore step, only physically removing it at tearDown() helped.

Upvotes: 0

gdm
gdm

Reputation: 7968

If you are on Fedora or Redhat with Selinux rememebr to switch off Selinux (or configure it correctly):

setenforce 0

Upvotes: 2

Jignesh Joisar
Jignesh Joisar

Reputation: 15165

give permission here in ubuntu working on 18.04 and laravel 6

sudo chmod -R 775 database
sudo chown -R $(whoami) database

Upvotes: 9

Zymawy
Zymawy

Reputation: 1626

You need to run two commands

First, change ownership of the Laravel directory to web group:

sudo chown -R :www-data /var/www/yourLarvelFolder

Second, give privileges over storage directory so it can be writable:

sudo chmod -R 775 /var/www/yourLarvelFolder/storage

Upvotes: 41

Hamfri
Hamfri

Reputation: 2219

First run the following command to check apache2's user. Usually it is www-data

ps aux | grep "apache2"

Then run the following commands to resolve your issue.

chgrp -R www-data your_application_directory
chmod -R g+rw your_application_directory

Upvotes: 1

miguelopezv
miguelopezv

Reputation: 860

Finally I solved it by using to different methods, one to delete the DB and create again (returning all the contents) and then, the second one to insert all the contents to the new DB.

Upvotes: 2

Related Questions