Skip to content
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

Extend the local stack script to pg_dump and restore the local DB #1280

Merged
merged 4 commits into from
Sep 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,15 @@ Stop the stack
* Create and repopulate the database from the snapshot

Note: the `-c / --include-change-log-data` flag can be used to opt to include the data of past change log events. The schema is created either way.
Note: the `-f / --filename` flag can be optionally used to point to a specific data dump .sql file to use to restore.

The way the snapshots are dated means that one will only end up downloading
one copy of the data per-day, both in the with and without change log data.

#### `vision-zero dump-local-db`
* pg_dump the current local database
* Stores the file in `./atd-vzd/dumps/visionzero-{date}-{time}.sql

#### `vision-zero remove-snapshots`
Remove snapshot files. This can be done to save space and clean up old snapshots, but it's also useful to cause a new copy of the day's data to be downloaded if an upstream change is made.

Expand Down
1 change: 1 addition & 0 deletions atd-vzd/dumps/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
*.sql
50 changes: 44 additions & 6 deletions vision-zero
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import subprocess
import os
import shtab
import shutil
from datetime import date
from datetime import date, datetime
import pprint
from dotenv import load_dotenv

Expand Down Expand Up @@ -76,6 +76,8 @@ def doCommand(args):
elif args.command == "replicate-db":
dbUp(args)
replicateDb(args)
elif args.command == "dump-local-db":
dumpLocalDb(args)
elif args.command == "remove-snapshots":
removeSnapshots(args)

Expand All @@ -94,11 +96,16 @@ def replicateDb(args):
graphqlEngineDown(args)

# print(tool_runner_command + replicate_command)
today = date.today().strftime("%Y-%m-%d")
snapshotFilename = (
"visionzero_" + today + "_" + (
"with-change-log" if args.include_change_log_data else "without-change-log") + ".sql"
)
snapshotFilename = ""
if(args.filename):
snapshotFilename = args.filename
else:
today = date.today().strftime("%Y-%m-%d")
snapshotFilename = (
"visionzero_" + today + "_" + (
"with-change-log" if args.include_change_log_data else "without-change-log") + ".sql"
)
Comment on lines +100 to +107
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reallllly like that you can specify a filename to be used when pulling data down from the production machine.


snapshotFQP = "atd-vzd/snapshots/" + snapshotFilename

# fmt: off
Expand Down Expand Up @@ -191,6 +198,35 @@ def replicateDb(args):

graphqlEngineUp(args)

def dumpLocalDb(args):
print("️Dumping local database")
today = datetime.now().strftime("%Y-%m-%d-%H-%M:%S")

dump_runner_command = docker_compose_invocation + [
"-f", "docker-compose.yml",
"-f",
"docker-compose-ram-disk.yml" if args.ram_disk else "docker-compose-docker-volume.yml",
"run",
"--rm",
"-e", "PGHOST=" + 'postgis',
"-e", "PGUSER=" + os.environ["POSTGRES_USER"],
"-e", "PGPASSWORD=" + os.environ["POSTGRES_PASSWORD"],
"-e", "PGDATABASE=" + os.environ["POSTGRES_DB"],
"db-tools",
]

dump_command = [
"pg_dump",
"--clean",
"--create",
"--no-owner",
"--no-privileges",
"--if-exists",
]

dump = open("atd-vzd/dumps/visionzero_" + today + ".sql", "w")
subprocess.run(dump_runner_command + dump_command, stdout=dump)
dump.close()

def psql(args):
# fmt: off
Expand Down Expand Up @@ -360,10 +396,12 @@ def get_main_parser():
"psql",
"tools-shell",
"replicate-db",
"dump-local-db",
"remove-snapshots",
"hasura-console"
],
)
parser.add_argument("-f", "--filename", required=False)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first, I was going to suggest something more specific that describes what the file needs to be, but then I realized that you can only use the -f flag meaningfully after the replicate-db command, so 👍.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool - thanks for the sanity check 🙏

return parser


Expand Down
Loading