Skip to content

Commit

Permalink
[Database] Allow SQLite (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
tchapi authored Mar 14, 2023
1 parent 1dfb25b commit 72dafd9
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 6 deletions.
4 changes: 2 additions & 2 deletions .env
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ APP_SECRET=630dc0d699fd37e720aff268f75583ed
###< symfony/framework-bundle ###

###> doctrine/doctrine-bundle ###
DATABASE_DRIVER=mysql # or postgresql, or sqlite
# Format described at https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html#connecting-using-a-url
# For a PostgreSQL database, use: "postgresql://db_user:db_password@127.0.0.1:5432/db_name?serverVersion=11&charset=UTF-8"
# For an SQLite database (not supported yet), use: "sqlite:///%kernel.project_dir%/var/data.db"
DATABASE_DRIVER=mysql # or postgresql
# For an SQLite database, use: "sqlite:///%kernel.project_dir%/var/data.db" (without the quotes so Symfony can resolve it if it's an absolute path)
DATABASE_URL="mysql://davis:davis@127.0.0.1:3306/davis?serverVersion=mariadb-10.6.10&charset=utf8mb4"
###< doctrine/doctrine-bundle ###

Expand Down
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ Created and maintained (with the help of the community) by [@tchapi](https://git

# Requirements

- PHP > 7.3.0 (with `pdo_mysql` [or `pdo_pgsql`], `gd` and `intl` extensions), compatible up to PHP 8.2
- A compatible database layer, such as MySQL or MariaDB (recommended) or PostgreSQL (not extensively tested yet)
- PHP > 7.3.0 (with `pdo_mysql` [or `pdo_pgsql`, `pdo_sqlite`], `gd` and `intl` extensions), compatible up to PHP 8.2
- A compatible database layer, such as MySQL or MariaDB (recommended), PostgreSQL (not extensively tested yet) or SQLite (not extensively tested yet)
- Composer > 2 (_The last release compatible with Composer 1 is [v1.6.2](https://github.com/tchapi/davis/releases/tag/v1.6.2)_)
- The [`imap`](https://www.php.net/manual/en/imap.installation.php) and [`ldap`](https://www.php.net/manual/en/ldap.installation.php) PHP extensions if you want to use either authentication methods (_these are not enabled / compiled by default except in the Docker image_)

Expand Down Expand Up @@ -58,7 +58,7 @@ Create your own `.env.local` file to change the necessary variables, if you plan
a. The database driver and url (_you should already have it configured since you created the database previously_)

```
DATABASE_DRIVER=mysql # or postgresql
DATABASE_DRIVER=mysql # or postgresql, or sqlite
DATABASE_URL=mysql://db_user:db_pass@host:3306/db_name?serverVersion=mariadb-10.6.10&charset=utf8mb4
```

Expand Down Expand Up @@ -322,11 +322,20 @@ You can start the containers with :
> ```
> cd docker && docker-compose -f docker-compose-postgresql.yml up -d
> ```
>
> Or SQLite with:
> ```
> cd docker && docker-compose -f docker-compose-sqlite.yml up -d
> ```
**⚠ Do not forget to run all the migrations the first time you run the container** :
docker exec -it davis sh -c "APP_ENV=prod bin/console doctrine:migrations:migrate --no-interaction"
> **Warning**
> For SQLite, you must also make sure that the folder the database will reside in AND the database file in itself have the right permissions! You can do for instance:
> `chown -R www-data: /data` if `/data` is the folder your SQLite database will be in, just after you have run the migrations
### Updating from a previous version
If you update the code, you need to make sure the database structure is in sync.
Expand Down
2 changes: 1 addition & 1 deletion config/packages/doctrine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ doctrine:
# The server_version must be configured directly in the
# DATABASE_URL to allow different drivers without adding
# too many env vars
driver: '%env(string:default:default_database_driver:DATABASE_DRIVER)%'
driver: 'pdo_%env(string:default:default_database_driver:DATABASE_DRIVER)%'

# only needed for MySQL
charset: utf8mb4
Expand Down
51 changes: 51 additions & 0 deletions docker/docker-compose-sqlite.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
version: "3.7"
name: "davis-docker"

services:

nginx:
image: nginx:1.23-alpine
container_name: nginx
command: "/bin/sh -c 'while :; do sleep 6h & wait $${!}; nginx -s reload; done & nginx -g \"daemon off;\"'"
depends_on:
- davis
volumes:
- davis_www:/var/www/davis
- type: bind
source: ./configurations/davis.conf
target: /etc/nginx/conf.d/default.conf
ports:
- 9000:80

davis:
build:
context: ../
dockerfile: ./docker/Dockerfile
image: davis:latest
# If you want to use a prebuilt image from Github
# image: ghcr.io/tchapi/davis:edge
container_name: davis
environment:
- APP_ENV=prod
- DATABASE_DRIVER=sqlite
- DATABASE_URL=sqlite:////data/davis-database.db # ⚠️ 4 slashes for an absolute path ⚠️ + no quotes (so Symfony can resolve it)
- MAILER_DSN=smtp://${MAIL_USERNAME}:${MAIL_PASSWORD}@${MAIL_HOST}:${MAIL_PORT}
- ADMIN_LOGIN=${ADMIN_LOGIN}
- ADMIN_PASSWORD=${ADMIN_PASSWORD}
- AUTH_REALM=${AUTH_REALM}
- AUTH_METHOD=${AUTH_METHOD}
- CALDAV_ENABLED=${CALDAV_ENABLED}
- CARDDAV_ENABLED=${CARDDAV_ENABLED}
- WEBDAV_ENABLED=${WEBDAV_ENABLED}
- WEBDAV_TMP_DIR=${WEBDAV_TMP_DIR}
- WEBDAV_PUBLIC_DIR=${WEBDAV_PUBLIC_DIR}
- INVITE_FROM_ADDRESS=${INVITE_FROM_ADDRESS}
volumes:
- davis_www:/var/www/davis
- davis_data:/data

volumes:
davis_www:
name: davis_www
davis_data:
name: davis_data
68 changes: 68 additions & 0 deletions migrations/Version20221211154443.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace DoctrineMigrations;

use Doctrine\DBAL\Schema\Schema;
use Doctrine\Migrations\AbstractMigration;

/**
* SQLite - Initial migration: Create all necessary sabre/dav tables.
*/
final class Version20221211154443 extends AbstractMigration
{
public function getDescription(): string
{
return '[SQLite] Initial migration: Create all necessary sabre/dav tables';
}

public function up(Schema $schema): void
{
$this->skipIf('sqlite' !== $this->connection->getDatabasePlatform()->getName(), 'This migration is specific to \'sqlite\'. Skipping it is fine.');

$this->addSql('CREATE TABLE addressbookchanges (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, addressbookid INTEGER NOT NULL, uri VARCHAR(255) NOT NULL, synctoken VARCHAR(255) NOT NULL, operation INTEGER NOT NULL)');
$this->addSql('CREATE INDEX IDX_EB122CD58B26C2E9 ON addressbookchanges (addressbookid)');
$this->addSql('CREATE TABLE addressbooks (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, principaluri VARCHAR(255) NOT NULL, displayname VARCHAR(255) NOT NULL, uri VARCHAR(255) NOT NULL, description CLOB DEFAULT NULL, synctoken VARCHAR(255) NOT NULL)');
$this->addSql('CREATE TABLE calendarchanges (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, calendarid INTEGER NOT NULL, uri VARCHAR(255) NOT NULL, synctoken INTEGER NOT NULL, operation SMALLINT NOT NULL)');
$this->addSql('CREATE INDEX IDX_737547E2B8CB7204 ON calendarchanges (calendarid)');
$this->addSql('CREATE TABLE calendarinstances (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, calendarid INTEGER NOT NULL, principaluri VARCHAR(255) DEFAULT NULL, access SMALLINT DEFAULT 1 NOT NULL, displayname VARCHAR(255) DEFAULT NULL, uri VARCHAR(255) DEFAULT NULL, description CLOB DEFAULT NULL, calendarorder INTEGER DEFAULT 0 NOT NULL, calendarcolor VARCHAR(10) DEFAULT NULL, timezone CLOB DEFAULT NULL, transparent INTEGER DEFAULT NULL, share_href VARCHAR(255) DEFAULT NULL, share_displayname VARCHAR(255) DEFAULT NULL, share_invitestatus INTEGER DEFAULT 2 NOT NULL)');
$this->addSql('CREATE INDEX IDX_51856561B8CB7204 ON calendarinstances (calendarid)');
$this->addSql('CREATE TABLE calendarobjects (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, calendarid INTEGER NOT NULL, calendardata BLOB DEFAULT NULL, uri VARCHAR(255) DEFAULT NULL, lastmodified INTEGER DEFAULT NULL, etag VARCHAR(255) DEFAULT NULL, size INTEGER NOT NULL, componenttype VARCHAR(255) DEFAULT NULL, firstoccurence INTEGER DEFAULT NULL, lastoccurence INTEGER DEFAULT NULL, uid VARCHAR(255) DEFAULT NULL)');
$this->addSql('CREATE INDEX IDX_E14F332CB8CB7204 ON calendarobjects (calendarid)');
$this->addSql('CREATE TABLE calendars (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, synctoken VARCHAR(255) NOT NULL, components VARCHAR(255) DEFAULT NULL)');
$this->addSql('CREATE TABLE calendarsubscriptions (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, uri VARCHAR(255) NOT NULL, principaluri VARCHAR(255) NOT NULL, source CLOB DEFAULT NULL, displayname VARCHAR(255) DEFAULT NULL, refreshrate VARCHAR(10) DEFAULT NULL, calendarorder INTEGER NOT NULL, calendarcolor VARCHAR(10) DEFAULT NULL, striptodos SMALLINT DEFAULT NULL, stripalarms SMALLINT DEFAULT NULL, stripattachments SMALLINT DEFAULT NULL, lastmodified INTEGER DEFAULT NULL)');
$this->addSql('CREATE TABLE cards (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, addressbookid INTEGER NOT NULL, carddata BLOB DEFAULT NULL, uri VARCHAR(255) DEFAULT NULL, lastmodified INTEGER DEFAULT NULL, etag VARCHAR(32) DEFAULT NULL, size INTEGER NOT NULL)');
$this->addSql('CREATE INDEX IDX_4C258FD8B26C2E9 ON cards (addressbookid)');
$this->addSql('CREATE TABLE locks (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, owner VARCHAR(255) DEFAULT NULL, timeout INTEGER DEFAULT NULL, created INTEGER DEFAULT NULL, token VARCHAR(255) DEFAULT NULL, scope SMALLINT DEFAULT NULL, depth SMALLINT DEFAULT NULL, uri VARCHAR(255) DEFAULT NULL)');
$this->addSql('CREATE TABLE principals (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, uri VARCHAR(255) NOT NULL, email VARCHAR(255) DEFAULT NULL, displayname VARCHAR(255) DEFAULT NULL, is_main BOOLEAN NOT NULL, is_admin BOOLEAN NOT NULL)');
$this->addSql('CREATE UNIQUE INDEX UNIQ_E797E7FB841CB121 ON principals (uri)');
$this->addSql('CREATE TABLE groupmembers (principal_id INTEGER NOT NULL, member_id INTEGER NOT NULL, PRIMARY KEY(principal_id, member_id))');
$this->addSql('CREATE INDEX IDX_6F15EDAC474870EE ON groupmembers (principal_id)');
$this->addSql('CREATE INDEX IDX_6F15EDAC7597D3FE ON groupmembers (member_id)');
$this->addSql('CREATE TABLE propertystorage (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, path VARCHAR(255) NOT NULL, name VARCHAR(255) NOT NULL, valuetype INTEGER DEFAULT NULL, value CLOB DEFAULT NULL)');
$this->addSql('CREATE TABLE schedulingobjects (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, principaluri VARCHAR(255) DEFAULT NULL, calendardata BLOB DEFAULT NULL, uri VARCHAR(255) DEFAULT NULL, lastmodified INTEGER DEFAULT NULL, etag VARCHAR(255) DEFAULT NULL, size INTEGER NOT NULL)');
$this->addSql('CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, username VARCHAR(255) NOT NULL, digesta1 VARCHAR(255) NOT NULL)');
$this->addSql('CREATE UNIQUE INDEX UNIQ_1483A5E9F85E0677 ON users (username)');
}

public function down(Schema $schema): void
{
$this->skipIf('sqlite' !== $this->connection->getDatabasePlatform()->getName(), 'This migration is specific to \'sqlite\'. Skipping it is fine.');

$this->addSql('DROP TABLE addressbookchanges');
$this->addSql('DROP TABLE addressbooks');
$this->addSql('DROP TABLE calendarchanges');
$this->addSql('DROP TABLE calendarinstances');
$this->addSql('DROP TABLE calendarobjects');
$this->addSql('DROP TABLE calendars');
$this->addSql('DROP TABLE calendarsubscriptions');
$this->addSql('DROP TABLE cards');
$this->addSql('DROP TABLE locks');
$this->addSql('DROP TABLE principals');
$this->addSql('DROP TABLE groupmembers');
$this->addSql('DROP TABLE propertystorage');
$this->addSql('DROP TABLE schedulingobjects');
$this->addSql('DROP TABLE users');
}
}

0 comments on commit 72dafd9

Please sign in to comment.