πŸ”¬ Working sample of UTC timestamps with Rust/Diesel/Postgres
Switch branches/tags
Nothing to show
Clone or download
Latest commit 87354e6 Feb 13, 2018
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
migrations 🌱 Feb 4, 2018
src 🌱 Feb 4, 2018
.gitignore 🌱 Feb 4, 2018
Cargo.lock 🌱 Feb 4, 2018
Cargo.toml 🌱 Feb 4, 2018
README.md πŸ“š Feb 12, 2018

README.md

Timestamps with Diesel.rs & Postgres

Example using UTC timestamps with the Rust language database ORM Diesel.

πŸŒ±πŸ’š Please note, I am new to Rust. I could not find a clear example for this basic task, so here's what I figured out. No guarantees about correctness. Please open an issue to give feedback. I'd love to find better ways to do this.

Requirements

Usage

export DATABASE_URL=postgres://postgres@localhost:5432/diesel-pg-timestamp-usage
diesel setup

Now, build & run the program:

cargo run

🏁 Each time it's run, a new example record will be inserted with the current timestamp. You should see output like:

Inserted example: Example { id: 3, created_at: 2018-02-04T19:35:20.404570, updated_at: Some(2018-02-04T19:35:20.404570) }

Architecture

As the Diesel Getting Started guide recommends:

  • src/migrations/
    • SQL commands to setup the database
    • generated by diesel migration generate $NAME
    • executed by diesel migration run
  • src/schema.rs
    • the typed database schema
    • generated by diesel print-schema
  • src/models.rs
    • structs that represent Diesel records in Rust
    • created by hand

And for this example,

  • src/main.rs
    • the high-level database connection & query program

Original setup

export DATABASE_URL=postgres://postgres@localhost:5432/diesel-pg-timestamp-usage
diesel setup
diesel migration generate create-example
# Write code in `migrations/up.sql` & `down.sql`
diesel migration run
diesel migration redo
diesel print-schema > src/schema.rs
# Write code in `src/models.rs`
# Write code in `src/main.rs`