Skip to content

Instantly share code, notes, and snippets.

@pdewilde
Created May 17, 2023 22:50
Show Gist options
  • Save pdewilde/360869733139538fe01442b550c4e363 to your computer and use it in GitHub Desktop.
Save pdewilde/360869733139538fe01442b550c4e363 to your computer and use it in GitHub Desktop.
Example user journey of new pop user discovering an issue with timezones
## Cleanup from previous run
rm -rf /tmp/popflow
### Flow of a new Pop User
# Install the pop CLI https://gobuffalo.io/pt/documentation/database/soda/
go install github.com/gobuffalo/pop/v6/soda@latest
## Make a directory
mkdir /tmp/popflow
cd /tmp/popflow
# set up project
go mod init popflow
# generate the database config
soda g config
# realize its easier to just overwrite config
cat << EOF > database.yml
development:
dialect: "postgres"
database: "postgres"
user: "myusername"
password: "mypassword"
host: "localhost"
port: "7777"
EOF
# use the generator to make a simple user model
soda generate model user name:string age:int
# Files created
# models/user.go
# models/user_test.go
# migrations/{timestamp}_create_users.down.fizz
# migrations/{timestamp}_create_users.up.fizz
# Add a simple main:
cat <<EOF > main.go
package main
import (
"encoding/json"
"fmt"
"github.com/gobuffalo/pop/v6"
"popflow/models"
)
func main() {
println("Connecting to database")
tx, err := pop.Connect("development")
if err != nil {
panic(err)
}
// Make a user
user := models.User{Name: "bob", Age: 19}
// Create the user
err = tx.Create(&user)
if err != nil {
panic(err)
}
// Find our new user
queriedUser := models.User{}
err = tx.Last(&queriedUser) // Only one user in our DB, so this works
if err != nil {
panic(err)
}
// Convert the user to json and print out
userStr, err := json.Marshal(queriedUser)
if err != nil {
panic(err)
}
fmt.Printf("Added user: %v", string(userStr))
}
EOF
# download dependencies
go mod tidy
# Start up a database matching your config wait for it to start up
docker run --name postgresql-experiment -e POSTGRES_USER=myusername -e POSTGRES_PASSWORD=mypassword -p 7777:5432 -d postgres && sleep 10
# run the generated migration
soda migrate
# run the program
go run main.go
## OUTPUT:
# Added user: {"id":"86523e1a-cf77-48b1-abc4-f2d774884581","name":"bob","age":19,"created_at":"2023-05-16T11:36:20.920415Z","updated_at":"2023-05-16T11:36:20.920419Z"}postgresql-experiment
# note that while timestamps are in Z, its actually local time (11:36 PDT May 16th when I ran this)
# tz info was destroyed, so the go time.time that is desearalized from the db thinks its GMT+0 but the timestamp is actually for GMT-7
# setting the database timezone to UTC before addign records did not fix the issue: "ALTER DATABASE postgres set TIMEZONE to 'posix/Universal';"
# stop the database
docker stop postgresql-experiment
docker rm postgresql-experiment
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment