Skip to content
This repository has been archived by the owner on Jan 24, 2024. It is now read-only.

Commit

Permalink
- Added command line interface to initdb.py (and additional dev dep…
Browse files Browse the repository at this point in the history
…endency "Typer", which is a wrapper around "click")

- Moved botfather commands out of README into a separate file
- Added docker/docker-compose.yml for easy setup of development database
- Updated README
  • Loading branch information
Joscha Götzer committed May 29, 2020
1 parent 3cf9f6d commit f428cbf
Show file tree
Hide file tree
Showing 7 changed files with 117 additions and 44 deletions.
49 changes: 20 additions & 29 deletions README.md
Expand Up @@ -127,44 +127,35 @@ Thanks to my patreons:


## Installation and Starting:
**This bot is developed for Linux.** Windows isn't tested, but it shouldn't be too hard to make it compatible. Feel free to create a PR.

Dependencies:
- `poetry` to manage and install dependencies.
- Ultimate Pollbot uses Postgres. Make sure the user has write/read rights.
- [Poetry](https://python-poetry.org/) to manage and install dependencies.
- Ultimate Pollbot uses Postgres. Make sure the user has write/read rights. You can use [the provided docker-compose
file](https://github.com/Nukesor/ultimate-poll-bot/blob/master/docker/docker-compose.yml) to set up a local development
environment.


1. Clone the repository:

% git clone git@github.com:nukesor/ultimate-poll-bot pollbot && cd pollbot

2. Execute `poetry install` to install all dependencies.
3. Either start the poll bot once with `poetry run python main.py` or copy the `pollbot.toml` manually to `~/.config/pollbot.toml` and adjust all necessary values.
4. Run `poetry run python initdb.py` to initialize the database.
5. Start the bot `poetry run python main.py`
1. Execute `poetry install` to install all dependencies.
1. Either start the poll bot once with `poetry run python main.py` or copy the `pollbot.toml` manually to `~/.config
/pollbot.toml` and adjust all necessary values. On Windows, the tilde (`~`) will substitute to your home directory
, usually at `C:\Users\your.name\.config\pollbot.toml`.
1. Run `poetry run python initdb.py` to initialize the database (or recreate it, if necessary).
1. Start the bot by running `poetry run python main.py`.
1. If you plan to keep your database schema up-to-date, you need to set the current alembic revision manually with
`poetry run alembic stamp head`.
1. Double-check if you are on the right revision `(head)` with `poetry run alembic current` and comparing it
to `poetry run alembic history`.

6. If you plan to keep up to date, you need to set the current alembic revision manually with `poetry run alembic stamp head`.
7. Double-check if you are on the right revision `(head)` with `poetry run alembic current` and comparing it to `poetry run alembic history`.

## Upgrading
If you did Step 7 in the previous section, this is the way you update

1. Stop the bot
2. `git pull`
3. `poetry run alembic upgrade head` to run migrations on your database
4. Start the bot

## Upgrading the Database

If you did Step 7 in the previous section, this is how you upgrade

## Botfather Commands

start - Start the bot
stop - Stop the bot
delete_me - Remove me from the bot. Forever
settings - Open the user settings menu
create - Create a new poll
list - List all active polls and manage them
list_closed - List all closed polls and manage them
notify - Activate notifications in external chats
help - Show the help text
donations - Get me a coffee
1. Stop the bot
1. `git pull`
1. `poetry run alembic upgrade head` to run migrations on your database
1. Start the bot
12 changes: 12 additions & 0 deletions botfather.md
@@ -0,0 +1,12 @@
## Botfather Commands

start - Start the bot
stop - Stop the bot
delete_me - Remove me from the bot. Forever
settings - Open the user settings menu
create - Create a new poll
list - List all active polls and manage them
list_closed - List all closed polls and manage them
notify - Activate notifications in external chats
help - Show the help text
donations - Get me a coffee
14 changes: 14 additions & 0 deletions docker/docker-compose.yml
@@ -0,0 +1,14 @@
version: '3'
services:
database:
image: postgres:latest # use latest official postgres version
ports:
- "5432:5432"
environment:
POSTGRES_DB: pollbot
POSTGRES_USER: pollbot
POSTGRES_PASSWORD: null
volumes:
- database-data:/var/lib/postgresql/data/ # persist data even if container shuts down
volumes:
database-data: # named volumes can be managed easier using docker-compose
52 changes: 45 additions & 7 deletions initdb.py
@@ -1,15 +1,53 @@
#!/bin/env python
"""Drop and create a new database with schema."""
"""Create a new database with schema."""
from contextlib import contextmanager

import typer
from sqlalchemy_utils.functions import database_exists, create_database, drop_database

from sqlalchemy_utils.functions import database_exists, create_database
from pollbot.db import engine, base
from pollbot.models import * # noqa

db_url = engine.url
if not database_exists(db_url):
create_database(db_url)

def initialize_database(exist_ok: bool = False, drop_existing: bool = False):
db_url = engine.url
typer.echo(f"Using database at {db_url}")

if database_exists(db_url):
if drop_existing:
with wrap_echo("Dropping database"):
drop_database(db_url)
elif not exist_ok:
typer.echo(
f"Database already exists, aborting.\n"
f"Use --exist-ok if you are sure the database is uninitialized and contains no data.\n"
f"Use --drop-existing if you want to recreate it.",
err=True,
)
return

with wrap_echo("Creating database"):
create_database(db_url)
pass

with engine.connect() as con:
con.execute("CREATE EXTENSION IF NOT EXISTS pgcrypto;")
with wrap_echo("Installing pgcrypto extension"):
con.execute("CREATE EXTENSION IF NOT EXISTS pgcrypto;")
pass

with wrap_echo("Creating metadata"):
base.metadata.create_all()
pass

typer.echo("Database initialization complete.")


@contextmanager
def wrap_echo(msg: str):
typer.echo(f"{msg}... ", nl=False)
yield
typer.echo("done.")


base.metadata.create_all()
if __name__ == "__main__":
typer.run(initialize_database)
28 changes: 22 additions & 6 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions pollbot/config.py
@@ -1,8 +1,9 @@
"""Config values for pollbot."""
import logging
import os
import sys

import toml
import logging

default_config = {
"telegram": {
Expand All @@ -15,7 +16,7 @@
"max_inline_shares": 20,
},
"database": {
"sql_uri": "postgres://localhost/pollbot",
"sql_uri": "postgres://pollbot:localhost/pollbot",
"connection_count": 20,
"overflow_count": 10,
},
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Expand Up @@ -27,6 +27,7 @@ argparse = "^1.4.0"
[tool.poetry.dev-dependencies]
pytest = "^4.0"
black = { version = "*", allow-prereleases = true }
typer = "^0.2.1"

[build-system]
requires = ["poetry>=0.12"]
Expand Down

0 comments on commit f428cbf

Please sign in to comment.