Setting up a Database

This will explain the basic steps involved in choosing and setting up a database.

Introduction

The following document will cover the choices you have for your Donation Store database and also information on how to choose a particular database and also how to set it up with Donation Store. While the most common database choices will be covered, there are a plethora of different databases available to use with Donation Store. This document will not cover all of them, however because Donation Store is a Django application, you can use any that are compatible with Django. 

Django uses an ORM to map Objects to the database of choice. By doing this, the Donation Store code that is written does not have to be changed depending on the database used, all that has to change is the particular database driver. The most common drivers/backends can be seen on the Django GitHub repository.

They provide drivers/backends for MySQL, Oracle, Postgres and SQLite, however there are other third party drivers available.

For the purpose of this tutorial, we will not compare the pros and cons of using a particular database, that is up to you. We will however show you how to get setup with both MySQL and Postgres. The setup differences are minimal.

Requirements

It is assumed that you have your database of choice setup and running either on the same server as the application or separately. It is advised that Donation Store runs in its own database, but it can run in a database alongside other databases you have running for your game server.

As Donation Store is a Django application, you may need to install a Python driver for your database of choice. You can see an example of the MySQL driver that is required on the Django website.

The Setup

In order to setup your database and to get the table information from Donation Store into your database, you must first setup your database then open the settings.py file found in the DonationStoreV2 folder within the main Donation Store files.

When opened you should see where the installed apps (3rd party libraries and internal Donation Store libraries are defined) and middleware (internal Django and Donation Store code needed for the application to operate properly) etc. Do not edit any of this as it will more than likely break the application.

Scroll down until you find the following:

DATABASES = {    
  'default': {        
    'ENGINE': 'django.db.backends.sqlite3',        
    'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),    
  }
}

By default (to keep things simple) Donation Store will use SQLite like above. To keep it that way then leave it as it is. If you want either MySQL or Postgres, change it to reflect either of the following:


MySQL

DATABASES = {    
  'default': {        
    'ENGINE': 'django.db.backends.mysql',        
    'NAME': 'database_name',        
    'USER': 'database_user',        
    'PASSWORD': 'database_password'    
  }
}


Postgres

DATABASES = {    
  'default': {        
    'ENGINE': 'django.db.backends.postgresql',        
    'NAME': 'database_name',        
    'USER': 'database_user',        
    'PASSWORD': 'database_password'    
  }
}

Save the above settings.py file and restart Gunicorn and Nginx. An easy way to restart Gunicorn is to find what process ID it is running as using:

ps aux | pgrep gunicorn

Once the process ID is found simply kill it using:

kill <process_id>

Then start Gunicorn back up as said in the Installation docs. To restart Nginx it's simply:

service nginx restart


The Driver

For the MySQL (or whatever database you chose) to communicate with Donation Store, it needs a Python driver of that database installed. It's a pretty simple step, just activate your virtual environment and run a pip install command with the name of your database driver as the package name. You can find a vast array of drivers online for your favourite database, however for our case, one of the MySQL drivers can be found here. For this, it would be:

pip install MySQL-python

If you get an error above along the lines of x86_64-linux-gnu-gcc, it means you may not have the Python dev libraries installed on your machine. To fix it, jus run:

sudo apt-get install python-dev

You can choose whichever you like that's up to you!


Building the Tables

To get the required Donation Store tables into the chosen into the chosen database, we must migrate our migrations. This basically instructs Donation Store to connect to your database and automatically populate the database with tables etc. This prevents you from having to execute an SQL statements, instead you can execute a single command.

Navigate to where the Donation Store files are located. If you followed the previous install doc, it should be in /home/donationstore/env. Make sure you are in the same location as the manage.py file. Once there simply run the following command to build the tables:

python manage.py migrate

And that's it! It is suggested that if you are coming from the original installation doc you should return there now.



Next Steps

Now that you have installed the database for Donation Store, if you are coming from the main app install return there, or continue on with some other setup docs:

Installing the Plugin

Setting up reCaptcha

Obtaining an SSL Certificate