-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate.py
More file actions
187 lines (159 loc) · 4.83 KB
/
Copy pathmigrate.py
File metadata and controls
187 lines (159 loc) · 4.83 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
'''
Edit ``postgresql.conf``
::
listen_addresses = '*'
Edit ``pg_hba.conf``:
::
host all all 0.0.0.0/0 md5
Restart the server for the changes to `postgres,conf` and `pg_ba.conf`
to be reflected:
::
sudo service postgresql restart
Set up a user for migrating data out of the previous database:
::
CREATE USER migrate WITH PASSWORD 'migrate';
GRANT ALL PRIVILEGES ON DATABASE grocom to migrate;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO migrate;
Make sure we have have defined a port to allow outside access
from the host machine, to the guest VM:
::
Vagrant.configure(VAGRANTFILE_API_VERSION) do |config|
config.ssh.forward_agent = true
config.vm.define "main" do |machine|
machine.vm.box = "trusty64"
# for postgres access
machine.vm.network "forwarded_port", guest: 5432, host: 8032
Make sure new port forwarding is active, so we can connect to the guest VM
database:
::
vagrant reload
'''
from decimal import Decimal
import datetime
import json
import psycopg2
from psycopg2.extras import DictCursor
import pytz
def run(dsn, path, out=print):
"""
Writes a stream of output to a provided
dsn: a String that psycopg2 uses to connect to Postgres
path: a String listing the filename to write to
out: a Callable used to sending logs to
"""
conn = psycopg2.connect(dsn)
cursor = conn.cursor(cursor_factory=DictCursor)
with open(path, 'wb') as fp:
fp.write(b'[\n')
out("Migrating bag type ...")
migrate_bag_type(cursor, fp, out)
out("\ndone.")
out("Migrating collection points ...")
migrate_collection_points(cursor, fp, out)
out("\ndone.")
fp.write(
json.dumps(
{
"model": "sites.site",
"pk": 1,
"fields": {
"domain": 'localhost',
"name": 'BlueWorld',
}
},
indent=4,
).encode('utf8')
)
fp.write(b',\n')
fp.write(
json.dumps(
{
"model": "join.customertag",
"pk": 1,
"fields": {
"tag": "Starter"
}
},
indent=4,
).encode('utf8')
)
fp.write(b'\n')
fp.write(b']')
def migrate_collection_points(cursor, fp, out):
cursor.execute('''
select
id
, name
, "where"
, latitude
, longitude
, available
, display_order
from joinforms_pickuppoint
where group_id=1
''')
for i, row in enumerate(cursor.fetchall()):
out('.', end='')
fp.write(
json.dumps(
{
"model": "join.collectionpoint",
"pk": row['id'],
"fields": {
"name": row['name'].strip(),
"location": row['where'],
"latitude": row['latitude'],
"longitude": row['longitude'],
"active": row['available'],
"display_order": row['display_order'],
}
},
indent=4,
).encode('utf8')
)
fp.write(b',\n')
def migrate_bag_type(cursor, fp, out):
cursor.execute('''
select
id
, name
, active
, "order"
, price
from joinforms_bagtype
where group_id=1
''')
for i, row in enumerate(cursor.fetchall()):
out('.', end='')
fp.write(
json.dumps(
{
"model": "join.bagtype",
"pk": row['id'],
"fields": {
"name": row['name'].strip(),
"active": row['active'],
"display_order": row['order'],
}
},
indent=4,
).encode('utf8')
)
fp.write(b',\n')
fp.write(
json.dumps(
{
"model": "join.bagtypecostchange",
"pk": i,
"fields": {
"bag_type_id": row['id'],
"changed": datetime.datetime.now(tz=pytz.UTC).isoformat(),
"weekly_cost": str(Decimal(row['price'])/Decimal(4.0)),
}
},
indent=4,
).encode('utf8')
)
fp.write(b',\n')
if __name__ == '__main__':
run('dbname=grocom user=migrate password=migrate port=8032 host=localhost', 'initial.json', print)