Make alembic result consistent with definitions in db.py and add bot_id to bot_chat table

This commit is contained in:
Tulir Asokan
2018-06-22 21:02:37 +03:00
parent b127afbf9b
commit 17aefd02da
4 changed files with 64 additions and 4 deletions
+13
View File
@@ -4,10 +4,12 @@ from logging.config import fileConfig
import sys
from os.path import abspath, dirname
sys.path.insert(0, dirname(dirname(abspath(__file__))))
from mautrix_telegram.base import Base
from mautrix_telegram.config import Config
from alchemysession import AlchemySessionContainer
import mautrix_telegram.db
# this is the Alembic Config object, which provides
@@ -20,6 +22,15 @@ mxtg_config.load()
config.set_main_option("sqlalchemy.url",
mxtg_config.get("appservice.database", "sqlite:///mautrix-telegram.db"))
class FakeDB:
@staticmethod
def query_property():
return None
AlchemySessionContainer.create_table_classes(FakeDB(), "telethon_", Base)
# Interpret the config file for Python logging.
# This line sets up loggers basically.
fileConfig(config.config_file_name)
@@ -30,6 +41,7 @@ fileConfig(config.config_file_name)
# target_metadata = mymodel.Base.metadata
target_metadata = Base.metadata
# other values from the config, defined by the needs of env.py,
# can be acquired:
# my_important_option = config.get_main_option("my_important_option")
@@ -77,6 +89,7 @@ def run_migrations_online():
with context.begin_transaction():
context.run_migrations()
if context.is_offline_mode():
run_migrations_offline()
else:
@@ -0,0 +1,46 @@
"""Add bot_id to BotChat
Revision ID: 6e8b438b5e84
Revises: 2228d49c383f
Create Date: 2018-06-22 16:32:31.922480
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "6e8b438b5e84"
down_revision = "2228d49c383f"
branch_labels = None
depends_on = None
convention = {
"pk": "pk_%(table_name)s"
}
metadata = sa.MetaData(naming_convention=convention)
def upgrade():
conn = op.get_bind()
res = conn.execute("SELECT id, `type` FROM bot_chat")
results = res.fetchall()
op.drop_table("bot_chat")
bot_chat = op.create_table('bot_chat',
sa.Column('bot_id', sa.Integer),
sa.Column('chat_id', sa.Integer),
sa.Column('type', sa.String, nullable=False),
sa.PrimaryKeyConstraint('bot_id', 'chat_id'))
op.bulk_insert(bot_chat, [{"bot_id": 0, "chat_id": r[0], "type": r[1]} for r in results])
def downgrade():
conn = op.get_bind()
res = conn.execute("SELECT chat_id, `type` FROM bot_chat WHERE bot_id=0")
results = res.fetchall()
op.drop_table("bot_chat")
bot_chat = op.create_table('bot_chat',
sa.Column('id', sa.Integer),
sa.Column('type', sa.String, nullable=False),
sa.PrimaryKeyConstraint('id'))
op.bulk_insert(bot_chat, [{"id": r[0], "type": r[1]} for r in results])
@@ -17,7 +17,7 @@ depends_on = None
def upgrade():
op.add_column('telegram_file',
sa.Column('timestamp', sa.BigInteger(), nullable=False, default=0,
sa.Column('timestamp', sa.BigInteger(), nullable=True, default=0,
server_default="0"))
+4 -3
View File
@@ -28,7 +28,7 @@ class Portal(Base):
# Telegram chat information
tgid = Column(Integer, primary_key=True)
tg_receiver = Column(Integer, primary_key=True)
peer_type = Column(String)
peer_type = Column(String, nullable=False)
megagroup = Column(Boolean)
# Matrix portal information
@@ -74,7 +74,7 @@ class User(Base):
mxid = Column(String, primary_key=True)
tgid = Column(Integer, nullable=True, unique=True)
tg_username = Column(String, nullable=True)
saved_contacts = Column(Integer, default=0)
saved_contacts = Column(Integer, default=0, nullable=False)
contacts = relationship("Contact", uselist=True,
cascade="save-update, merge, delete, delete-orphan")
portals = relationship("Portal", secondary="user_portal")
@@ -104,7 +104,8 @@ class Puppet(Base):
class BotChat(Base):
query = None
__tablename__ = "bot_chat"
id = Column(Integer, primary_key=True)
bot_id = Column(Integer, primary_key=True, default=0)
chat_id = Column(Integer, primary_key=True)
type = Column(String, nullable=False)