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

Test database interaction in CI #5

Merged
merged 4 commits into from
Sep 20, 2022
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
84 changes: 80 additions & 4 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
name: Push action
on: [push]
on:
push:
branches:
- master
pull_request:
jobs:
test:
runs-on: ${{ matrix.os }}
Expand All @@ -18,12 +22,12 @@ jobs:
components: clippy
profile: minimal

- uses: Swatinem/rust-cache@v1
- uses: Swatinem/rust-cache@v2

- name: Test and clippy (main)
run: cargo clippy -- -Dwarnings && cargo test --verbose && cargo check --no-default-features

examples:
example-lint:
runs-on: ${{ matrix.os }}
strategy:
matrix:
Expand All @@ -40,12 +44,84 @@ jobs:
components: clippy
profile: minimal

- uses: Swatinem/rust-cache@v1
- uses: Swatinem/rust-cache@v2

- name: clippy (examples)
working-directory: examples
run: cargo clippy -- -Dwarnings && cargo check --verbose

example-sqlite:
runs-on: ${{ matrix.os }}
strategy:
matrix:
os: [ubuntu-latest]
rust: [stable]

steps:
- uses: actions/checkout@v2

- uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
override: true
profile: minimal

- uses: Swatinem/rust-cache@v2

- name: "Test example server"
working-directory: examples/axum-example
# This should really be a Python script or something, but this is fine for now.
run: |
cargo build
cargo run &
sleep 5
curl http://localhost:3000 --cookie .cookies --cookie-jar .cookies
curl http://localhost:3000 --cookie .cookies --cookie-jar .cookies > output
test "$(cat output)" -eq "2"

example-postgres:
# services require linux runner
runs-on: ubuntu-latest
strategy:
matrix:
rust: [stable]

services:
postgres:
image: postgres
env:
POSTGRES_DB: axum_example
POSTGRES_PASSWORD: postgres
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
ports:
- 5432:5432

steps:
- uses: actions/checkout@v2

- uses: actions-rs/toolchain@v1
with:
toolchain: ${{ matrix.rust }}
override: true
profile: minimal

- uses: Swatinem/rust-cache@v2

- name: "Test example server"
working-directory: examples/axum-example
# This should really be a Python script or something, but this is fine for now.
run: |
cargo build --features postgres
DATABASE_URI=postgres://postgres:postgres@localhost/axum_example cargo run --features postgres &
sleep 5
curl http://localhost:3000 --cookie .cookies --cookie-jar .cookies
curl http://localhost:3000 --cookie .cookies --cookie-jar .cookies > output
test "$(cat output)" -eq "2"

fmt:
runs-on: ubuntu-latest

Expand Down
18 changes: 13 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,23 @@ async fn main() -> Result<(), sea_orm::DbErr> {

## Examples

There are full examples in the `examples` directory of the repository.
There are full examples in the `examples` directory of the repository. Feel free to contribute examples showing
different setups!

- **axum-example**

This example combines the [axum](https://github.com/tokio-rs/axum) web application
framework with `async-sea-orm-session` for session storage and [tower-cookies](https://github.com/imbolc/tower-cookies)
for cookie management.
This example combines the [axum](https://github.com/tokio-rs/axum) web application
framework with `async-sea-orm-session` for session storage and [tower-cookies](https://github.com/imbolc/tower-cookies)
for cookie management.

By default, this example runs using an in-memory sqlite database. The
example can also be run using a postgres database by running the following
from the `axum-example` subdirectory:

```shell
DATABASE_URI=postgres://username:password@host/database cargo run --features postgres
```

Feel free to contribute examples showing different setups!

## License

Expand Down
9 changes: 7 additions & 2 deletions examples/axum-example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,14 @@ edition = "2021"
anyhow = "1.0"
async-sea-orm-session = { path = "../.." }
axum = "0.5"
sea-orm = { version = "0.9", features = ["runtime-tokio-rustls", "sqlx-sqlite"] }
sea-orm = { version = "0.9", features = ["runtime-tokio-rustls"] }
serde = { version = "1.0", features = ["derive"] }
tokio = { version = "1.19", features = ["rt", "macros"] }
tower-cookies = "0.7"
tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing-subscriber = { version = "0.3", features = ["env-filter"] }

[features]
default = ["sqlite"]
postgres = ["sea-orm/sqlx-postgres"]
sqlite = ["sea-orm/sqlx-sqlite"]
5 changes: 4 additions & 1 deletion examples/axum-example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ async fn main() -> Result<()> {
.init();

// We use an in-memory sqlite database for the purposes of this example.
let db: DatabaseConnection = Database::connect("sqlite::memory:").await?;
let db: DatabaseConnection = Database::connect(
std::env::var("DATABASE_URI").unwrap_or_else(|_| "sqlite::memory:".to_string()),
)
.await?;

// Create the store and create the tables required for storing session information.
let store = DatabaseSessionStore::new(db.clone());
Expand Down
2 changes: 1 addition & 1 deletion src/sessions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel)]
#[sea_orm(table_name = "sessions")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false, column_type = "Text")]
Expand Down