Added pgsql.sql command for scripting raw SQL.

development
Shawn Davis 7 months ago
parent a10e6a7069
commit c659b7c999
  1. 4
      Makefile
  2. 16
      scripttease/lib/commands/pgsql.py
  3. 8
      tests/test_lib_commands_pgsql.py
  4. 2
      tests/test_lib_factories.py

@ -51,7 +51,7 @@ docs: lines
cd docs && make html;
cd docs && make coverage;
open docs/build/coverage/python.txt;
open docs/build/html/index.html;
firefox docs/build/html/index.html;
#> clean - Remove pyc files and documentation builds.
clean:
@ -91,5 +91,5 @@ support:
tests:
coverage run --source=. -m pytest;
coverage html --directory=$(COVERAGE_PATH);
open $(COVERAGE_PATH)/index.html;
firefox $(COVERAGE_PATH)/index.html;

@ -14,6 +14,7 @@ __all__ = (
"pgsql_exists",
"pgsql_grant",
"pgsql_load",
"pgsql_sql",
"pgsql_user",
)
@ -242,6 +243,17 @@ def pgsql_load(database, path, **kwargs):
return pgsql("psql", **kwargs)
def pgsql_sql(statement, database="template1", **kwargs):
kwargs.setdefault("comment", "run SQL statement")
kwargs['dbname'] = database
command = pgsql("psql", **kwargs)
command.statement += ' -c "%s"' % statement
return command
def pgsql_user(name, admin_pass=None, admin_user="postgres", op="create", password=None, **kwargs):
"""Work with a PostgreSQL user.
@ -278,7 +290,7 @@ def pgsql_user(name, admin_pass=None, admin_user="postgres", op="create", passwo
return pgsql("dropuser", name, password=admin_pass, user=admin_user, **kwargs)
elif op == "exists":
kwargs.setdefault("comment", "determine if %s postgres user exists" % name)
kwargs.setdefault("register", "pgsql_use_exists")
kwargs.setdefault("register", "pgsql_user_exists")
command = pgsql("psql", password=admin_pass, user=admin_user, **kwargs)
@ -297,6 +309,6 @@ PGSQL_MAPPINGS = {
'pgsql.dump': pgsql_dump,
'pgsql.exists': pgsql_exists,
'pgsql.grant': pgsql_grant,
# 'pgsql.sql': pgsql_exec,
'pgsql.sql': pgsql_sql,
'pgsql.user': pgsql_user,
}

@ -71,6 +71,14 @@ def test_pgsql_load():
assert '--bogus=1' in s # to test passing any key
def test_pgsql_sql():
c = pgsql_sql("UPDATE etl_testing SET is_processed = 't'", database="example_app")
s = c.get_statement()
# psql -U postgres --host=localhost --port=5432 --dbname="example_app" -c "UPDATE etl_testing SET is_processed = 't'"
assert '--dbname="example_app"' in s
assert '-c "UPDATE etl_testing SET is_processed = \'t\'"' in s
def test_pgsql_user():
c = pgsql_user("testing", password="secret")
s = c.get_statement()

@ -21,7 +21,7 @@ def test_command_factory():
ini = INILoader("tests/examples/kitchen_sink.ini")
ini.load()
commands = command_factory(ini)
assert len(commands) == 48
assert len(commands) == 49
ini = INILoader("tests/examples/bad_command.ini")
ini.load()

Loading…
Cancel
Save