From edf328e7130a048c3a09da3bb73b6588ee92e797 Mon Sep 17 00:00:00 2001 From: hygienic-books Date: Wed, 4 May 2022 03:17:06 +0200 Subject: [PATCH] feat(db): Set up database via alembic migration, add docs --- README.md | 35 +++++++++++++++++++ .../68d8f4d96043_add_kodi_instances_table.py | 33 +++++++++++++++++ 2 files changed, 68 insertions(+) create mode 100644 db/versions/68d8f4d96043_add_kodi_instances_table.py diff --git a/README.md b/README.md index 1f1c75c..18397e3 100644 --- a/README.md +++ b/README.md @@ -2,6 +2,41 @@ An approach to having kids use a Kodi media center instance: manage session use and keep track of time spent and available budget. +## Getting started + +1. Set up a MySQL database by whatever means you prefer. This step is outside the scope of this *getting started* guide. + * In your MySQL daemon create one database and a user with all grants except for the `GRANT` grant on that database, here with account `kodi-timekeeper` as an example on a MySQL 5.7.22 daemon: + ``` + # Create account + CREATE USER 'kodi-timekeeper'@'localhost' IDENTIFIED WITH 'mysql_native_password' AS '*hash' REQUIRE NONE PASSWORD EXPIRE DEFAULT ACCOUNT UNLOCK; + + # Grants for kodi-timekeeper@localhost + GRANT USAGE ON *.* TO 'kodi-timekeeper'@'localhost'; + GRANT ALL PRIVILEGES ON `kodi-timekeeper`.* TO 'kodi-timekeeper'@'localhost'; + ``` + Here `hash` is the hashed account password. To compute it yourself head into your MySQL daemon and execute the query: + ``` + SELECT CONCAT('*', UPPER(SHA1(UNHEX(SHA1('topsecret'))))); + ``` + The result of which will be something like: + ``` + *6C47D9CD3A183D230B04FE7F38D7D313E2B4B5AE + ``` + Which is the hash representation of password `topsecret`. This query-hash mechanism comes courtesy of René Cannaò formerly of Pythian Services Inc. in his 2011 [article "Hashing Algorithm in MySQL PASSWORD()" at blog.pythian.com](https://blog.pythian.com/hashing-algorithm-in-mysql-password-2/). +2. On your operating system make sure the `mysql_config` binary exists. On a Debian or a derivative distribution that typically involves `sudo apt-get install default-libmysqlclient-dev`. +3. `git clone` this repo +4. Install requirements via `pip install -r requirements.txt` +5. Export the following shell environment variables as needed. The next step will use these variables to connect to your database and create tables. Values below are defaults so if you want to stick to a default feel free to simply not export the variable. + ``` + export DB_DIALECTDRIVER='mysql' + export DB_USER='kodi-timekeeper' + export DB_PASSWORD='-kodi-timekeeper' + export DB_HOST='localhost' + export DB_NAME='kodi-timekeeper' + ``` +6. Navigate into the repository's `db` subdirectory +7. Initialize your database: `alembic upgrade head` + ## Valid Conventional Commits scopes * `meta`: Affects the Git project's structure such as for example a `pyproject.toml` change, adding a new directory or changing how a news fragment file is formatted diff --git a/db/versions/68d8f4d96043_add_kodi_instances_table.py b/db/versions/68d8f4d96043_add_kodi_instances_table.py new file mode 100644 index 0000000..8cc0fd5 --- /dev/null +++ b/db/versions/68d8f4d96043_add_kodi_instances_table.py @@ -0,0 +1,33 @@ +"""Add Kodi instances table + +Revision ID: 68d8f4d96043 +Revises: +Create Date: 2022-05-04 01:35:12.791868 + +""" +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision = '68d8f4d96043' +down_revision = None +branch_labels = None +depends_on = None + + +def upgrade(): + op.create_table( + 'kodi-instances', + sa.Column('id', sa.Integer, primary_key=True), + sa.Column('webserver-proto', sa.String(10), nullable=False, server_default="ws"), + sa.Column('webserver-addr', sa.String(40), nullable=False, server_default="localhost"), + sa.Column('webserver-port', sa.Integer, nullable=False, server_default="8080"), + sa.Column('webserver-jsonrpcpath', sa.String(10), nullable=False, server_default="/jsonrpc"), + sa.Column('webserver-username', sa.String(48)), + sa.Column('webserver-password', sa.String(48)) + ) + + +def downgrade(): + op.drop_table('kodi-instances')