diff options
| author | 2018-04-15 18:47:58 +0200 | |
|---|---|---|
| committer | 2018-04-15 17:47:58 +0100 | |
| commit | f5e7c358761ee49da45933e73b943fb1eb66c030 (patch) | |
| tree | cde9eed9f010caba22842d39af33385aae411a45 /pysite/database.py | |
| parent | Added an About category with Partners view (#1pv7h) (#50) (diff) | |
Initial data for tables and refactoring the dev-mode database handling to be similar to prod-mode. (#53)
* Changed the dev-mode logic to be the same as prod for creating new tables if they don't exist. Also added a new feature where a table can be initialized with data if you create a JSON file in the pysite/database/table_init/ folder and fill it with a list of dicts where each dict represents a row in your table. Included a hiphoppers json so that I can actually test if it works in production. It will only init the table if the table is empty.
* Not sure if this will solve it, but I think so.
* Renamed the tables and primary keys, and alphabetized the dict. Now complies with the gdudes holy wishes.
Diffstat (limited to '')
| -rw-r--r-- | pysite/database.py | 70 | 
1 files changed, 63 insertions, 7 deletions
diff --git a/pysite/database.py b/pysite/database.py index 82e1e84e..3ecdf53b 100644 --- a/pysite/database.py +++ b/pysite/database.py @@ -1,4 +1,5 @@  # coding=utf-8 +import json  import logging  import os  from typing import Any, Callable, Dict, Iterator, List, Optional, Union @@ -8,13 +9,16 @@ from flask import abort  from rethinkdb.ast import RqlMethodQuery, Table, UserError  from rethinkdb.net import DefaultConnection +from pysite.constants import DEBUG_MODE +  ALL_TABLES = {      # table: primary_key - +    "hiphopify": "user_id", +    "hiphopify_namelist": "name",      "oauth_data": "id",      "tags": "tag_name",      "users": "user_id", -    "wiki": "slug", +    "wiki": "slug"  } @@ -30,22 +34,74 @@ class RethinkDB:          if loop_type:              rethinkdb.set_loop_type(loop_type) -        with self.get_connection(connect_database=False) as conn: +        with self.get_connection() as self.conn:              try: -                rethinkdb.db_create(self.database).run(conn) +                rethinkdb.db_create(self.database).run(self.conn)                  self.log.debug(f"Database created: '{self.database}'")              except rethinkdb.RqlRuntimeError:                  self.log.debug(f"Database found: '{self.database}'") -    def create_tables(self) -> int: -        created = 0 +            if DEBUG_MODE: +                # Create any table that doesn't exist +                created = self.create_tables() +                if created: +                    tables = ", ".join([f"{table}" for table in created]) +                    self.log.debug(f"Created the following tables: {tables}") + +                # Init the tables that require initialization +                initialized = self.init_tables() +                if initialized: +                    tables = ", ".join([f"{table} ({count} items)" for table, count in initialized.items()]) +                    self.log.debug(f"Initialized the following tables: {tables}") + +    def create_tables(self) -> List[str]: +        """ +        Creates whichever tables exist in the ALL_TABLES +        constant if they don't already exist in the database. + +        :return: a list of the tables that were created. +        """ +        created = []          for table, primary_key in ALL_TABLES.items():              if self.create_table(table, primary_key): -                created += 1 +                created.append(table)          return created +    def init_tables(self) -> Dict[str, int]: +        """ +        If the table is empty, and there is a corresponding JSON file with data, +        then we fill the table with the data in the JSON file. + +        The JSON files are contained inside of pysite/database/table_init/ +        :return: +        """ + +        initialized = {} + +        for table, primary_key in ALL_TABLES.items(): + +            # If the table is empty +            if not self.pluck(table, primary_key): + +                # And a corresponding JSON file exists +                if os.path.isfile(f"pysite/database/table_init/{table}.json"): + +                    # Load in all the data in that file. +                    with open(f"pysite/database/table_init/{table}.json") as json_file: +                        table_data = json.load(json_file) + +                        for row in table_data: +                            self.insert( +                                table, +                                row +                            ) + +                    initialized[table] = len(table_data) + +        return initialized +      def get_connection(self, connect_database: bool = True) -> DefaultConnection:          """          Grab a connection to the RethinkDB server, optionally without selecting a database  |