forked from xeroc/stakemachine
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add new columns "virtual" and "custom" to Orders table. "Virtual" is for indicating virtual order, "Custom" is for adding any custom data in string format, may be used by any strategy to add additional info about order. This commit also introduces database migrations mechanism via alembic tool. Seems like it's the best solution for sqlalchemy to handle schema updates.
- Loading branch information
Showing
8 changed files
with
235 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
Dexbot database migrations are handled by alembic. See https://alembic.sqlalchemy.org/ | ||
|
||
## Create new migration | ||
|
||
``` | ||
alembic revision -m "Short summary of changes" | ||
``` | ||
|
||
Next, modify the migration script in dexbot/migrations/versions/ | ||
|
||
Migration will be applied automatically on next run of dexbot, see `run_migrations()` in dexbot/storage.py | ||
|
||
Don't forget to change table definitions in dexbot/storage.py. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
from sqlalchemy import engine_from_config | ||
from sqlalchemy import pool | ||
|
||
from alembic import context | ||
|
||
# this is the Alembic Config object, which provides | ||
# access to the values within the .ini file in use. | ||
config = context.config | ||
|
||
# Interpret the config file for Python logging. | ||
# This line sets up loggers basically. | ||
# fileConfig(config.config_file_name) | ||
|
||
# add your model's MetaData object here | ||
# for 'autogenerate' support | ||
# from myapp import mymodel | ||
# target_metadata = mymodel.Base.metadata | ||
target_metadata = None | ||
|
||
# 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") | ||
# ... etc. | ||
|
||
|
||
def run_migrations_offline(): | ||
"""Run migrations in 'offline' mode. | ||
This configures the context with just a URL | ||
and not an Engine, though an Engine is acceptable | ||
here as well. By skipping the Engine creation | ||
we don't even need a DBAPI to be available. | ||
Calls to context.execute() here emit the given string to the | ||
script output. | ||
""" | ||
url = config.get_main_option("sqlalchemy.url") | ||
context.configure( | ||
url=url, target_metadata=target_metadata, literal_binds=True | ||
) | ||
|
||
with context.begin_transaction(): | ||
context.run_migrations() | ||
|
||
|
||
def run_migrations_online(): | ||
"""Run migrations in 'online' mode. | ||
In this scenario we need to create an Engine | ||
and associate a connection with the context. | ||
""" | ||
connectable = engine_from_config( | ||
config.get_section(config.config_ini_section), | ||
prefix="sqlalchemy.", | ||
poolclass=pool.NullPool, | ||
) | ||
|
||
with connectable.connect() as connection: | ||
context.configure( | ||
connection=connection, target_metadata=target_metadata | ||
) | ||
|
||
with context.begin_transaction(): | ||
context.run_migrations() | ||
|
||
|
||
if context.is_offline_mode(): | ||
run_migrations_offline() | ||
else: | ||
run_migrations_online() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
"""${message} | ||
|
||
Revision ID: ${up_revision} | ||
Revises: ${down_revision | comma,n} | ||
Create Date: ${create_date} | ||
|
||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
${imports if imports else ""} | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = ${repr(up_revision)} | ||
down_revision = ${repr(down_revision)} | ||
branch_labels = ${repr(branch_labels)} | ||
depends_on = ${repr(depends_on)} | ||
|
||
|
||
def upgrade(): | ||
${upgrades if upgrades else "pass"} | ||
|
||
|
||
def downgrade(): | ||
${downgrades if downgrades else "pass"} |
26 changes: 26 additions & 0 deletions
26
dexbot/migrations/versions/d1e6672520b2_extend_orders_table.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
"""extend orders table | ||
Revision ID: d1e6672520b2 | ||
Revises: | ||
Create Date: 2019-07-29 17:38:09.136485 | ||
""" | ||
from alembic import op | ||
import sqlalchemy as sa | ||
|
||
|
||
# revision identifiers, used by Alembic. | ||
revision = 'd1e6672520b2' | ||
down_revision = None | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
op.add_column('orders', sa.Column('virtual', sa.Boolean(create_constraint=False))) | ||
op.add_column('orders', sa.Column('custom', sa.String)) | ||
|
||
|
||
def downgrade(): | ||
op.drop_column('orders', 'virtual') | ||
op.drop_column('orders', 'custom') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,4 @@ websocket-client==0.56.0 | |
sdnotify==0.3.2 | ||
sqlalchemy==1.3.0 | ||
click==7.0 | ||
alembic==1.0.11 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import os | ||
import pytest | ||
import tempfile | ||
import logging | ||
|
||
from sqlalchemy import create_engine, Column, String, Integer, Float | ||
from sqlalchemy.ext.declarative import declarative_base | ||
from sqlalchemy.orm import sessionmaker | ||
|
||
log = logging.getLogger("dexbot") | ||
log.setLevel(logging.DEBUG) | ||
|
||
Base = declarative_base() | ||
|
||
# Classes are represent initial table structure | ||
|
||
|
||
class Config(Base): | ||
__tablename__ = 'config' | ||
|
||
id = Column(Integer, primary_key=True) | ||
category = Column(String) | ||
key = Column(String) | ||
value = Column(String) | ||
|
||
|
||
class Orders(Base): | ||
__tablename__ = 'orders' | ||
|
||
id = Column(Integer, primary_key=True) | ||
worker = Column(String) | ||
order_id = Column(String) | ||
order = Column(String) | ||
|
||
|
||
class Balances(Base): | ||
__tablename__ = 'balances' | ||
|
||
id = Column(Integer, primary_key=True) | ||
account = Column(String) | ||
worker = Column(String) | ||
base_total = Column(Float) | ||
base_symbol = Column(String) | ||
quote_total = Column(Float) | ||
quote_symbol = Column(String) | ||
center_price = Column(Float) | ||
timestamp = Column(Integer) | ||
|
||
|
||
@pytest.fixture | ||
def initial_db(): | ||
|
||
_, db_file = tempfile.mkstemp() # noqa: F811 | ||
engine = create_engine('sqlite:///{}'.format(db_file), echo=False) | ||
Session = sessionmaker(bind=engine) | ||
session = Session() | ||
Base.metadata.create_all(engine) | ||
session.commit() | ||
log.debug('Prepared db on {}'.format(db_file)) | ||
|
||
yield db_file | ||
os.unlink(db_file) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from dexbot.storage import DatabaseWorker | ||
|
||
|
||
def test_apply_migrations(initial_db): | ||
DatabaseWorker.run_migrations('dexbot/migrations', 'sqlite:///{}'.format(initial_db)) |