-
Notifications
You must be signed in to change notification settings - Fork 4
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.sql |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
||
|
@@ -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) | ||
|
||
|
@@ -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" | ||
) | ||
|
||
snapshotFQP = "atd-vzd/snapshots/" + snapshotFilename | ||
|
||
# fmt: off | ||
|
@@ -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 | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool - thanks for the sanity check 🙏 |
||
return parser | ||
|
||
|
||
|
There was a problem hiding this comment.
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.