No longer require a migration in seedfile

We don't need to do this any more, since our database backups now
include the migration table. The seedfile command also no longer
requires the user to type a password.

This also prevents the Discord history bot from running when it has no
credentials.
This commit is contained in:
Ben Visness 2021-09-05 19:43:49 -05:00
parent 6f7237f656
commit b53770932d
9 changed files with 55 additions and 36 deletions

1
.gitignore vendored
View File

@ -11,3 +11,4 @@ annotations/
hmn.conf hmn.conf
adminmailer/config.go adminmailer/config.go
adminmailer/adminmailer adminmailer/adminmailer
local/backups

0
local/backups/.gitkeep Normal file
View File

View File

@ -0,0 +1,15 @@
#!/bin/bash
set -euo pipefail
s3cmd ls s3://hmn-backup/db/
echo ""
echo "Above is a list of all the available database backups."
echo "Enter the name of the one you would like to download (e.g. \"hmn_pg_dump_live_2021-09-01\"):"
read filename
s3cmd get --force s3://hmn-backup/db/$filename ./local/backups/$filename
echo ""
echo "Downloaded $filename to local/backups."

22
local/resetdb.sh Normal file
View File

@ -0,0 +1,22 @@
#!/bin/bash
set -eou pipefail
# This script is for use in local development only. It wipes the existing db,
# creates a new empty one, runs the initial migration to create the schema,
# and then imports actual db content on top of that.
# TODO(opensource): We should adapt Asaf's seedfile command and then delete this.
THIS_PATH=$(pwd)
BETA_PATH='/mnt/c/Users/bvisn/Developer/handmade/handmade-beta'
# BETA_PATH='/Users/benvisness/Developer/handmade/handmade-beta'
pushd $BETA_PATH
docker-compose down -v
docker-compose up -d postgres s3
sleep 3
docker-compose exec postgres bash -c "psql -U postgres -c \"CREATE ROLE hmn CREATEDB LOGIN PASSWORD 'password';\""
popd
go run src/main.go seedfile local/backups/hmn_pg_dump_live_2021-09-06

View File

@ -13,7 +13,7 @@ echo "Above is a list of all the available database backups."
echo "Enter the name of the one you would like to download (e.g. \"hmn_pg_dump_live_2021-09-01\"):" echo "Enter the name of the one you would like to download (e.g. \"hmn_pg_dump_live_2021-09-01\"):"
read filename read filename
s3cmd --config /home/hmn/.s3cfg get s3://hmn-backup/db/$filename $filename s3cmd --config /home/hmn/.s3cfg get --force s3://hmn-backup/db/$filename $filename
echo "" echo ""
echo "Downloaded $filename to $(pwd)." echo "Downloaded $filename to $(pwd)."

View File

@ -377,8 +377,7 @@ ${BLUE_BOLD}Download and restore a database backup${RESET}
su hmn su hmn
cd ~ cd ~
hmn migrate --list hmn seedfile <your backup file>
hmn seedfile <your backup file> <ID of initial migration>
hmn migrate hmn migrate
${BLUE_BOLD}Restore static files${RESET} ${BLUE_BOLD}Restore static files${RESET}

View File

@ -17,8 +17,14 @@ func RunHistoryWatcher(ctx context.Context, dbConn *pgxpool.Pool) <-chan struct{
log := logging.ExtractLogger(ctx).With().Str("discord goroutine", "history watcher").Logger() log := logging.ExtractLogger(ctx).With().Str("discord goroutine", "history watcher").Logger()
ctx = logging.AttachLoggerToContext(&log, ctx) ctx = logging.AttachLoggerToContext(&log, ctx)
done := make(chan struct{}) if config.Config.Discord.BotToken == "" {
log.Warn().Msg("No Discord bot token was provided, so the Discord history bot cannot run.")
done := make(chan struct{}, 1)
done <- struct{}{}
return done
}
done := make(chan struct{})
go func() { go func() {
defer func() { defer func() {
log.Debug().Msg("shut down Discord history watcher") log.Debug().Msg("shut down Discord history watcher")

View File

@ -65,24 +65,16 @@ func init() {
} }
seedFromFileCommand := &cobra.Command{ seedFromFileCommand := &cobra.Command{
Use: "seedfile <filename> <after migration id>", Use: "seedfile <filename>",
Short: "Resets the db, runs migrations up to and including <after migration id>, and runs the seed file.", Short: "Resets the db and runs the seed file.",
Run: func(cmd *cobra.Command, args []string) { Run: func(cmd *cobra.Command, args []string) {
if len(args) < 2 { if len(args) < 1 {
fmt.Printf("You must provide a seed file and migration id.\n\n") fmt.Printf("You must provide a seed file.\n\n")
cmd.Usage() cmd.Usage()
os.Exit(1) os.Exit(1)
} }
seedFile := args[0] SeedFromFile(args[0])
afterMigration, err := time.Parse(time.RFC3339, args[1])
if err != nil {
fmt.Printf("ERROR: bad version string: %v", err)
os.Exit(1)
}
SeedFromFile(seedFile, types.MigrationVersion(afterMigration))
}, },
} }
@ -301,19 +293,13 @@ func MakeMigration(name, description string) {
// Applies a cloned db to the local db. // Applies a cloned db to the local db.
// Applies the seed after the migration specified in `afterMigration`. // Applies the seed after the migration specified in `afterMigration`.
// NOTE(asaf): The db role specified in the config must have the CREATEDB attribute! `ALTER ROLE hmn WITH CREATEDB;` // NOTE(asaf): The db role specified in the config must have the CREATEDB attribute! `ALTER ROLE hmn WITH CREATEDB;`
func SeedFromFile(seedFile string, afterMigration types.MigrationVersion) { func SeedFromFile(seedFile string) {
file, err := os.Open(seedFile) file, err := os.Open(seedFile)
if err != nil { if err != nil {
panic(fmt.Errorf("couldn't open seed file %s: %w", seedFile, err)) panic(fmt.Errorf("couldn't open seed file %s: %w", seedFile, err))
} }
file.Close() file.Close()
migration := migrations.All[afterMigration]
if migration == nil {
panic(fmt.Errorf("could not find migration: %s", afterMigration))
}
fmt.Println("Resetting database...") fmt.Println("Resetting database...")
{ {
ctx := context.Background() ctx := context.Background()
@ -348,20 +334,10 @@ func SeedFromFile(seedFile string, afterMigration types.MigrationVersion) {
} }
} }
fmt.Println("Running migrations...")
Migrate(afterMigration)
fmt.Println("Executing seed...") fmt.Println("Executing seed...")
cmd := exec.Command("pg_restore", cmd := exec.Command("pg_restore",
"--single-transaction", "--single-transaction",
"--dbname", "--dbname", config.Config.Postgres.DSN(),
config.Config.Postgres.DbName,
"--host",
config.Config.Postgres.Hostname,
"--username",
config.Config.Postgres.User,
"--password",
"--data-only",
seedFile, seedFile,
) )
fmt.Println("Running command:", cmd) fmt.Println("Running command:", cmd)

View File

@ -40,7 +40,7 @@ func (m ChangeHMNColors) Up(ctx context.Context, tx pgx.Tx) error {
return oops.New(err, "failed to update HMN colors") return oops.New(err, "failed to update HMN colors")
} }
if tag.RowsAffected() != 1 { if tag.RowsAffected() > 1 {
return oops.New(nil, "was supposed to update only HMN, but updated %d projects instead", tag.RowsAffected()) return oops.New(nil, "was supposed to update only HMN, but updated %d projects instead", tag.RowsAffected())
} }