def initial_database():
    """
    初始化数据库,
    重建旧表
    创建表
    Returns:

    """
    # TODO: 重复代码抽象

    from yiyun.helpers import find_subclasses
    from yiyun.models import BaseModel, Sport, User, Team, Activity

    # drop old database and create new;
    test_settings = reject_settings()
    test_db = test_settings['db_name']

    raw_sql = ("drop database {test_db};"
               "create database {test_db};"
               "use {test_db};")

    # create new tables
    BaseModel._meta.database.execute_sql(raw_sql.format(test_db=test_db))

    models = find_subclasses(BaseModel)

    if not Sport.table_exists():
        Sport.create_table()

    if not User.table_exists():
        User.create_table()

    if not Team.table_exists():
        Team.create_table()

    if not Activity.table_exists():
        Activity.create_table()

    for model in models:
        if model._meta.db_table.startswith("__"):
            logging.debug(("table skip: " + model._meta.db_table))
        elif model.table_exists():
            logging.debug(('table exist: ' + model._meta.db_table))
        else:
            model.create_table()
            logging.debug(('table created: ' + model._meta.db_table))

    logging.debug('create all [ok]')
def main():

    settings = setting_from_object(local_settings)
    if settings.get('debug', False):
        options.logging = "debug"

    tornado.options.parse_command_line()

    if options.debug:
        settings['debug'] = True

    if options.cmd == 'createall':
        """Create all database tables"""

        create_app(settings)

        if not Sport.table_exists():
            Sport.create_table()

        if not User.table_exists():
            User.create_table()

        if not Team.table_exists():
            Team.create_table()

        if not TeamMemberGroup.table_exists():
            TeamMemberGroup.create_table()

        if not TeamMember.table_exists():
            TeamMember.create_table()

        if not Activity.table_exists():
            Activity.create_table()

        if not Admin.table_exists():
            Admin.create_table()

        if not Match.table_exists():
            Match.create_table()

        if not MatchStatus.table_exists():
            MatchStatus.create_table()

        models = find_subclasses(BaseModel)
        for model in models:
            if model._meta.db_table.startswith("__"):
                print(("table skip: " + model._meta.db_table))
            elif model.table_exists():
                print(('table exist: ' + model._meta.db_table))
            else:
                model.create_table()
                print(('table created: ' + model._meta.db_table))

        print('create all [ok]')

    elif options.cmd == 'createadmin':
        app = create_app(settings)
        Admin.create(username="******",
                     password=Admin.create_password("admin"),
                     mobile="17088888888",
                     email="*****@*****.**",
                     name="Admin",
                     is_superadmin=True,
                     state=1)

    elif options.cmd == 'createclient':
        app = create_app(settings)
        Client.create(name="ios",
                      key=create_token(32),
                      secret=create_token(32))

    elif options.cmd == 'run_as_wsgi':
        logging.info('server started. port %s' % options.port)

        import gevent.wsgi

        app = create_app(settings)

        # 转换成wsgi实例
        wsgi_app = tornado.wsgi.WSGIAdapter(app)

        http_server = gevent.wsgi.WSGIServer(('', options.port), wsgi_app)
        http_server.serve_forever()

    elif options.cmd == 'runserver':

        tornado.platform.asyncio.AsyncIOMainLoop().install()
        ioloop = asyncio.get_event_loop()

        app = create_app(settings)
        app.listen(options.port, xheaders=True)

        print("running...")
        ioloop.run_forever()
    elif options.cmd == "fix_notify":
        from fix_script.fix_match_start_nofity import fix_match_start_notify
        fix_match_start_notify()