Add runsql
command, mostly for dev use
at least for now? kind of in a rush at the moment
This commit is contained in:
parent
ddca6b7d9b
commit
41da3e6ae8
|
@ -39,6 +39,7 @@ import logging
|
||||||
from getpass import getpass
|
from getpass import getpass
|
||||||
|
|
||||||
import six
|
import six
|
||||||
|
import sqlalchemy as sa
|
||||||
from sqlalchemy import orm
|
from sqlalchemy import orm
|
||||||
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
|
from sqlalchemy.orm.exc import NoResultFound, MultipleResultsFound
|
||||||
|
|
||||||
|
@ -1014,6 +1015,43 @@ class PalmCommand(Subcommand):
|
||||||
sys.stderr.write("{}\n".format(error))
|
sys.stderr.write("{}\n".format(error))
|
||||||
|
|
||||||
|
|
||||||
|
class RunSQL(Subcommand):
|
||||||
|
"""
|
||||||
|
Run (first statement of) a SQL script against a database
|
||||||
|
"""
|
||||||
|
name = 'runsql'
|
||||||
|
description = __doc__.strip()
|
||||||
|
|
||||||
|
def add_parser_args(self, parser):
|
||||||
|
parser.add_argument('engine',
|
||||||
|
help="SQLAlchemy engine URL for the database.")
|
||||||
|
parser.add_argument('script', type=argparse.FileType('r'),
|
||||||
|
help="Path to file which contains a SQL script.")
|
||||||
|
parser.add_argument('--max-width', type=int, default=80,
|
||||||
|
help="Max table width when displaying results.")
|
||||||
|
|
||||||
|
def run(self, args):
|
||||||
|
import texttable
|
||||||
|
|
||||||
|
sql = []
|
||||||
|
for line in args.script:
|
||||||
|
line = line.strip()
|
||||||
|
if line and not line.startswith('--'):
|
||||||
|
sql.append(line)
|
||||||
|
if line.endswith(';'):
|
||||||
|
break
|
||||||
|
|
||||||
|
sql = ' '.join(sql)
|
||||||
|
engine = sa.create_engine(args.engine)
|
||||||
|
|
||||||
|
result = engine.execute(sql)
|
||||||
|
rows = result.fetchall()
|
||||||
|
if rows:
|
||||||
|
table = texttable.Texttable(max_width=args.max_width)
|
||||||
|
table.add_rows([rows[0].keys()] + rows)
|
||||||
|
self.stdout.write("{}\n".format(table.draw()))
|
||||||
|
|
||||||
|
|
||||||
class Upgrade(Subcommand):
|
class Upgrade(Subcommand):
|
||||||
"""
|
"""
|
||||||
Upgrade the local Rattail app
|
Upgrade the local Rattail app
|
||||||
|
|
2
setup.py
2
setup.py
|
@ -68,6 +68,7 @@ requires = [
|
||||||
'openpyxl', # 2.5.0
|
'openpyxl', # 2.5.0
|
||||||
'progress', # 1.3
|
'progress', # 1.3
|
||||||
'six', # 1.10.0
|
'six', # 1.10.0
|
||||||
|
'texttable', # 1.2.1
|
||||||
'xlrd', # 1.1.0
|
'xlrd', # 1.1.0
|
||||||
|
|
||||||
# TODO: Remove this / make it optional / etc.
|
# TODO: Remove this / make it optional / etc.
|
||||||
|
@ -206,6 +207,7 @@ make-appdir = rattail.commands.core:MakeAppDir
|
||||||
make-config = rattail.commands.core:MakeConfig
|
make-config = rattail.commands.core:MakeConfig
|
||||||
make-user = rattail.commands.core:MakeUser
|
make-user = rattail.commands.core:MakeUser
|
||||||
make-uuid = rattail.commands.core:MakeUUID
|
make-uuid = rattail.commands.core:MakeUUID
|
||||||
|
runsql = rattail.commands.core:RunSQL
|
||||||
upgrade = rattail.commands.core:Upgrade
|
upgrade = rattail.commands.core:Upgrade
|
||||||
|
|
||||||
[rattail_dev.commands]
|
[rattail_dev.commands]
|
||||||
|
|
Loading…
Reference in a new issue