Support foreign keys with SQLite (#590)
This commit is contained in:
@@ -78,6 +78,12 @@ services:
|
||||
- { name: doctrine.event_listener, event: prePersist, lazy: true }
|
||||
- { name: doctrine.event_listener, event: preUpdate, lazy: true }
|
||||
|
||||
# make sure, that sqlite supports foreign keys and cascade deletes
|
||||
App\Doctrine\SqliteSessionInitSubscriber:
|
||||
class: App\Doctrine\SqliteSessionInitSubscriber
|
||||
tags:
|
||||
- { name: doctrine.event_listener, event: postConnect }
|
||||
|
||||
# ================================================================================
|
||||
# FORMS
|
||||
# ================================================================================
|
||||
|
||||
37
src/Doctrine/SqliteSessionInitSubscriber.php
Normal file
37
src/Doctrine/SqliteSessionInitSubscriber.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Doctrine;
|
||||
|
||||
use Doctrine\Common\EventSubscriber;
|
||||
use Doctrine\DBAL\Event\ConnectionEventArgs;
|
||||
use Doctrine\DBAL\Events;
|
||||
|
||||
class SqliteSessionInitSubscriber implements EventSubscriber
|
||||
{
|
||||
/**
|
||||
* @param ConnectionEventArgs $args
|
||||
* @throws \Doctrine\DBAL\DBALException
|
||||
*/
|
||||
public function postConnect(ConnectionEventArgs $args)
|
||||
{
|
||||
if ('sqlite' !== strtolower($args->getDatabasePlatform()->getName())) {
|
||||
return;
|
||||
}
|
||||
$args->getConnection()->executeUpdate('PRAGMA foreign_keys = ON;');
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getSubscribedEvents()
|
||||
{
|
||||
return [Events::postConnect];
|
||||
}
|
||||
}
|
||||
@@ -43,13 +43,6 @@ Further readings:
|
||||
- [MariaDB - JSON support was added with 10.2.7](https://mariadb.com/kb/en/library/json-data-type/)
|
||||
- [Using JSON fields with Doctrine ORM on PostgreSQL & MySQL](https://symfony.fi/entry/using-json-fields-with-doctrine-orm-on-postgresql-mysql)
|
||||
|
||||
## SQLite not recommended for production usage
|
||||
|
||||
SQLite is a great database engine for testing, but when it comes to production usage it fails due to several reasons:
|
||||
|
||||
- It does not support ALTER TABLE commands and makes update procedures very clunky and problematic (we still try to support updates, but they are heavy on large databases)
|
||||
- It does not support FOREIGN KEY constraints [out of the box](https://www.sqlite.org/foreignkeys.html#fk_enable), which can lead to critical bugs when deleting users/activities/projects/customers
|
||||
|
||||
## Dotenv::populate() must be an instance of Symfony\\Component\\Dotenv\\void
|
||||
|
||||
If you encounter an error like this:
|
||||
|
||||
@@ -29,13 +29,13 @@ chmod -R g+rw var/
|
||||
cp .env.dist .env
|
||||
```
|
||||
|
||||
It's up to you which database server you want to use, Kimai v2 supports MySQL/MariaDB and SQLite, but SQLite is [not recommended](faq.md) for production usage.
|
||||
Configure the database connection string in your the `.env` file:
|
||||
Configure the database connection string in your the `.env` file (Kimai v2 supports MySQL/MariaDB and SQLite):
|
||||
```
|
||||
# adjust all settings in .env to your needs
|
||||
APP_ENV=prod
|
||||
DATABASE_URL=mysql://user:password@127.0.0.1:3306/database
|
||||
```
|
||||
SQLite is not recommended for production usage, check FAQ below.
|
||||
|
||||
Now install all dependencies for Kimai 2:
|
||||
```bash
|
||||
@@ -209,6 +209,16 @@ npm run prod
|
||||
|
||||
## FAQ and common problems
|
||||
|
||||
## SQLite not recommended for production usage
|
||||
|
||||
SQLite is a great database engine for testing, but when it comes to production usage it is imperfect due to several reasons:
|
||||
|
||||
- It does not support ALTER TABLE commands and makes update procedures very clunky and problematic (we still try to support updates, but they are heavy on large databases)
|
||||
- It does [not support FOREIGN KEY](https://www.sqlite.org/quirks.html#foreign_key_enforcement_is_off_by_default) constraints [out of the box](https://www.sqlite.org/foreignkeys.html#fk_enable), which can lead to critical bugs when deleting users/activities/projects/customers
|
||||
|
||||
Kimai works around the Foreign Keys issue by using a [Doctrine PostConnect EventSubscriber]({{ site.kimai_v2_file }}/src/Doctrine/SqliteSessionInitSubscriber.php) since v0.8.1,
|
||||
but it is not guaranteed that SQLite handles everything as expected.
|
||||
|
||||
### Malformed parameter "url"
|
||||
|
||||
If you see an error message like this, then you have a special character in your DATABASE_URL.
|
||||
|
||||
Reference in New Issue
Block a user