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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
|
- import os
- import sys
- import re
- import time
- import logging
- import hashlib
- from pathlib import Path
- from contextlib import contextmanager
- from datetime import timedelta
- from typing import NamedTuple, List
- from docopt import docopt
- from more_itertools import peekable
- import psycopg2, psycopg2.errorcodes
- import sqlparse
- _log = logging.getLogger(__name__)
- # Meta-schema for storing stage and file status in the database
- meta_schema = """
- CREATE TABLE IF NOT EXISTS stage_status (
- stage_name VARCHAR PRIMARY KEY,
- started_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- finished_at TIMESTAMP NULL,
- stage_key VARCHAR NULL
- );
- CREATE TABLE IF NOT EXISTS source_file (
- filename VARCHAR NOT NULL PRIMARY KEY,
- reg_time TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
- checksum VARCHAR NOT NULL
- );
- CREATE TABLE IF NOT EXISTS stage_file (
- stage_name VARCHAR NOT NULL REFERENCES stage_status,
- filename VARCHAR NOT NULL REFERENCES source_file
- );
- INSERT INTO stage_status (stage_name, started_at, finished_at, stage_key)
- VALUES ('init', NOW(), NOW(), uuid_generate_v4())
- ON CONFLICT (stage_name) DO NOTHING;
- """
- def db_url():
- if 'DB_URL' in os.environ:
- return os.environ['DB_URL']
- host = os.environ.get('PGHOST', 'localhost')
- port = os.environ.get('PGPORT', None)
- db = os.environ['PGDATABASE']
- user = os.environ.get('PGUSER', None)
- pw = os.environ.get('PGPASSWORD', None)
- url = 'postgresql://'
- if user:
- url += user
- if pw:
- url += ':' + pw
- url += '@'
- url += host
- if port:
- url += ':' + port
- url += '/' + db
- return url
- @contextmanager
- def connect():
- "Connect to a database. This context manager yields the connection, and closes it when exited."
- _log.info('connecting to %s', db_url())
- conn = psycopg2.connect(db_url())
- try:
- yield conn
- finally:
- conn.close()
- def save_file(cur, path, stage=None):
- h = hashlib.md5()
- with open(path, 'rb') as f:
- data = f.read(8192 * 4)
- while data:
- h.update(data)
- data = f.read(8192 * 4)
- hash = h.hexdigest()
- path = Path(path).as_posix()
- _log.info('recording file %s with hash %s', path, hash)
- cur.execute('''
- INSERT INTO source_file (filename, checksum)
- VALUES (%(fn)s, %(hash)s)
- ON CONFLICT (filename)
- DO UPDATE SET reg_time = now(), checksum = %(hash)s
- ''', {'fn': path, 'hash': hash})
- if stage:
- cur.execute('''
- INSERT INTO stage_file (stage_name, filename)
- VALUES (%s, %s)
- ''', [stage, path])
- def start_stage(cur, stage):
- if hasattr(cur, 'cursor'):
- # this is a connection
- with cur, cur.cursor() as c:
- start_stage(c, stage)
- _log.info('starting or resetting stage %s', stage)
- cur.execute('''
- INSERT INTO stage_status (stage_name)
- VALUES (%s)
- ON CONFLICT (stage_name)
- DO UPDATE SET started_at = now(), finished_at = NULL, stage_key = NULL
- ''', [stage])
- cur.execute('DELETE FROM stage_file WHERE stage_name = %s', [stage])
- def finish_stage(cur, stage, key=None):
- if hasattr(cur, 'cursor'):
- # this is a connection
- with cur, cur.cursor() as c:
- finish_stage(c, stage, key)
- _log.info('finishing stage %s', stage)
- cur.execute('''
- UPDATE stage_status
- SET finished_at = NOW(), stage_key = %(key)s
- WHERE stage_name = %(stage)s
- ''', {'stage': stage, 'key': key})
- def _tokens(s, start=-1, skip_ws=True, skip_cm=True):
- i, t = s.token_next(start, skip_ws=skip_ws, skip_cm=skip_cm)
- while t is not None:
- yield t
- i, t = s.token_next(i, skip_ws=skip_ws, skip_cm=skip_cm)
- def describe_statement(s):
- "Describe an SQL statement"
- label = s.get_type()
- li, lt = s.token_next(-1, skip_cm=True)
- if lt is None:
- return None
- if lt and lt.ttype == sqlparse.tokens.DDL:
- # DDL - build up!
- parts = []
- first = True
- skipping = False
- for t in _tokens(s, li):
- if not first:
- if isinstance(t, sqlparse.sql.Identifier) or isinstance(t, sqlparse.sql.Function):
- parts.append(t.normalized)
- break
- elif t.ttype != sqlparse.tokens.Keyword:
- break
- first = False
- if t.normalized == 'IF':
- skipping = True
- if not skipping:
- parts.append(t.normalized)
- label = label + ' ' + ' '.join(parts)
- elif label == 'UNKNOWN':
- ls = []
- for t in _tokens(s):
- if t.ttype == sqlparse.tokens.Keyword:
- ls.append(t.normalized)
- else:
- break
- if ls:
- label = ' '.join(ls)
- name = s.get_real_name()
- if name:
- label += f' {name}'
- return label
- def is_empty(s):
- "check if an SQL statement is empty"
- lt = s.token_first(skip_cm=True, skip_ws=True)
- return lt is None
- class ScriptChunk(NamedTuple):
- "A single chunk of an SQL script."
- label: str
- allowed_errors: List[str]
- src: str
- use_transaction: bool = True
- @property
- def statements(self):
- return [s for s in sqlparse.parse(self.src) if not is_empty(s)]
- class SqlScript:
- """
- Class for processing & executing SQL scripts.
- """
- _sep_re = re.compile(r'^---\s*(?P<inst>.*)')
- _icode_re = re.compile(r'#(?P<code>\w+)\s*(?P<args>.*\S)?\s*$')
- chunks: List[ScriptChunk]
- def __init__(self, file):
- if hasattr(file, 'read'):
- self._parse(peekable(file))
- else:
- with open(file, 'r', encoding='utf8') as f:
- self._parse(peekable(f))
- def _parse(self, lines):
- self.chunks = []
- next_chunk = self._parse_chunk(lines, len(self.chunks) + 1)
- while next_chunk is not None:
- if next_chunk:
- self.chunks.append(next_chunk)
- next_chunk = self._parse_chunk(lines, len(self.chunks) + 1)
- @classmethod
- def _parse_chunk(cls, lines: peekable, n: int):
- qlines = []
- chunk = cls._read_header(lines)
- qlines = cls._read_query(lines)
- # end of file, do we have a chunk?
- if qlines:
- if chunk.label is None:
- chunk = chunk._replace(label=f'Step {n}')
- return chunk._replace(src='\n'.join(qlines))
- elif qlines is not None:
- return False # empty chunk
- @classmethod
- def _read_header(cls, lines: peekable):
- label = None
- errs = []
- tx = True
- line = lines.peek(None)
- while line is not None:
- hm = cls._sep_re.match(line)
- if hm is None:
- break
- next(lines) # eat line
- line = lines.peek(None)
- inst = hm.group('inst')
- cm = cls._icode_re.match(inst)
- if cm is None:
- continue
- code = cm.group('code')
- args = cm.group('args')
- if code == 'step':
- label = args
- elif code == 'allow':
- err = getattr(psycopg2.errorcodes, args.upper())
- _log.debug('step allows error %s (%s)', args, err)
- errs.append(err)
- elif code == 'notx':
- _log.debug('chunk will run outside a transaction')
- tx = False
- else:
- _log.error('unrecognized query instruction %s', code)
- raise ValueError(f'invalid query instruction {code}')
- return ScriptChunk(label=label, allowed_errors=errs, src=None,
- use_transaction=tx)
- @classmethod
- def _read_query(cls, lines: peekable):
- qls = []
- line = lines.peek(None)
- while line is not None and not cls._sep_re.match(line):
- qls.append(next(lines))
- line = lines.peek(None)
- # trim lines
- while qls and not qls[0].strip():
- qls.pop(0)
- while qls and not qls[-1].strip():
- qls.pop(-1)
- if qls or line is not None:
- return qls
- else:
- return None # end of file
- def execute(self, dbc, transcript=None):
- """
- Execute the SQL script.
- Args:
- dbc: the database connection.
- transcript: a file to receive the run transcript.
- """
- for step in self.chunks:
- start = time.perf_counter()
- _log.info('Running ‘%s’', step.label)
- if transcript is not None:
- print('CHUNK', step.label, file=transcript)
- if step.use_transaction:
- with dbc, dbc.cursor() as cur:
- self._run_step(step, dbc, cur, True, transcript)
- else:
- ac = dbc.autocommit
- try:
- dbc.autocommit = True
- with dbc.cursor() as cur:
- self._run_step(step, dbc, cur, False, transcript)
- finally:
- dbc.autocommit = ac
- elapsed = time.perf_counter() - start
- elapsed = timedelta(seconds=elapsed)
- _log.info('Finished ‘%s’ in %s', step.label, elapsed)
- def describe(self):
- for step in self.chunks:
- _log.info('Chunk ‘%s’', step.label)
- for s in step.statements:
- _log.info('Statement %s', describe_statement(s))
- def _run_step(self, step, dbc, cur, commit, transcript):
- try:
- for sql in step.statements:
- start = time.perf_counter()
- _log.debug('Executing %s', describe_statement(sql))
- _log.debug('Query: %s', sql)
- if transcript is not None:
- print('STMT', describe_statement(sql), file=transcript)
- cur.execute(str(sql))
- elapsed = time.perf_counter() - start
- rows = cur.rowcount
- if transcript is not None:
- print('ELAPSED', elapsed, file=transcript)
- if rows is not None and rows >= 0:
- if transcript is not None:
- print('ROWS', rows, file=transcript)
- _log.info('finished %s in %s (%d rows)', describe_statement(sql),
- timedelta(seconds=elapsed), rows)
- else:
- _log.info('finished %s in %s (%d rows)', describe_statement(sql),
- timedelta(seconds=elapsed), rows)
- if commit:
- dbc.commit()
- except psycopg2.Error as e:
- if e.pgcode in step.allowed_errors:
- _log.info('Failed with acceptable error %s (%s)',
- e.pgcode, psycopg2.errorcodes.lookup(e.pgcode))
- if transcript is not None:
- print('ERROR', e.pgcode, psycopg2.errorcodes.lookup(e.pgcode), file=transcript)
- else:
- _log.error('Error in "%s" %s: %s: %s',
- step.label, describe_statement(sql),
- psycopg2.errorcodes.lookup(e.pgcode), e)
- if e.pgerror:
- _log.info('Query diagnostics:\n%s', e.pgerror)
- raise e
|