I'm getting this error:
Database (homestead) does not exist.
When I try this simple Dusk test:
class ShowArticleTest extends DuskTestCase
{
use DatabaseMigrations;
/** #test */
public function it_shows_all_articles()
{
Article::create([
'title' => 'My First Article',
'body' => 'Some body text.'
]);
$this->browse(function (Browser $browser) {
$browser->visit('/articles')
->assertSee('My First Article');
});
}
}
I can see in the stack trace that the error comes from the controller method that handles the /articles request, which looks like this:
public function all()
{
Article::all();
}
So it seems that the database is accessible by the test itself but not by the controller.
The .env.dusk.local file looks like this:
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
The homestead file is being created in my project root folder. The file is found inside the virtual machine too and the permissions looks like this:
-rw-rw-r-- 1 vagrant vagrant
Tried setting DATABASE to database/testing.sqlite in .env.dusk.local. The file is created once Dusk starts but the error still says that it can't find the database/testing.sqlite database.
Database (database/testing.sqlite) does not exist.
The database file can be accessed using the sqlite3 CLI and I can query for records without problem.
I'm using Laravel 5.5 and Homestead.
The problem occurs inside config/database.php, around here:
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
]
Because DB_DATABASE inside .env.dusk.local is defined as homestead, the configuration value for database.connections.sqlite.database ends up just the same: homestead.
The actual value should be the full path to the file, which can be obtained with the database_path() helper. So, by simple moving database_path() out of the env() call:
'database' => database_path(env('DB_DATABASE', 'database.sqlite'))
The value for database.connections.sqlite.database becomes the full path to the database file and everything seems to be working.
Related
VERSIONS: Laravel v5.8.21 & Laravel Dusk v5.1.0
I'm having real issues with getting Dusk to work with a testing database. I seem to have gone through every piece of advice and still no luck. Dusk is not loading my .env.dusk.local file for some reason. Can anyone help?
// test
class ApplicationTest extends DuskTestCase
{
use DatabaseMigrations;
/** #test */
public function it_works()
{
$this->browse(function (Browser $browser) {
$browser->visit('/test')->assertSee('It works!');
});
}
}
// .env.dusk.local
APP_NAME="Laravel"
APP_ENV=local
APP_URL=http://127.0.0.1:8000
DB_CONNECTION=dusk
// database.php
'dusk' => [
'driver' => 'sqlite',
'database' => database_path('dusk.sqlite'),
'prefix' => '',
]
Then I run the PHP web server with php artisan serve --env=dusk.local however Dusk uses the .env file and goes to my development site using the development database.
What am I doing wrong?
I think you might need to update Laravel. Are you using an older version of the framework? This used to be a bug pre-5.8.7. See: https://github.com/laravel/framework/issues/27828
I have this situation in laravel 5.2 where if I do this:
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=
DB_DATABASE=database/database.sqlite
DB_USERNAME=
DB_PASSWORD=
in the .env file I get this:
InvalidArgumentException in SQLiteConnector.php line 34:
Database (database/database.sqlite) does not exist.
on trying to manipulate the laravel error rendering system with this code from a controller:
<?php
namespace App\Http\Controllers;
use App\User;
class SampleController extends Controller
{
public function findUser()
{
$user = User::firstOrFail();
return $user->toArray();
}
}
And this goes well with php artisan migrate command, but to get the controller code to render the error as expected I have to do this:
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=
DB_DATABASE=../database/database.sqlite
DB_USERNAME=
DB_PASSWORD=
To fix the problem so I get both php artisan migrate and SampleController to work I have put this in the config/database.php file:
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => database_path('database.sqlite'),
'prefix' => '',
],
Why is the default means to access sqlite as given on the laravel website not working. Is it a bug I should be aware of?
The normal configuration in the config.php file is expected to be
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', database_path('database.sqlite')),
'prefix' => '',
],
Furthermore, the appropriate way to mention the DB_DATABASE in the .env file is to specify the absolute file name, such as /var/www/laravel/database/database.sqlite not the relative location.
For example, in Windows
DB_DATABASE=C:\wamp\www\laravel\database\database.sqlite
And this is what is mentioned in the Laravel Documentation
DB_DATABASE=/absolute/path/to/database.sqlite
I migrated my local laravel 5 project onto my AWS ec2 lamp Ubuntu server.
http://realtoraxe.com/realtoraxe/public/ but it shows
InvalidArgumentException in SQLiteConnector.php line 34: Database does not exist.
I changed the database.php set for sqlite
<?php
return array(
'default' => 'sqlite',
'connections' => array(
'sqlite' => array(
'driver' => 'sqlite',
'database' => 'http://realtoraxe.com/realtoraxe/storage/database.sqlite',
'prefix' => '',
),
),
);
?>
and I changed the .env to
APP_ENV=local
APP_DEBUG=true
APP_KEY=mystring
DB_CONNECTION=sqlite
CACHE_DRIVER=file
SESSION_DRIVER=file
it still doesn't see the database
Laravel probably does not support much of the helper functions( e.g. storage_path) inside config files, but it surely does supports env function. More info here
That's the reason it's unable to locate your database. You should define the database in .env file or use the full path in the config file instead.
I would like to add sqlite db to my lumen app but I have some troubles.
I create migration:
php artisan make:migration create_users_table --create=users
then I changed my .env file, so it looks like:
DB_CONNECTION=sqlite
DB_HOST=localhost
DB_DATABASE=database.sqlite
then I was created database.sqlite and put it in storage folder and when I 'm trying to do:
php artisan migrate
I have
[InvalidArgumentException]
Database (database.sqlite) does not exist.
I uncommented this lines in bootstrap/app.php:
Dotenv::load(__DIR__.'/../');
$app->withFacades();
I can't find what is wrong.
I work on ubuntu 14.04
In my .env file I changed to:
DB_CONNECTION=sqlite
# DB_HOST=localhost
# DB_DATABASE=database.sqlite
I left only
DB_CONNECTION=sqlite
So Lumen use default config from /vendor/laravel/lumen-framework/config/database.php. It works.
According to lumen-framwork/config/database.php
'sqlite' => [
'driver' => 'sqlite',
'database' => env('DB_DATABASE', storage_path('database.sqlite')),
'prefix' => env('DB_PREFIX', ''),
],
sqlite is the default setting for lumen.
And you can set DB_CONNECTION in .env as follows:
DB_CONNECTION=sqlite
Then lumen will use storage_path('database.sqlite') as storage/database.sqlite
Otherwise, if you would like to assign DB_DATABASE directly, you should give the full path:
DB_DATABASE=/Users/../../storage/database.sqlite
Step 1 : Open .env file & replace respective database configuration with following piece of code.
DB_CONNECTION = sqlite
#DB_HOST = 127.0.0.1
#DB_PORT = 3306
#DB_DATABASE = homestead
#DB_USERNAME = homestead
#DB_PASSWORD = secret
Note : lines those are started with # are basically commented code.
Step 2 : Create a new file, name it database.sqlite in database folder. It will store out database structure.
Step 3 : There is need to include this file as ignored in our versioning system as there will be so many changes are going to be made with database i.e. insert, delete, update etc. To ignore database.sqlite, Open .gitignore file, and add this line at the end database/database.sqlite. (i.e. path of database.sqlite file).
I'm try use a sqlite database in my laravel project, in local environment for dev (Windows 8.1 with AMMPS), but when I try run a migrate:instal command, this error apeear:
[PDOException]
SQLSTATE[HY000] [14] unable to open database file
My database config file (app/config/local/database.php):
<?php
return array(
'default' => 'sqlite',
'connections' => array(
'sqlite' => array(
'driver' => 'sqlite',
'database' => __DIR__ . '\..\..\database\production.sqlite',
'prefix' => '',
),
),
);
try to remove the DB_DATABASE="yourdatabase" variable in your .env file
If the permissions allow to write on the folder just create the storage folders.
e-g, this how I fix the problem.
in config/database.php :
'connections' => [
'sqlite' => [
'driver' => 'sqlite',
'database' => storage_path('database/databaseName.sqlite'),
'prefix' => '',
],
Then if you run on your terminal php artisan migrate, it returns you the [PDOException] SQLSTATE[HY000] [14] unable to open database file
Create the path folder by your own, e-g in your terminal mkdir storage/database/databaseName.sqlite
Make sure the permissions allow you to write, then re-run the command php artisan migrate, it returns you the success message : Migration table created successfully.
On laravel 5.3.x
This was the only thing that worked for me:
Change DB_CONNECTION from "mysql" to "sqlite". Because this is an
sqlite database, you can delete all the other DB_* items in that .env
file.
APP_ENV=local APP_KEY=base64: APP_DEBUG=true APP_LOG_LEVEL=debug
APP_URL=http://localhost
DB_CONNECTION=sqlite
You just need to make sqlite file available at the defined path. In larvel 5.7, I just used following command and run migration.
touch /absolute/path/of/db.sqlite
That will create an empty file at defined location and you are good to go.
Observation: In Larvel 5.7, .sqlite file should be already available at defined location.
For windows, right click and create new file with the defined name at defined location.
try change the file databaseName.sqlite permission database to writeable and readable. Maybe change permissions folder database also.
In the production server change this file database to private directory for security.
Well it may be late but I have also faced the problem in ubuntu environment, here exactly how I overcome this issue.
You have to pass your fully qualified sqlite file's path to your .env file, like following:
DB_CONNECTION=sqlite
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE="/var/www/html/my-awesome-project/database/database.sqlite"
DB_USERNAME=homestead
DB_PASSWORD=secret
Here change the value of DB_DATABASE key to the exact location of your sqlite file in your computer's filesystem.
Tips:
You can find your database's path for sqlite file by adding
dd(database_path('database.sqlite'));
this line of code in the top of config/database.php file and run php artisan migrate to terminal. You can find the exact location from there, copy it and put it to .envs DB_DATABASE key.
Don't forget to restart laravel server and remove dd(database_path('database.sqlite')); form database.php before testing.
In my case, changing url: '%env(DATABASE_AUTH_URL)%' to url: '%env(resolve:AUTH_DATABASE_URL)%' fixed the issue
Please check the permissions of the directory that database file is stored in. chmod 777 YOU_APP/database
Change DB_CONNECTION from "mysql" to "sqlite".
Because this is an sqlite database, you can delete all the other DB_* items in that .env file.
APP_ENV=local
APP_KEY=base64:
APP_DEBUG=true
APP_LOG_LEVEL=debug
APP_URL=http://localhost
DB_CONNECTION=sqlite