<commit_before>from uber.common import *

verbose = dry_run = False

class Style:
    def __getattr__(self, name):
        return lambda text: text

with open(join(MODULE_ROOT, 'models.py')) as f:
    text = f.read()

classes = sorted(all_models(), key=lambda c: text.index('class {}('.format(c.__name__)))

def drop_and_create():
    with closing(connection.cursor()) as cursor:
        for model in reversed(classes):
            sql = 'DROP TABLE IF EXISTS "{}";'.format(model.__name__)
            if verbose:
                print(sql)
            if not dry_run:
                cursor.execute(sql)

        for model in classes:
            sql = connection.creation.sql_create_model(model, Style(), classes)[0][0]
            if verbose:
                print(sql)
            if not dry_run:
                cursor.execute(sql)

def insert_admin():
    attendee = Attendee.objects.create(
        placeholder = True,
        first_name  = 'Test',
        last_name   = 'Developer',
        email       = '<EMAIL>',
        badge_type  = STAFF_BADGE,
        ribbon      = DEPT_HEAD_RIBBON
    )
    AdminAccount.objects.create(
        attendee = attendee,
        access   = ','.join(str(level) for level, name in ACCESS_OPTS),
        hashed   = bcrypt.hashpw('magfest', bcrypt.gensalt())
    )

if __name__ == '__main__':
    verbose = '--quiet' not in sys.argv
    dry_run = '--dry-run' in sys.argv
    if any(not arg.startswith('-') for arg in sys.argv[1:]):
        classes = [c for c in classes if c.__name__ in sys.argv[1:]]

    drop_and_create()
    if not dry_run:
        insert_admin()<commit_msg>init_db will by default not erase existing database unless you do --force-init<commit_after>from uber.common import *

verbose = dry_run = False

class Style:
    def __getattr__(self, name):
        return lambda text: text

with open(join(MODULE_ROOT, 'models.py')) as f:
    text = f.read()

classes = sorted(all_models(), key=lambda c: text.index('class {}('.format(c.__name__)))

def does_attendee_table_already_exist(cursor):
    cursor.execute("select exists(select * from information_schema.tables where table_name='Attendee')", ('mytable',))
    return cursor.fetchone()[0]

def drop_and_create(cursor):
    for model in reversed(classes):
        sql = 'DROP TABLE IF EXISTS "{}";'.format(model.__name__)
        if verbose:
            print(sql)
        if not dry_run:
            cursor.execute(sql)

    for model in classes:
        sql = connection.creation.sql_create_model(model, Style(), classes)[0][0]
        if verbose:
            print(sql)
        if not dry_run:
            cursor.execute(sql)

def insert_admin():
    attendee = Attendee.objects.create(
        placeholder = True,
        first_name  = 'Test',
        last_name   = 'Developer',
        email       = '<EMAIL>',
        badge_type  = STAFF_BADGE,
        ribbon      = DEPT_HEAD_RIBBON
    )
    AdminAccount.objects.create(
        attendee = attendee,
        access   = ','.join(str(level) for level, name in ACCESS_OPTS),
        hashed   = bcrypt.hashpw('magfest', bcrypt.gensalt())
    )

def init_db():
    with closing(connection.cursor()) as cursor:
        if (does_attendee_table_already_exist(cursor)):
            if not force_init:
                print("ERROR: data already exists in this database, not going to init.\n"
                      "Use --force-init to override. WARNING: YOU WILL LOSE ALL DATA")
                return
            else:
                print("WARNING: --force-init specified, deleting everything the database.")

        drop_and_create(cursor)

    if not dry_run:
        insert_admin()

if __name__ == '__main__':
    verbose = '--quiet' not in sys.argv
    dry_run = '--dry-run' in sys.argv
    force_init = '--force-init' in sys.argv

    if any(not arg.startswith('-') for arg in sys.argv[1:]):
        classes = [c for c in classes if c.__name__ in sys.argv[1:]]

    init_db()
{'fraction_non_alphanumeric': 0.11790060851926977, 'fraction_numerical': 0.002281947261663286, 'mean_word_length': 2.343220338983051, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1017562', 'n_tokens_mistral': 1250, 'n_tokens_neox': 1212, 'n_words': 321}