Add db table for plugin files

This commit is contained in:
Tulir Asokan 2020-01-02 02:03:53 +02:00
parent 8a711e0c8e
commit ab88568d08
3 changed files with 54 additions and 2 deletions

View File

@ -0,0 +1,40 @@
"""Let plugins have multiple files
Revision ID: 6b66c1600d16
Revises: d295f8dcfa64
Create Date: 2020-01-02 01:30:51.622962
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = "6b66c1600d16"
down_revision = "d295f8dcfa64"
branch_labels = None
depends_on = None
def upgrade():
plugin_file: sa.Table = op.create_table(
"plugin_file",
sa.Column("plugin_id", sa.String(length=255), nullable=False),
sa.Column("file_name", sa.String(length=255), nullable=False),
sa.Column("content", sa.Text(), nullable=False),
sa.ForeignKeyConstraint(("plugin_id",), ["plugin.id"], onupdate="CASCADE",
ondelete="CASCADE"),
sa.PrimaryKeyConstraint("plugin_id", "file_name"))
conn: sa.engine.Connection = op.get_bind()
conn.execute(plugin_file.insert().values([{
"plugin_id": plugin_id,
"file_name": "config.yaml",
"content": config
} for plugin_id, config in conn.execute("SELECT id, config FROM plugin").fetchall()]))
op.drop_column("plugin", "config")
def downgrade():
op.add_column("plugin", sa.Column("config", sa.TEXT(), autoincrement=False, nullable=False))
op.drop_table("plugin_file")

View File

@ -36,7 +36,6 @@ class DBPlugin(Base):
primary_user: UserID = Column(String(255),
ForeignKey("client.id", onupdate="CASCADE", ondelete="RESTRICT"),
nullable=False)
config: str = Column(Text, nullable=False, default='')
@classmethod
def all(cls) -> Iterable['DBPlugin']:
@ -47,6 +46,20 @@ class DBPlugin(Base):
return cls._select_one_or_none(cls.c.id == id)
class DBPluginFile(Base):
__tablename__ = "plugin_file"
plugin_id: str = Column(String(255),
ForeignKey("plugin.id", onupdate="CASCADE", ondelete="CASCADE"),
primary_key=True)
file_name: str = Column(String(255), primary_key=True)
content: str = Column(Text, nullable=False, default="")
@classmethod
def all_for_plugin(cls, id: str) -> Iterable['DBPluginFile']:
return cls._select_all(cls.c.plugin_id == id)
class DBClient(Base):
__tablename__ = "client"

View File

@ -84,4 +84,3 @@ class PrefixResource(web.Resource):
def raw_match(self, path: str) -> bool:
return path and path.startswith(self._prefix)