Skip to content

Feature Request: Add support for sqlx(flatten): #42

Open

Description

@poliorcetics

Right now atmosphere doesn't support sqlx(flatten) which prevents lots of code reuse.

I would like the following test to work transparently (or maybe needing an sql(flattened) attribute of its own, that's fine):

CREATE TABLE scientists (
    id     INT PRIMARY KEY,
    name   TEXT NOT NULL,
    domain TEXT NOT NULL
);
use atmosphere::prelude::*;
use atmosphere_core::Table;
use sqlx::prelude::{FromRow, Type};

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
#[table(name = "scientists", schema = "public")]
struct ScientistEntry {
    #[sql(pk)]
    id: i32,
    #[sqlx(flatten)]
    scientist: Scientist,
}

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, FromRow, Type)]
struct Scientist {
    name: String,
    domain: String,
}

impl Scientist {
    pub fn new(name: &str, domain: &str) -> Self {
        Self {
            name: name.into(),
            domain: domain.into(),
        }
    }
}

#[sqlx::test(migrations = "tests/flatten/migrations")]
async fn create(pool: sqlx::PgPool) {
    atmosphere::testing::create(
        &pool,
        ScientistEntry {
            id: 0,
            scientist: Scientist::new("Rosalyn Yalow", "physiology and medicine"),
        },
    )
    .await;

    ScientistEntry {
        id: 1,
        scientist: Scientist::new("Chien-Shiung Wu", "physics"),
    }
    .upsert(&pool)
    .await
    .unwrap();
}

#[sqlx::test(migrations = "tests/flatten/migrations")]
async fn read(pool: sqlx::PgPool) {
    atmosphere::testing::read(
        &pool,
        ScientistEntry {
            id: 0,
            scientist: Scientist::new("Fanny Bullock Workman", "geography and exploration"),
        },
    )
    .await;
}

#[sqlx::test(migrations = "tests/flatten/migrations")]
async fn update(pool: sqlx::PgPool) {
    atmosphere::testing::update(
        &pool,
        ScientistEntry {
            id: 0,
            scientist: Scientist::new("Elisabeth S. Vrba", "paleontology"),
        },
        vec![
            ScientistEntry {
                id: 0,
                scientist: Scientist::new("Lydia Villa-Komaroff", "molecular biology"),
            },
            ScientistEntry {
                id: 0,
                scientist: Scientist::new("Trota of Salerno", "medicine"),
            },
        ],
    )
    .await;
}

#[sqlx::test(migrations = "tests/flatten/migrations")]
async fn delete(pool: sqlx::PgPool) {
    atmosphere::testing::delete(
        &pool,
        ScientistEntry {
            id: 0,
            scientist: Scientist::new("Sheila Tobias", "mathematics and science education"),
        },
    )
    .await;
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions