forked from arXiv/arxiv-browse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
populate_test_database.py
46 lines (36 loc) · 1.43 KB
/
populate_test_database.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
"""Helper script to initialize the browse database and add a few rows."""
import click
from browse.factory import create_web_app
from browse.services.database import models
import glob
from tests import execute_sql_files
from typing import List
app = create_web_app()
app.app_context().push()
@app.cli.command()
@click.option('--drop_and_create', '-c', is_flag=True,
help='Drop and recreate tables from models.')
def populate_test_database(drop_and_create: bool) -> None:
"""Initialize the browse tables."""
if drop_and_create:
models.db.drop_all()
models.db.create_all()
# Member institution data
models.db.session.add(
models.MemberInstitution(
id=1, name='Localhost University', label='Localhost University'),
)
models.db.session.add(models.MemberInstitutionIP(
id=1, sid=1, start=2130706433, end=2130706433, exclude=0))
# Intentionally add another insitution for the same loopback IP as above
models.db.session.add(
models.MemberInstitution(
id=2, name='Loopback University', label='Loopback University'),
)
models.db.session.add(models.MemberInstitutionIP(
id=2, sid=2, start=2130706433, end=2130706433, exclude=0))
models.db.session.commit()
sql_files: List[str] = glob.glob('./tests/data/db/sql/*.sql')
execute_sql_files(sql_files, models.db.engine)
if __name__ == '__main__':
populate_test_database()