Browse Source

Create module to import DatabaseModel subclasses

Both the initialize_db script and Alembic need to import all
DatabaseModel subclasses so that DatabaseModel.metadata has all the
models attached. Previously this wasn't being done properly, and the
intialize_db script wasn't creating the scraper_result table since it
hadn't been imported.

This commit creates a dedicated module to import all of those classes,
so that both those locations can simply import * from it instead of
needing to import all the models individually. This still isn't great
overall, but it should be less prone to mistakes at least.
merge-requests/40/head
Deimos 6 years ago
parent
commit
734220bdfe
  1. 15
      tildes/alembic/env.py
  2. 3
      tildes/scripts/initialize_db.py
  3. 19
      tildes/tildes/database_models.py

15
tildes/alembic/env.py

@ -11,22 +11,11 @@ config = context.config
# This line sets up loggers basically.
fileConfig(config.config_file_name)
# import all DatabaseModel subclasses here for autogenerate support
from tildes.models.comment import (
Comment,
CommentLabel,
CommentNotification,
CommentVote,
)
from tildes.models.group import Group, GroupSubscription
from tildes.models.log import Log
from tildes.models.message import MessageConversation, MessageReply
from tildes.models.scraper import ScraperResult
from tildes.models.topic import Topic, TopicVisit, TopicVote
from tildes.models.user import User, UserGroupSettings, UserInviteCode
from tildes.database_models import *
from tildes.models import DatabaseModel
target_metadata = DatabaseModel.metadata
# other values from the config, defined by the needs of env.py,

3
tildes/scripts/initialize_db.py

@ -2,7 +2,7 @@
# SPDX-License-Identifier: AGPL-3.0-or-later
"""Script for doing the initial setup of database tables."""
# pylint: disable=wrong-import-order
# pylint: disable=unused-wildcard-import,wildcard-import,wrong-import-order
import os
import subprocess
@ -12,6 +12,7 @@ from alembic import command
from alembic.config import Config
from sqlalchemy.engine import Connectable, Engine
from tildes.database_models import * # noqa
from tildes.lib.database import get_session_from_config
from tildes.models import DatabaseModel
from tildes.models.group import Group

19
tildes/tildes/database_models.py

@ -0,0 +1,19 @@
"""Module that imports all DatabaseModel subclasses.
This module shouldn't really be used for anything directly. It's for convenience so that
both Alembic and the script for initializing the database can simply import * from here.
"""
# pylint: disable=unused-import
from tildes.models.comment import (
Comment,
CommentLabel,
CommentNotification,
CommentVote,
)
from tildes.models.group import Group, GroupSubscription
from tildes.models.log import Log
from tildes.models.message import MessageConversation, MessageReply
from tildes.models.scraper import ScraperResult
from tildes.models.topic import Topic, TopicVisit, TopicVote
from tildes.models.user import User, UserGroupSettings, UserInviteCode
Loading…
Cancel
Save