Create standalone_next_batch table automatically

This commit is contained in:
Tulir Asokan 2022-03-27 20:27:54 +03:00
parent 484749c08c
commit e9e325a871
2 changed files with 22 additions and 2 deletions

View file

@ -49,7 +49,7 @@ from ..plugin_base import Plugin
from ..plugin_server import PluginWebApp, PrefixResource from ..plugin_server import PluginWebApp, PrefixResource
from ..server import AccessLogger from ..server import AccessLogger
from .config import Config from .config import Config
from .database import NextBatch from .database import NextBatch, upgrade_table
from .loader import FileSystemLoader from .loader import FileSystemLoader
crypto_import_error = None crypto_import_error = None
@ -124,6 +124,7 @@ db = Database.create(
db_args=config.get("database_opts", {}), db_args=config.get("database_opts", {}),
ignore_foreign_tables=True, ignore_foreign_tables=True,
log=logging.getLogger("maubot.db"), log=logging.getLogger("maubot.db"),
upgrade_table=upgrade_table,
) )
user_id = config["user.credentials.id"] user_id = config["user.credentials.id"]

View file

@ -15,11 +15,30 @@
# along with this program. If not, see <https://www.gnu.org/licenses/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from __future__ import annotations from __future__ import annotations
import logging
from attr import dataclass from attr import dataclass
from mautrix.client import SyncStore from mautrix.client import SyncStore
from mautrix.types import FilterID, SyncToken, UserID from mautrix.types import FilterID, SyncToken, UserID
from mautrix.util.async_db import Database from mautrix.util.async_db import Connection, Database, UpgradeTable
upgrade_table = UpgradeTable(
version_table_name="standalone_version", log=logging.getLogger("maubot.db")
)
@upgrade_table.register(description="Initial revision")
async def upgrade_v1(conn: Connection) -> None:
await conn.execute(
"""CREATE TABLE IF NOT EXISTS standalone_next_batch (
user_id TEXT NOT NULL,
next_batch TEXT,
filter_id TEXT,
PRIMARY KEY (user_id)
)"""
)
find_q = "SELECT next_batch, filter_id FROM standalone_next_batch WHERE user_id=$1" find_q = "SELECT next_batch, filter_id FROM standalone_next_batch WHERE user_id=$1"
insert_q = "INSERT INTO standalone_next_batch (user_id, next_batch, filter_id) VALUES ($1, $2, $3)" insert_q = "INSERT INTO standalone_next_batch (user_id, next_batch, filter_id) VALUES ($1, $2, $3)"