Repository navigation
Apply current publication SQL during upgrades #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,82 @@ | ||
| """Exercise deployment orchestration with a disposable Compose command recorder.""" | ||
|
|
||
| import json | ||
| import os | ||
| import shutil | ||
| import subprocess | ||
| import sys | ||
| import tempfile | ||
| import unittest | ||
| from pathlib import Path | ||
| from typing import Any | ||
|
|
||
| ROOT = Path(__file__).resolve().parents[2] | ||
|
|
||
|
|
||
| class DeploymentScriptTest(unittest.TestCase): | ||
| def setUp(self) -> None: | ||
| directory = tempfile.TemporaryDirectory() | ||
| self.addCleanup(directory.cleanup) | ||
| self.root = Path(directory.name) | ||
| shutil.copy(ROOT / "deploy/deploy.sh", self.root / "deploy.sh") | ||
| (self.root / "validate_config.py").write_text("pass\n") | ||
| self.sql = self.root / "bootstrap-powersync.sql" | ||
| self.calls = self.root / "compose-calls.jsonl" | ||
| binary = self.root / "bin" | ||
| binary.mkdir() | ||
| docker = binary / "docker" | ||
|
|
||
| docker.write_text( | ||
| f"#!{sys.executable}\n" | ||
| "import json, os, sys\n" | ||
| "args = sys.argv[1:]\n" | ||
| "publication = 'exec' in args and 'database' in args\n" | ||
| "data = sys.stdin.read() if publication else None\n" | ||
| "with open(os.environ['COMPOSE_CALLS'], 'a') as output:\n" | ||
| " output.write(json.dumps({'args': args, 'stdin': data}) + '\\n')\n" | ||
| "if publication and os.environ.get('PUBLICATION_FAILURE'):\n" | ||
| " sys.exit(42)\n" | ||
| ) | ||
|
|
||
| docker.chmod(0o755) | ||
|
|
||
| self.environment = { | ||
| **os.environ, | ||
| "PATH": str(binary) + os.pathsep + os.environ["PATH"], | ||
| "COMPOSE_CALLS": str(self.calls), | ||
| } | ||
|
|
||
| def run_deployment(self) -> subprocess.CompletedProcess[str]: | ||
| return subprocess.run( | ||
| ["sh", str(self.root / "deploy.sh")], | ||
| env=self.environment, | ||
| stdin=subprocess.DEVNULL, | ||
| capture_output=True, | ||
| text=True, | ||
| ) | ||
|
|
||
| def recorded_calls(self) -> list[dict[str, Any]]: | ||
| return [json.loads(line) for line in self.calls.read_text().splitlines()] | ||
|
|
||
| def test_each_upgrade_streams_current_host_sql(self) -> None: | ||
| for sql in ("SELECT 'books';\n", "SELECT 'books', 'reading_goals', 'reading_activities';\n"): | ||
| replacement = self.root / "bootstrap-next.sql" | ||
| replacement.write_text(sql) | ||
| replacement.replace(self.sql) | ||
| result = self.run_deployment() | ||
| self.assertEqual(result.returncode, 0, result.stderr) | ||
| publication_calls = [call for call in self.recorded_calls() if call["stdin"] is not None] | ||
| self.assertEqual(publication_calls[-1]["stdin"], sql) | ||
|
|
||
| def test_publication_failure_prevents_service_activation(self) -> None: | ||
| self.sql.write_text("SELECT 'reading_goals';\n") | ||
| self.environment["PUBLICATION_FAILURE"] = "1" | ||
| result = self.run_deployment() | ||
| self.assertEqual(result.returncode, 42) | ||
| calls = self.recorded_calls() | ||
| self.assertIsNotNone(calls[-1]["stdin"]) | ||
| self.assertFalse(any("up" in call["args"] and "api" in call["args"] for call in calls)) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| unittest.main() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.