forked from fedora-infra/anitya
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createdb.py
60 lines (45 loc) · 1.82 KB
/
createdb.py
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
from argparse import ArgumentParser
from pathlib import Path
from alembic import command
from alembic.config import Config
from social_flask_sqlalchemy import models as social_models
from anitya.app import create
from anitya.config import load
from anitya.db import Base, Session
parser = ArgumentParser()
script_dir = Path(__file__).parent.absolute()
parser.add_argument("--debug", action="store_true", default=False, dest="verbose")
parser.add_argument("--alembic-config", default=None)
parser.add_argument(
"--db-uri",
default=None,
help="This will override the URLs specified in alembic and anitya",
)
args = parser.parse_args()
alembic_config = args.alembic_config
anitya_config = load()
anitya_config["SQL_DEBUG"] = args.verbose
if args.db_uri:
anitya_config["DB_URL"] = args.db_uri
# An app object is required for social_auth tables to be created properly
anitya_app = create(config=anitya_config)
engine = Session.get_bind()
Base.metadata.create_all(engine)
# This call depends on app.create calling init_social which defines
# necessary column information in the social_auth_usersocialauth table
social_models.PSABase.metadata.create_all(engine)
# Set the alembic_version based on the current migrations available.
# This presupposes the models haven't changed outside of a migration.
#
# Default to the side-by-side alembic.ini.
if alembic_config is None:
alembic_config = os.path.join(script_dir, "alembic.ini")
if args.verbose and os.path.isfile(alembic_config):
print("No alembic config specified, defaulting to: {}".format(alembic_config))
if alembic_config and os.path.isfile(alembic_config):
alembic_cfg = Config(alembic_config)
alembic_cfg.set_main_option("sqlalchemy.url", anitya_config["DB_URL"])
command.stamp(alembic_cfg, "head")