Skip to content

Instantly share code, notes, and snippets.

@oucb
Forked from zhoubaozhou/partition.py
Created August 30, 2018 01:11
Show Gist options
  • Save oucb/1b2eca2c4fb1ef333576b5aa10664bc0 to your computer and use it in GitHub Desktop.
Save oucb/1b2eca2c4fb1ef333576b5aa10664bc0 to your computer and use it in GitHub Desktop.
sqlalchemy分表测试代码
#-!-coding:utf-8-!-
# schema
# revision identifiers, used by Alembic.
revision = '507c3af35680'
down_revision = None
from alembic import op
import sqlalchemy as sa
def upgrade():
for table_id in range(0, 128):
op.create_table(
'test_%03d' % table_id,
sa.Column('key_sign', sa.Integer, primary_key=True, autoincrement=False),
sa.Column('db_id', sa.SmallInteger, nullable=False),
)
def downgrade():
for table_id in range(0, 128):
op.drop_table('test_%03d' % table_id)
# 测试代码
import random
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('sqlite:///test.sqlite', pool_recycle=3600, strategy='threadlocal')
Base = declarative_base()
Base.metadata.bind = engine
Base.metadata.reflect(engine)
def table_name(key_sign):
table_id = key_sign % 128
return 'test_%03d' % table_id
conn = engine.connect()
for i in range(100000):
key_sign = random.randint(0, 0x7fffffffffffffff)
ins = Base.metadata.tables[table_name(key_sign)].insert().values(
key_sign=key_sign,
db_id=random.randint(0,255)
)
result = conn.execute(ins)
print result.inserted_primary_key
result.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment