Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use SQLite as default DB; added migration instructions #13

Closed
wants to merge 12 commits into from
62 changes: 59 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ See the real world example at [Pantsu.cat](https://pantsu.cat/).

## Requirements

Original development environment is Nginx + PHP5.5 + MySQL, but is confirmed to
Original development environment is Nginx + PHP5.5 + SQLite, but is confirmed to
work with Apache 2.4 and newer PHP versions. Should work with any other
PDO-compatible database.
PDO-compatible database. Is known to work with MySQL, which has been the previous
default.

## Install

For the purposes of this guide, we won't cover setting up Nginx, PHP, MySQL,
For the purposes of this guide, we won't cover setting up Nginx, PHP, or SQLite,
Node, or NPM. So we'll just assume you already have them all running well.

### Compiling
Expand All @@ -47,6 +48,15 @@ After this, the pomf site is now compressed and set up inside `dist/`.

## Configuring

We need to create the SQLite database before it may be used by pomf.
Fortunately, this is incredibly simple.

To create it from the schema, simply run `sqlite3 /path/to/db.sq3 -init /path/to/pomf/sqlite_schema.sql`,
obviously ensuring the paths are correct. Using default paths, this would be
`sqlite3 /usr/share/nginx/pomf.sq3 -init /usr/share/nginx/html/sqlite_schema.sql`.

_NOTE: The **directory** where the SQLite database is stored, must be writable by the user the web server is running as_

The majority of the settings are in `static/includes/settings.inc.php`.

For file size configuration, open `Gruntfile.js` in an editor and modify the
Expand All @@ -62,6 +72,52 @@ A best practice is to disable executing `.php` files on the `POMF_URL` domain
for uploaded files. This assures that a malicious user cannot execute arbitrary
PHP code on the server.

### Migrating from MySQL to SQLite

Previously, we have used MySQL as the default database platform for pomf. Generally this
is undesirable.

MySQL is relatively complicated to administer, brings in many unneeded dependencies, and consumes
more resources than SQLite would. Additonally, poorly configured installations have the potential
to pose a security risk.

Fortunately, it is incredibly simple to migrate your database. This may be done on a live server, and should require
zero downtime.

**Note: some directories that were initally in the web root, are now in static/php/. Ensure you consider this when reading the following**

*You may test this first in a subdirectory (or vhost, or equivelant) if you wish. If you make a mistake, however, only uploading will temporarily be impacted. None of these steps are destructive, and are easily reverted.*

Make a copy of the file `static/php/includes/settings.inc.php`, and edit it, making the changes outlined below. Note where you save it.
```php
define('POMF_DB_CONN', '[stuff]'); ---> define('POMF_DB_CONN', 'sqlite:/usr/share/nginx/pomf.sq3');`
define('POMF_DB_USER', '[stuff]'); ---> define('POMF_DB_USER', null);
define('POMF_DB_PASS', '[stuff]'); ---> define('POMF_DB_PASS', null);
```

The following script will make a dump of the MySQL database, convert it to a format acceptable for SQLite, then initialise a new SQLite database with the contents of the dump. It will then backup your existing `settings.inc.php` file, and move the new one into place.
```bash
#!/bin/bash
# ensure you change these to match your environment
OLD_DB_USER=pomf
OLD_DB_PASS=pass
SETTINGS_INC_FILE='/usr/share/nginx/html/static/php/includes/settings.inc.php'
NEW_SETTINGS_INC_FILE='/path/to/edited/file'
# it is unlikely the following two need to be changed
OLD_DB_NAME=pomf
NEW_DB_PATH='/usr/share/nginx/pomf.sq3'

wget -O /tmp/m2s https://github.com/dumblob/mysql2sqlite/raw/master/mysql2sqlite.sh
mysqldump -u $OLD_DB_USER -p $OLD_DB_PASS $OLD_DB_NAME | sh /tmp/m2s | sqlite3 $NEW_DB_PATH
rm /tmp/m2s
echo == SQLite database has been prepared at $NEW_DB_PATH ==
cp $SETTINGS_INC_FILE ${SETTINGS_INC_FILE}.bak
mv $TMP_SETTINGS_INC_FILE $SETTINGS_INC_FILE
echo == Backed up old settings.inc.file, and moved new one into place ==
```

Ensure you are able to upload files, and then, all done! You may now uninstall, or disable. MySQL if you wish.

### Apache

If you are running Apache and want to compress your output when serving files,
Expand Down
File renamed without changes.
16 changes: 16 additions & 0 deletions sqlite_schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
PRAGMA synchronous = OFF;
PRAGMA journal_mode = MEMORY;
BEGIN TRANSACTION;
CREATE TABLE `files` (
`id` integer NOT NULL PRIMARY KEY AUTOINCREMENT
, `hash` char(40) default NULL
, `originalname` varchar(255) default NULL
, `filename` varchar(30) default NULL
, `size` integer default NULL
, `date` date default NULL
, `expire` date default NULL
, `delid` char(40) default NULL
, `user` integer default '0'
, `dir` integer default '0'
);
END TRANSACTION;
15 changes: 11 additions & 4 deletions static/php/includes/settings.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,23 @@
* PDO_MYSQL DSN.
* @param string POMF_DB_CONN DSN:host|unix_socket=hostname|path;dbname=database
*/
/* example MySQL DSN
define('POMF_DB_CONN', 'mysql:unix_socket=/tmp/mysql.sock;dbname=pomf');
*/
/* NOTE: the directory, not just the file, your database is stored in must
be writable by the web server user
*/
define('POMF_DB_CONN', 'sqlite:/usr/share/nginx/pomf.sq3');

/**
* PDO database login credentials
* NOTE: not necessary for SQLite; these variables are ignored if SQLite is used
*/

/** @param string POMF_DB_NAME Database username */
define('POMF_DB_USER', 'pomf');
/** @param string POMF_DB_PASS Database password */
define('POMF_DB_PASS', '***');
/** @param string|null POMF_DB_NAME Database username */
define('POMF_DB_USER', null);
/** @param string|null POMF_DB_PASS Database password */
define('POMF_DB_PASS', null);

/**
* File system location where to store uploaded files
Expand Down