-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #73 from peopledoc/psql
Use psql to run migrations
- Loading branch information
Showing
6 changed files
with
157 additions
and
257 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import io | ||
import os | ||
|
||
import pytest | ||
|
||
from septentrion.db import Query | ||
from septentrion.runner import Script, SQLRunnerException | ||
|
||
|
||
@pytest.fixture() | ||
def run_script(db, settings_factory, tmp_path): | ||
settings = settings_factory(**db) | ||
|
||
def _run_script(script): | ||
path = tmp_path / "script.sql" | ||
path.write_text(script) | ||
|
||
with io.open(path, "r", encoding="utf8") as f: | ||
script = Script(settings, f, path) | ||
script.run() | ||
|
||
return _run_script | ||
|
||
|
||
@pytest.fixture() | ||
def env(): | ||
environ = {**os.environ} | ||
yield os.environ | ||
os.environ.clear() | ||
os.environ.update(environ) | ||
|
||
|
||
def test_run_simple(db, settings_factory, run_script): | ||
settings = settings_factory(**db) | ||
|
||
run_script("CREATE TABLE foo ();") | ||
|
||
query = "SELECT COUNT(*) FROM pg_catalog.pg_tables WHERE tablename = 'foo'" | ||
with Query(settings, query) as cur: | ||
assert [row[0] for row in cur] == [1] | ||
|
||
|
||
def test_run_simple_error(run_script): | ||
with pytest.raises(SQLRunnerException) as err: | ||
run_script("CREATE TABLE ???") | ||
|
||
assert 'ERROR: syntax error at or near "???"' in str(err.value) | ||
|
||
|
||
def test_run_psql_not_found(run_script, env): | ||
env["PATH"] = "" | ||
|
||
with pytest.raises(RuntimeError) as err: | ||
run_script("SELECT 1;") | ||
|
||
assert str(err.value) == ( | ||
"Septentrion requires the 'psql' executable to be present in the PATH." | ||
) | ||
|
||
|
||
def test_run_with_meta_loop(db, settings_factory, run_script): | ||
settings = settings_factory(**db) | ||
|
||
# create a table with 10 rows | ||
script = """ | ||
CREATE TABLE foo(value int); | ||
INSERT INTO foo SELECT generate_series(1, 10); | ||
""" | ||
run_script(script) | ||
|
||
query = "SELECT * FROM foo ORDER BY value" | ||
with Query(settings, query) as cur: | ||
assert [row[0] for row in cur] == [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] | ||
|
||
# update the rows 3 by 3 to multiply them by 100 | ||
script = """ | ||
--meta-psql:do-until-0 | ||
WITH to_update AS ( | ||
SELECT value FROM foo WHERE value < 100 LIMIT 3 | ||
) | ||
UPDATE foo SET value = foo.value * 100 | ||
FROM to_update WHERE foo.value = to_update.value | ||
--meta-psql:done | ||
""" | ||
run_script(script) | ||
|
||
query = "SELECT * FROM foo ORDER BY value" | ||
with Query(settings, query) as cur: | ||
assert [row[0] for row in cur] == [ | ||
100, | ||
200, | ||
300, | ||
400, | ||
500, | ||
600, | ||
700, | ||
800, | ||
900, | ||
1000, | ||
] |
Oops, something went wrong.