forked from getsentry/self-hosted
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinitialize.py
58 lines (49 loc) · 1.58 KB
/
initialize.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
import re
import sys
import psycopg2 as Database
from django.conf import settings
from django.db import connection
from django.db.utils import OperationalError
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
def create_database(connection_params):
params = connection_params.copy()
database = params.pop('database')
params['database'] = 'postgres'
conn = Database.connect(**params)
conn.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cursor = conn.cursor()
cursor.execute('CREATE DATABASE ' + database)
cursor.close()
conn.close()
if __name__ == '__main__':
params = connection.get_connection_params()
try:
connection.ensure_connection()
except OperationalError as error:
if 'database "' + params['database'] + '" does not exist' in error.message:
print('Creating database "' + params['database'] + '"')
create_database(params)
else:
raise error
# skipping if already some tables exists
if connection.introspection.table_names():
sys.exit(0)
print('Loading inital schema...')
sql_file = open('schema.sql', 'U')
try:
output = []
statements = re.compile(r";[ \t]*$", re.M)
for statement in statements.split(sql_file.read().decode(settings.FILE_CHARSET)):
# Remove any comments from the file
statement = re.sub(ur"--.*([\n\Z]|$)", "", statement)
if statement.strip():
output.append(statement + u";")
sql = u'\n'.join(output).encode('utf-8')
finally:
sql_file.close()
cursor = connection.cursor()
try:
cursor.execute(sql)
connection.commit()
finally:
cursor.close()