@@ -29,24 +29,131 @@ def _createCmdTuple(cursor, keys, templ, attribs):
29
29
"""Use the cursor to mogrify a tuple of data.
30
30
The passed data in `attribs` is augmented with default data (NULLs) and the
31
31
order of data in the tuple is the same as in the list of `keys`. The
32
- `cursor` is used toe mogrify the data and the `templ` is the template used
32
+ `cursor` is used to mogrify the data and the `templ` is the template used
33
33
for the mogrification.
34
34
"""
35
35
defs = _makeDefValues (keys )
36
36
defs .update (attribs )
37
37
return cursor .mogrify (templ , defs )
38
38
39
+ def _getTableKeys (table ):
40
+ """Return an array of the keys for a given table"""
41
+ keys = None
42
+ if table == 'Users' :
43
+ keys = [
44
+ 'Id'
45
+ , 'Reputation'
46
+ , 'CreationDate'
47
+ , 'DisplayName'
48
+ , 'LastAccessDate'
49
+ , 'WebsiteUrl'
50
+ , 'Location'
51
+ , 'AboutMe'
52
+ , 'Views'
53
+ , 'UpVotes'
54
+ , 'DownVotes'
55
+ , 'ProfileImageUrl'
56
+ , 'Age'
57
+ , 'AccountId'
58
+ ]
59
+ elif table == 'Badges' :
60
+ keys = [
61
+ 'Id'
62
+ , 'UserId'
63
+ , 'Name'
64
+ , 'Date'
65
+ ]
66
+ elif table == 'PostLinks' :
67
+ keys = [
68
+ 'Id'
69
+ , 'CreationDate'
70
+ , 'PostId'
71
+ , 'RelatedPostId'
72
+ , 'LinkTypeId'
73
+ ]
74
+ elif table == 'Comments' :
75
+ keys = [
76
+ 'Id'
77
+ , 'PostId'
78
+ , 'Score'
79
+ , 'Text'
80
+ , 'CreationDate'
81
+ , 'UserId'
82
+ ]
83
+ elif table == 'Votes' :
84
+ keys = [
85
+ 'Id'
86
+ , 'PostId'
87
+ , 'VoteTypeId'
88
+ , 'UserId'
89
+ , 'CreationDate'
90
+ , 'BountyAmount'
91
+ ]
92
+ elif table == 'Posts' :
93
+ keys = [
94
+ 'Id'
95
+ , 'PostTypeId'
96
+ , 'AcceptedAnswerId'
97
+ , 'ParentId'
98
+ , 'CreationDate'
99
+ , 'Score'
100
+ , 'ViewCount'
101
+ , 'Body'
102
+ , 'OwnerUserId'
103
+ , 'LastEditorUserId'
104
+ , 'LastEditorDisplayName'
105
+ , 'LastEditDate'
106
+ , 'LastActivityDate'
107
+ , 'Title'
108
+ , 'Tags'
109
+ , 'AnswerCount'
110
+ , 'CommentCount'
111
+ , 'FavoriteCount'
112
+ , 'ClosedDate'
113
+ , 'CommunityOwnedDate'
114
+ ]
115
+ elif table == 'Tags' :
116
+ keys = [
117
+ 'Id'
118
+ , 'TagName'
119
+ , 'Count'
120
+ , 'ExcerptPostId'
121
+ , 'WikiPostId'
122
+ ]
123
+ elif table == 'PostHistory' :
124
+ keys = [
125
+ 'Id' ,
126
+ 'PostHistoryTypeId' ,
127
+ 'PostId' ,
128
+ 'RevisionGUID' ,
129
+ 'CreationDate' ,
130
+ 'UserId' ,
131
+ 'Text'
132
+ ]
133
+ elif table == 'Comments' :
134
+ keys = [
135
+ 'Id' ,
136
+ 'PostId' ,
137
+ 'Score' ,
138
+ 'Text' ,
139
+ 'CreationDate' ,
140
+ 'UserId' ,
141
+ ]
142
+ return keys
143
+
39
144
def handleTable (table , keys , dbname , mbDbFile , mbHost , mbPort , mbUsername , mbPassword ):
40
145
"""Handle the table including the post/pre processing."""
146
+ keys = _getTableKeys (table )
41
147
dbFile = mbDbFile if mbDbFile is not None else table + '.xml'
42
148
tmpl = _createMogrificationTemplate (table , keys )
43
149
start_time = time .time ()
44
150
45
151
try :
46
152
pre = open ('./sql/' + table + '_pre.sql' ).read ()
47
153
post = open ('./sql/' + table + '_post.sql' ).read ()
154
+ fk = open ('./sql/' + table + '_fk.sql' ).read ()
48
155
except IOError as e :
49
- six .print_ ("Could not load pre/post sql. Are you running from the correct path?" , file = sys .stderr )
156
+ six .print_ ("Could not load pre/post/fk sql. Are you running from the correct path?" , file = sys .stderr )
50
157
sys .exit (- 1 )
51
158
52
159
dbConnectionParam = "dbname={}" .format (dbname )
@@ -65,6 +172,7 @@ def handleTable(table, keys, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPas
65
172
if mbPassword is not None :
66
173
dbConnectionParam += ' password={}' .format (mbPassword )
67
174
175
+
68
176
try :
69
177
with pg .connect (dbConnectionParam ) as conn :
70
178
with conn .cursor () as cur :
@@ -86,13 +194,12 @@ def handleTable(table, keys, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPas
86
194
for row_attribs in rows
87
195
]
88
196
)
89
-
90
197
if len (valuesStr ) > 0 :
91
198
cmd = 'INSERT INTO ' + table + \
92
199
' VALUES\n ' + valuesStr + ';'
93
200
cur .execute (cmd )
94
201
conn .commit ()
95
- six .print_ ('Table processing took {:.1f} seconds' .format (time .time () - start_time ))
202
+ six .print_ ('Table processing took {1 :.1f} seconds' .format (table , time .time () - start_time ))
96
203
97
204
# Post-processing (creation of indexes)
98
205
start_time = time .time ()
@@ -101,6 +208,14 @@ def handleTable(table, keys, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPas
101
208
cur .execute (post )
102
209
conn .commit ()
103
210
six .print_ ('Post processing took {} seconds' .format (time .time () - start_time ))
211
+ if createFk :
212
+ # fk-processing (creation of foreign keys)
213
+ start_time = time .time ()
214
+ six .print_ ('fk processing ...' )
215
+ if post != '' :
216
+ cur .execute (fk )
217
+ conn .commit ()
218
+ six .print_ ('fk processing took {} seconds' .format (time .time () - start_time ))
104
219
105
220
except IOError as e :
106
221
six .print_ ("Could not read from file {}." .format (dbFile ), file = sys .stderr )
@@ -113,8 +228,6 @@ def handleTable(table, keys, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPas
113
228
six .print_ ("Warning from the database." , file = sys .stderr )
114
229
six .print_ ("pg.Warning: {0}" .format (str (w )), file = sys .stderr )
115
230
116
-
117
-
118
231
#############################################################
119
232
120
233
parser = argparse .ArgumentParser ()
@@ -159,127 +272,30 @@ def handleTable(table, keys, dbname, mbDbFile, mbHost, mbPort, mbUsername, mbPas
159
272
, default = False
160
273
)
161
274
275
+ parser .add_argument ( '--foreign-keys'
276
+ , help = 'Create foreign keys.'
277
+ , action = 'store_true'
278
+ , default = False
279
+ )
280
+
162
281
args = parser .parse_args ()
163
282
164
283
table = args .table
165
- keys = None
166
-
167
- if table == 'Users' :
168
- keys = [
169
- 'Id'
170
- , 'Reputation'
171
- , 'CreationDate'
172
- , 'DisplayName'
173
- , 'LastAccessDate'
174
- , 'WebsiteUrl'
175
- , 'Location'
176
- , 'AboutMe'
177
- , 'Views'
178
- , 'UpVotes'
179
- , 'DownVotes'
180
- , 'ProfileImageUrl'
181
- , 'Age'
182
- , 'AccountId'
183
- ]
184
- elif table == 'Badges' :
185
- keys = [
186
- 'Id'
187
- , 'UserId'
188
- , 'Name'
189
- , 'Date'
190
- ]
191
- elif table == 'PostLinks' :
192
- keys = [
193
- 'Id'
194
- , 'CreationDate'
195
- , 'PostId'
196
- , 'RelatedPostId'
197
- , 'LinkTypeId'
198
- ]
199
- elif table == 'Comments' :
200
- keys = [
201
- 'Id'
202
- , 'PostId'
203
- , 'Score'
204
- , 'Text'
205
- , 'CreationDate'
206
- , 'UserId'
207
- ]
208
- elif table == 'Votes' :
209
- keys = [
210
- 'Id'
211
- , 'PostId'
212
- , 'VoteTypeId'
213
- , 'UserId'
214
- , 'CreationDate'
215
- , 'BountyAmount'
216
- ]
217
- elif table == 'Posts' :
218
- keys = [
219
- 'Id'
220
- , 'PostTypeId'
221
- , 'AcceptedAnswerId'
222
- , 'ParentId'
223
- , 'CreationDate'
224
- , 'Score'
225
- , 'ViewCount'
226
- , 'Body'
227
- , 'OwnerUserId'
228
- , 'LastEditorUserId'
229
- , 'LastEditorDisplayName'
230
- , 'LastEditDate'
231
- , 'LastActivityDate'
232
- , 'Title'
233
- , 'Tags'
234
- , 'AnswerCount'
235
- , 'CommentCount'
236
- , 'FavoriteCount'
237
- , 'ClosedDate'
238
- , 'CommunityOwnedDate'
239
- ]
240
-
241
- # If the user has not explicitly asked for loading the body, we replace it with NULL
242
- if not args .with_post_body :
243
- specialRules [('Posts' , 'Body' )] = 'NULL'
244
-
245
- elif table == 'Tags' :
246
- keys = [
247
- 'Id'
248
- , 'TagName'
249
- , 'Count'
250
- , 'ExcerptPostId'
251
- , 'WikiPostId'
252
- ]
253
- elif table == 'PostHistory' :
254
- keys = [
255
- 'Id' ,
256
- 'PostHistoryTypeId' ,
257
- 'PostId' ,
258
- 'RevisionGUID' ,
259
- 'CreationDate' ,
260
- 'UserId' ,
261
- 'Text'
262
- ]
263
- elif table == 'Comments' :
264
- keys = [
265
- 'Id' ,
266
- 'PostId' ,
267
- 'Score' ,
268
- 'Text' ,
269
- 'CreationDate' ,
270
- 'UserId' ,
271
- ]
272
284
273
285
try :
274
286
# Python 2/3 compatibility
275
287
input = raw_input
276
288
except NameError :
277
289
pass
278
290
279
- choice = input ('This will drop the {} table. Are you sure [y/n]?' .format (table ))
280
291
292
+ if table == 'Posts' :
293
+ # If the user has not explicitly asked for loading the body, we replace it with NULL
294
+ if not args .with_post_body :
295
+ specialRules [('Posts' , 'Body' )] = 'NULL'
296
+
297
+ choice = input ('This will drop the {} table. Are you sure [y/n]?' .format (table ))
281
298
if len (choice ) > 0 and choice [0 ].lower () == 'y' :
282
299
handleTable (table , keys , args .dbname , args .file , args .host , args .port , args .username , args .password )
283
300
else :
284
301
six .print_ ("Cancelled." )
285
-
0 commit comments