diff --git a/Dockerfile b/Dockerfile index c2071f0500c4c1540e8ea42ea13a6a56dd5bc6d9..bc8a2db61ff62b55de86513b97193937f19adcb0 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,7 +5,8 @@ COPY . /src WORKDIR /src RUN pip install ipython -RUN pip install -r requirements.txt +#RUN pip install -r dev-requirements.txt +RUN pip install -e . EXPOSE 5000 diff --git a/Makefile b/Makefile index 9325dff900021e674b0e5aa8886fdc317c167895..0cdb44e7e88cee88cfc09b3c39f9e8e51e0ebcbd 100644 --- a/Makefile +++ b/Makefile @@ -15,6 +15,9 @@ build: run: docker run $(PROJECT_DOCKER_RUN_ARGS) $(PROJECT_DOCKER_TAG) +ipython: + docker run -it $(PROJECT_DOCKER_TAG) ipython + test: test-without-build build test-without-build: test-without-lint test-pylint diff --git a/scripts/schema.py b/scripts/schema.py index 86c1afd49e8bbcd2eab7ed4f0a216adffab0d009..e7865e37c73308d2b1df45d7226f36a67ccf78b1 100644 --- a/scripts/schema.py +++ b/scripts/schema.py @@ -1,34 +1,186 @@ -import datetime as dt - -from sqlalchemy import ( - Column, - Integer, - MetaData, - Table, - DateTime, - Index, -) -from sqlalchemy import create_engine -from sqlalchemy.dialects.mysql import SMALLINT - -metadata = MetaData() - -addresses = Table( - 'hive_blocks', metadata, - Column('num', Integer, primary_key=True), - Column('prev', Integer), - Column('txs', SMALLINT(unsigned=True), default=0), - Column('created_at', DateTime), - Index('hive_blocks_ux1', 'prev', unique=True), - # ForeignKeyConstraint(name='hive_blocks_fk1', columns='prev', refcolumns='hive_blocks.num'), - mysql_engine='InnoDB', - mysql_charset='utf8mb4', +import logging + +import sqlalchemy as sa +from sqlalchemy.dialects.mysql import ( + CHAR, SMALLINT, TINYINT, + TINYTEXT, DOUBLE, ) -if __name__ == '__main__': - engine = create_engine('sqlite:///hive.sqlite', echo=True) - metadata.create_all(engine) - engine.execute( - addresses.insert([addresses.c.num, addresses.c.prev, addresses.c.created_at]). - values(num=0, prev=None, created_at=dt.datetime.fromtimestamp(0))) - # INSERT INTO hive_blocks (num, prev, created_at) VALUES (0, NULL, "1970-01-01T00:00:00"); +connection_url = 'mysql://root:root_password@localhost:3306/testdb' + +logging.basicConfig() +logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO) + +engine = sa.create_engine(connection_url) +conn = engine.connect() +metadata = sa.MetaData() + +hive_blocks = sa.Table('hive_blocks', metadata, + sa.Column('num', sa.Integer, primary_key=True), + sa.Column('prev', sa.Integer), + sa.Column('txs', SMALLINT(unsigned=True), server_default='0', nullable=False), + sa.Column('created_at', sa.DateTime, nullable=False), + sa.UniqueConstraint('prev', name='hive_blocks_ux1'), + sa.ForeignKeyConstraint(['prev'], ['hive_blocks.num'], name='hive_blocks_fk1'), + mysql_engine='InnoDB', + mysql_default_charset='utf8mb4') + +hive_accounts = sa.Table('hive_accounts', metadata, + sa.Column('id', sa.Integer, primary_key=True), + sa.Column('name', sa.String(16), nullable=False), + sa.Column('created_at', sa.DateTime, nullable=False), + sa.UniqueConstraint('name', name='hive_accounts_ux1'), + mysql_engine='InnoDB', + mysql_default_charset='utf8mb4') + +# The column 'permlink' is CHAR(190) instead of CHAR(255) since the latter +# will give error: (1071, 'Specified key was too long; max key length is 767 bytes') +hive_posts = sa.Table('hive_posts', metadata, + sa.Column('id', sa.Integer, primary_key=True), + sa.Column('parent_id', sa.Integer), + sa.Column('author', CHAR(16), nullable=False), + sa.Column('permlink', CHAR(190), nullable=False), + sa.Column('community', CHAR(16)), + sa.Column('category', CHAR(16), nullable=False), + sa.Column('depth', SMALLINT(unsigned=True), nullable=False), + sa.Column('created_at', sa.DateTime, nullable=False), + sa.Column('is_deleted', TINYINT(1), nullable=False, server_default='0'), + sa.Column('is_pinned', TINYINT(1), nullable=False, server_default='0'), + sa.Column('is_muted', TINYINT(1), nullable=False, server_default='0'), + sa.ForeignKeyConstraint(['author'], ['hive_accounts.name'], name='hive_posts_fk1'), + sa.ForeignKeyConstraint(['community'], ['hive_accounts.name'], name='hive_posts_fk2'), + sa.ForeignKeyConstraint(['parent_id'], ['hive_posts.id'], name='hive_posts_fk3'), + sa.UniqueConstraint('author', 'permlink', name='hive_posts_ux1'), + sa.Index('hive_posts_ix1', 'parent_id'), + sa.Index('hive_posts_ix2', 'is_deleted'), + mysql_engine='InnoDB', + mysql_default_charset='utf8mb4') + +hive_follows = sa.Table('hive_follows', metadata, + sa.Column('follower', CHAR(16), nullable=False), + sa.Column('following', CHAR(16), nullable=False), + sa.Column('created_at', sa.DateTime, nullable=False), + sa.ForeignKeyConstraint(['follower'], ['hive_accounts.name'], name='hive_follows_fk1'), + sa.ForeignKeyConstraint(['following'], ['hive_accounts.name'], name='hive_follows_fk2'), + sa.UniqueConstraint('follower', 'following', name='hive_follows_ux1'), + mysql_engine='InnoDB', + mysql_default_charset='utf8mb4') + +hive_reblogs = sa.Table('hive_reblogs', metadata, + sa.Column('account', CHAR(16), nullable=False), + sa.Column('post_id', sa.Integer, nullable=False), + sa.Column('created_at', sa.DateTime, nullable=False), + sa.ForeignKeyConstraint(['account'], ['hive_accounts.name'], name='hive_reblogs_fk1'), + sa.ForeignKeyConstraint(['post_id'], ['hive_posts.id'], name='hive_reblogs_fk2'), + sa.UniqueConstraint('account', 'post_id', name='hive_reblogs_ux1'), + sa.Index('hive_reblogs_ix1', 'post_id', 'account', 'created_at'), + mysql_engine='InnoDB', + mysql_default_charset='utf8mb4') + +hive_communities = sa.Table('hive_communities', metadata, + sa.Column('name', CHAR(16), primary_key=True), + sa.Column('title', sa.String(32), nullable=False), + sa.Column('about', sa.String(255), nullable=False, server_default=''), + sa.Column('description', sa.String(5000), nullable=False, server_default=''), + sa.Column('lang', CHAR(2), nullable=False, server_default='en'), + sa.Column('settings', TINYTEXT, nullable=False, server_default=''), + sa.Column('type_id', TINYINT(1), nullable=False, server_default='0'), + sa.Column('is_nsfw', TINYINT(1), nullable=False, server_default='0'), + sa.Column('created_at', sa.DateTime, nullable=False), + sa.ForeignKeyConstraint(['name'], ['hive_accounts.name'], name='hive_communities_fk1'), + mysql_engine='InnoDB', + mysql_default_charset='utf8mb4') + +hive_members = sa.Table('hive_members', metadata, + sa.Column('community', CHAR(16), nullable=False), + sa.Column('account', CHAR(16), nullable=False), + sa.Column('is_admin', TINYINT(1), nullable=False), + sa.Column('is_mod', TINYINT(1), nullable=False), + sa.Column('is_approved', TINYINT(1), nullable=False), + sa.Column('is_muted', TINYINT(1), nullable=False), + sa.Column('title', sa.String(255), nullable=False, server_default=''), + sa.ForeignKeyConstraint(['community'], ['hive_communities.name'], name='hive_members_fk1'), + sa.ForeignKeyConstraint(['account'], ['hive_accounts.name'], name='hive_members_fk2'), + sa.UniqueConstraint('community', 'account', name='hive_members_ux1'), + mysql_engine='InnoDB', + mysql_default_charset='utf8mb4') + +hive_flags = sa.Table('hive_flags', metadata, + sa.Column('account', CHAR(16), nullable=False), + sa.Column('post_id', sa.Integer, nullable=False), + sa.Column('created_at', sa.DateTime, nullable=False), + sa.Column('notes', sa.String(255), nullable=False), + sa.ForeignKeyConstraint(['account'], ['hive_accounts.name'], name='hive_flags_fk1'), + sa.ForeignKeyConstraint(['post_id'], ['hive_posts.id'], name='hive_flags_fk2'), + sa.UniqueConstraint('account', 'post_id', name='hive_flags_ux1'), + mysql_engine='InnoDB', + mysql_default_charset='utf8mb4') + +hive_modlog = sa.Table('hive_modlog', metadata, + sa.Column('id', sa.Integer, primary_key=True), + sa.Column('community', CHAR(16), nullable=False), + sa.Column('account', CHAR(16), nullable=False), + sa.Column('action', sa.String(32), nullable=False), + sa.Column('params', sa.String(1000), nullable=False), + sa.Column('created_at', sa.DateTime, nullable=False), + sa.ForeignKeyConstraint(['community'], ['hive_communities.name'], name='hive_modlog_fk1'), + sa.ForeignKeyConstraint(['account'], ['hive_accounts.name'], name='hive_modlog_fk2'), + sa.Index('hive_modlog_ix1', 'community', 'created_at'), + mysql_engine='InnoDB', + mysql_default_charset='utf8mb4') + +hive_posts_cache = sa.Table('hive_posts_cache', metadata, + sa.Column('post_id', sa.Integer, primary_key=True), + sa.Column('title', sa.String(255), nullable=False), + sa.Column('preview', sa.String(1024), nullable=False), + sa.Column('img_url', sa.String(1024), nullable=False), + sa.Column('payout', sa.types.DECIMAL(10, 3), nullable=False), + sa.Column('promoted', sa.types.DECIMAL(10, 3), nullable=False), + sa.Column('created_at', sa.DateTime, nullable=False), + sa.Column('payout_at', sa.DateTime, nullable=False), + sa.Column('updated_at', sa.DateTime, nullable=False), + sa.Column('is_nsfw', TINYINT(1), nullable=False, server_default='0'), + sa.Column('children', sa.Integer, nullable=False, server_default='0'), + sa.Column('rshares', sa.BigInteger, nullable=False), + sa.Column('sc_trend', DOUBLE, nullable=False), + sa.Column('sc_hot', DOUBLE, nullable=False), + sa.Column('body', sa.Text), + sa.Column('votes', sa.Text), + sa.Column('json', sa.Text), + sa.ForeignKeyConstraint(['post_id'], ['hive_posts.id'], name='hive_posts_cache_fk1'), + sa.Index('hive_posts_cache_ix1', 'payout'), + sa.Index('hive_posts_cache_ix2', 'promoted'), + sa.Index('hive_posts_cache_ix3', 'payout_at'), + sa.Index('hive_posts_cache_ix4', 'updated_at'), + sa.Index('hive_posts_cache_ix5', 'rshares'), + mysql_engine='InnoDB', + mysql_default_charset='utf8mb4') + +hive_accounts_cache = sa.Table('hive_accounts_cache', metadata, + sa.Column('account', CHAR(16), primary_key=True), + sa.Column('reputation', sa.Float, nullable=False, server_default='25'), + sa.Column('name', sa.String(20)), + sa.Column('about', sa.String(160)), + sa.Column('location', sa.String(30)), + sa.Column('url', sa.String(100)), + sa.Column('img_url', sa.String(1024)), + sa.ForeignKeyConstraint(['account'], ['hive_accounts.name'], + name='hive_accounts_cache_fk1'), + mysql_engine='InnoDB', + mysql_default_charset='utf8mb4') + +metadata.drop_all(engine) +metadata.create_all(engine) + +# Insert hive_blocks data +insert = hive_blocks.insert().values(num=0, prev=None, created_at='1970-01-01T00:00:00') +conn.execute(insert) + +# Insert hive_accounts data +insert = hive_accounts.insert() +conn.execute(insert, [ + {'name': 'miners', 'created_at': '1970-01-01T00:00:00'}, + {'name': 'null', 'created_at': '1970-01-01T00:00:00'}, + {'name': 'temp', 'created_at': '1970-01-01T00:00:00'}, + {'name': 'initminer', 'created_at': '1970-01-01T00:00:00'} +]) diff --git a/scripts/test.py b/scripts/test.py deleted file mode 100644 index 06015154d59096d51f4d26e5e83e5881e551fb7d..0000000000000000000000000000000000000000 --- a/scripts/test.py +++ /dev/null @@ -1,33 +0,0 @@ -from pprint import pprint -from sqlalchemy import ( - Column, - Integer, - MetaData, - String, - Table, - create_engine, -) -from sqlalchemy import select -from steem.account import Account - -engine = create_engine('sqlite:///hive.sqlite', echo=True) - -metadata = MetaData() - -authors = Table( - 'authors', - metadata, - Column('id', Integer, primary_key=True, autoincrement=True), - Column('name', String(16)), - Column('balances', String), -) - -metadata.create_all(engine) - -if __name__ == '__main__': - conn = engine.connect() - acc = Account('furion') - r = authors.insert().values(name=acc.name, balances='100') - engine.execute(r) - q = select([authors]) - pprint(conn.execute(q).fetchall()) diff --git a/setup.py b/setup.py index a91f0ae8b4e09adcd121c78fcf5718a42c924ea0..3c8f2a7e2a5d7362e1ab2f6b44b2ab8b89542db6 100644 --- a/setup.py +++ b/setup.py @@ -25,10 +25,12 @@ setup( 'pytest-console-scripts'], install_requires=[ - 'steem==0.18.1', + 'steem', 'maya', 'toolz', 'funcy', + 'sqlalchemy', + 'mysqlclient', ], entry_points={ 'console_scripts': [