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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
|
- import os
- import sys
- import re
- import time
- import logging
- import hashlib
- import threading
- from configparser import ConfigParser
- from pathlib import Path
- from contextlib import contextmanager
- from datetime import timedelta
- from typing import NamedTuple, List
- from docopt import docopt
- from natural.date import compress as compress_date
- import pandas as pd
- from more_itertools import peekable
- import psycopg2, psycopg2.errorcodes
- from psycopg2 import sql
- from psycopg2.pool import ThreadedConnectionPool
- from sqlalchemy import create_engine
- import sqlparse
- import git
- _log = logging.getLogger(__name__)
- # Meta-schema for storing stage and file status in the database
- _ms_path = Path(__file__).parent.parent / 'schemas' / 'meta-schema.sql'
- meta_schema = _ms_path.read_text()
- _pool = None
- _engine = None
- # DB configuration info
- class DBConfig:
- host: str
- port: str
- database: str
- user: str
- password: str
- @classmethod
- def load(cls):
- repo = git.Repo(search_parent_directories=True)
- cfg = ConfigParser()
- _log.debug('reading config from db.cfg')
- cfg.read([repo.working_tree_dir + '/db.cfg'])
- branch = repo.head.reference.name
- _log.info('reading database config for branch %s', branch)
- if branch in cfg:
- section = cfg[branch]
- else:
- _log.debug('No configuration for branch %s, using default', branch)
- section = cfg['DEFAULT']
- dbc = cls()
- dbc.host = section.get('host', 'localhost')
- dbc.port = section.get('port', None)
- dbc.database = section.get('database', None)
- dbc.user = section.get('user', None)
- dbc.password = section.get('password', None)
- if dbc.database is None:
- _log.error('No database specified for branch %s', branch)
- raise RuntimeError('no database specified')
- return dbc
- def url(self) -> str:
- url = 'postgresql://'
- if self.user:
- url += self.user
- if self.password:
- url += ':' + self.password
- url += '@'
- url += self.host
- if self.port:
- url += ':' + self.port
- url += '/' + self.database
- return url
- def db_url():
- "Get the URL to connect to the database."
- if 'DB_URL' in os.environ:
- _log.info('using env var DB_URL')
- return os.environ['DB_URL']
- config = DBConfig.load()
- _log.info('using database %s', config.database)
- return config.url()
- @contextmanager
- def connect():
- "Connect to a database. This context manager yields the connection, and closes it when exited."
- global _pool
- if _pool is None:
- _log.info('connecting to %s', db_url())
- _pool = ThreadedConnectionPool(1, 5, db_url())
- conn = _pool.getconn()
- try:
- yield conn
- finally:
- _pool.putconn(conn)
- def engine():
- "Get an SQLAlchemy engine"
- global _engine
- if _engine is None:
- _log.info('connecting to %s', db_url())
- _engine = create_engine(db_url())
- return _engine
- 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. This utility function is used to summarize statements."
- 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 with the following features ``psql``
- does not have:
- * Splitting the script into (named) steps, to commit chunks in transactions
- * Recording metadata (currently just dependencies) for the script
- * Allowing chunks to fail with specific errors
- The last feature is to help with writing _idempotent_ scripts: by allowing a chunk
- to fail with a known error (e.g. creating a constraint that already exists), you
- can write a script that can run cleanly even if it has already been run.
- Args:
- file: the path to the SQL script to read.
- """
- _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 = []
- self.deps, self.tables = self._parse_script_header(lines)
- 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_script_header(cls, lines):
- deps = []
- tables = []
- line = lines.peek(None)
- while line is not None:
- hm = cls._sep_re.match(line)
- if hm is None:
- break
- inst = hm.group('inst')
- cm = cls._icode_re.match(inst)
- if cm is None:
- next(lines) # eat line
- continue
- code = cm.group('code')
- args = cm.group('args')
- if code == 'dep':
- deps.append(args)
- next(lines) # eat line
- elif code == 'table':
- parts = args.split('.', 2)
- if len(parts) > 1:
- ns, tbl = parts
- tables.append((ns, tbl))
- else:
- tables.append(('public', args))
- next(lines) # eat line
- else: # any other code, we're out of header
- break
- line = lines.peek(None)
- return deps, tables
- @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.
- """
- all_st = time.perf_counter()
- 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)
- print('CHUNK ELAPSED', elapsed, file=transcript)
- _log.info('Finished ‘%s’ in %s', step.label, compress_date(elapsed))
- elapsed = time.perf_counter() - all_st
- elasped = timedelta(seconds=elapsed)
- _log.info('Script completed in %s', compress_date(elapsed))
- def describe(self):
- for dep in self.deps:
- _log.info('Dependency ‘%s’', dep)
- 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
- elapsed = timedelta(seconds=elapsed)
- 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),
- compress_date(elapsed), rows)
- else:
- _log.info('finished %s in %s (%d rows)', describe_statement(sql),
- compress_date(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
- class _LoadThread(threading.Thread):
- """
- Thread worker for copying database results to a stream we can read.
- """
- def __init__(self, dbc, query, dir='out'):
- super().__init__()
- self.database = dbc
- self.query = query
- rfd, wfd = os.pipe()
- self.reader = os.fdopen(rfd)
- self.writer = os.fdopen(wfd, 'w')
- self.chan = self.writer if dir == 'out' else self.reader
- def run(self):
- with self.chan, self.database.cursor() as cur:
- cur.copy_expert(self.query, self.chan)
- def load_table(dbc, query, **kwargs):
- """
- Load a query into a Pandas data frame.
- This is substantially more efficient than Pandas ``read_sql``, because it directly
- streams CSV data from the database instead of going through SQLAlchemy.
- """
- cq = sql.SQL('COPY ({}) TO STDOUT WITH CSV HEADER')
- q = sql.SQL(query)
- thread = _LoadThread(dbc, cq.format(q))
- thread.start()
- data = pd.read_csv(thread.reader, **kwargs)
- thread.join()
- return data
- def save_table(dbc, table, data: pd.DataFrame):
- """
- Save a table from a Pandas data frame.
- This is substantially more efficient than Pandas ``read_sql``, because it directly
- streams CSV data from the database instead of going through SQLAlchemy.
- """
- cq = sql.SQL('COPY {} FROM STDIN WITH CSV')
- thread = _LoadThread(dbc, cq.format(table), 'in')
- thread.start()
- data.to_csv(thread.writer, header=False, index=False)
- thread.writer.close()
- thread.join()
|