Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

sql-script.py 1.4 KB

You have to be logged in to leave a comment. Sign In
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
  1. """
  2. Usage:
  3. sql-script.py [options] SCRIPT
  4. Options:
  5. -T, --transcript FILE
  6. Write the execution transcript to FILE.
  7. -s, --stage-name NAME
  8. Record as stage NAME.
  9. --dry-run
  10. Print the script's information without actually running it.
  11. --verbose
  12. Verbose logging information.
  13. SCRIPT
  14. The script to run.
  15. """
  16. import os
  17. import sys
  18. import re
  19. import time
  20. from pathlib import Path
  21. from datetime import timedelta
  22. from typing import NamedTuple, List
  23. from docopt import docopt
  24. import psycopg2
  25. from more_itertools import peekable
  26. import sqlparse
  27. from bookdata import script_log
  28. from bookdata import db
  29. opts = docopt(__doc__)
  30. _log = script_log(__name__, opts.get('--verbose'))
  31. script_file = Path(opts.get('SCRIPT'))
  32. tfile = opts.get('-T', None)
  33. if tfile:
  34. tfile = Path(tfile)
  35. else:
  36. tfile = script_file.with_suffix('.transcript')
  37. stage = opts.get('-s', None)
  38. if not stage:
  39. stage = script_file.stem
  40. _log.info('reading %s', script_file)
  41. script = db.SqlScript(script_file)
  42. _log.info('%s has %d chunks', script_file, len(script.chunks))
  43. if opts.get('--dry-run'):
  44. script.describe()
  45. else:
  46. with tfile.open('w') as txf, db.connect() as dbc:
  47. with dbc, dbc.cursor() as cur:
  48. db.start_stage(cur, stage)
  49. db.save_file(cur, script_file, stage)
  50. script.execute(dbc, transcript=txf)
  51. with dbc, dbc.cursor() as cur:
  52. db.finish_stage(cur, stage)
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...