-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdb_setup.py
More file actions
76 lines (58 loc) · 2.49 KB
/
db_setup.py
File metadata and controls
76 lines (58 loc) · 2.49 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# Creates a simple postgres DB to store the GH data in
#
#!/usr/bin/python
# -*- coding: utf-8 -*-
import psycopg2
import sys
import yaml
con = None
# Parse the yaml config file
config_file = open('config.yaml', 'r')
config = yaml.load(config_file.read())
try:
con = psycopg2.connect(database=config['database'],
user=config['database_user'],
password=config['database_password'])
cur = con.cursor()
cur.execute("""CREATE TABLE repositories(id SERIAL PRIMARY KEY,
gh_id INT UNIQUE,
owner_login VARCHAR,
name VARCHAR,
full_name VARCHAR,
description VARCHAR,
private BOOLEAN,
fork BOOLEAN,
api_url VARCHAR,
html_url VARCHAR)""")
cur.execute("""CREATE TABLE repository_licenses(id SERIAL PRIMARY KEY,
repository_id INT REFERENCES
repositories (gh_id),
type VARCHAR,
encoding VARCHAR,
api_url VARCHAR,
html_url VARCHAR,
size INT,
name VARCHAR,
path VARCHAR,
content TEXT,
sha VARCHAR)""")
cur.execute("""CREATE TABLE license_metadata(id SERIAL PRIMARY KEY,
license_id INT REFERENCES
repository_licenses (id),
is_primary BOOLEAN DEFAULT FALSE,
license_abbr VARCHAR,
UNIQUE(license_id, license_abbr))""")
cur.execute("""CREATE TABLE repository_license_abbr(repository_id INT REFERENCES
repositories(id),
license_abbr_id INT REFERENCES
licenses(id),
UNIQUE(repository_id, license_abbr_id))""")
con.commit()
except psycopg2.DatabaseError, e:
if con:
con.rollback()
print 'Error %s' % e
sys.exit(1)
finally:
if con:
con.close()