summaryrefslogtreecommitdiffstats
path: root/aurweb/initdb.py
blob: 91777f7eb3c8867506d1eef1b24e19ba8635a9f6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import aurweb.db
import aurweb.schema

import alembic.command
import alembic.config
import argparse
import sqlalchemy


def feed_initial_data(conn):
    conn.execute(aurweb.schema.AccountTypes.insert(), [
        {'ID': 1, 'AccountType': 'User'},
        {'ID': 2, 'AccountType': 'Trusted User'},
        {'ID': 3, 'AccountType': 'Developer'},
        {'ID': 4, 'AccountType': 'Trusted User & Developer'},
    ])
    conn.execute(aurweb.schema.DependencyTypes.insert(), [
        {'ID': 1, 'Name': 'depends'},
        {'ID': 2, 'Name': 'makedepends'},
        {'ID': 3, 'Name': 'checkdepends'},
        {'ID': 4, 'Name': 'optdepends'},
    ])
    conn.execute(aurweb.schema.RelationTypes.insert(), [
        {'ID': 1, 'Name': 'conflicts'},
        {'ID': 2, 'Name': 'provides'},
        {'ID': 3, 'Name': 'replaces'},
    ])
    conn.execute(aurweb.schema.RequestTypes.insert(), [
        {'ID': 1, 'Name': 'deletion'},
        {'ID': 2, 'Name': 'orphan'},
        {'ID': 3, 'Name': 'merge'},
    ])


def run(args):
    # Ensure Alembic is fine before we do the real work, in order not to fail at
    # the last step and leave the database in an inconsistent state. The
    # configuration is loaded lazily, so we query it to force its loading.
    if args.use_alembic:
        alembic_config = alembic.config.Config('alembic.ini')
        alembic_config.get_main_option('script_location')

    engine = sqlalchemy.create_engine(aurweb.db.get_sqlalchemy_url(),
                                      echo=(args.verbose >= 1))
    aurweb.schema.metadata.create_all(engine)
    feed_initial_data(engine.connect())

    if args.use_alembic:
        alembic.command.stamp(alembic_config, 'head')


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        prog='python -m aurweb.initdb',
        description='Initialize the aurweb database.')
    parser.add_argument('-v', '--verbose', action='count', default=0,
                        help='increase verbosity')
    parser.add_argument('--no-alembic',
                        help='disable Alembic migrations support',
                        dest='use_alembic', action='store_false')
    args = parser.parse_args()
    run(args)