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

Fails on schema inside mod #29

Open
Boscop opened this issue Nov 21, 2019 · 6 comments
Open

Fails on schema inside mod #29

Boscop opened this issue Nov 21, 2019 · 6 comments

Comments

@Boscop
Copy link

Boscop commented Nov 21, 2019

Please make it work with this:

pub mod foo {
    table! {
        ...
    }
}

Currently, it panics:

thread 'main' panicked at 'index out of bounds: the len is 1 but the index is 1'

@abbychau
Copy link
Owner

can you give me a full example?
test for mod is passed using test_data/schema_localmodded.rs

@abbychau
Copy link
Owner

abbychau commented Dec 5, 2019

i made a fix on 0.3.4. can you try it again?

@Boscop
Copy link
Author

Boscop commented Dec 11, 2019

Thanks for being so helpful :)
I just tried with 0.3.4 and I got the same panic.
But looking at test_data/schema_localmodded.rs I think I know why:
We have this format:

pub mod tenant {
    table! {
        use crate::diesel_types::org::*;

        asd (id) {
            id -> Int4,
            ...
        }
    }

    table! {
            ...
    }
}

but schema_localmodded.rs has the format tenant.asd (id) {.
Why do we have this format?
We are post-processing the output of diesel print-schema with | sed -e 's/my_schema\.//g' -e 's/my_schema/tenant/g' because we have a multi-tenant system where each tenant has a different schema in postgres at runtime, but they all use the same structural "schema", so we don't want to have a statically fixed tenant.asd table, because asd's model structs get used with different schemas at runtime, depending on which tenant we're dealing (we set the search_path accordingly).
I think the easiest way to make our use case work is if diesel_cli_ext worked when it gets an indented normal schema, because then, we could pre-process our schema file with sed (removing pub mod tenant { and } at the end, before piping it into diesel_cli_ext and it would work.
It would pass input in this format:

    table! {
        use crate::diesel_types::org::*;

        asd (id) {
            id -> Int4,
            ...
        }
    }

    table! {
            ...
    }

Currently it doesn't work for two reasons:

  • diesel_cli_ext doesn't accept stdin. It could be supported with -s -
  • When the schema is indented, it currently generates models like this:
#[derive(Debug, Queryable, QueryableByName, Insertable, PartialEq, Eq, Serialize, Deserialize, TypeName, Identifiable)]
#[primary_key(, , , access_controls, id)]
#[table_name = ""]
pub struct  {
    pub : /* TODO: unknown type  */,
    pub : /* TODO: unknown type  */,
    pub : /* TODO: unknown type  */,
    pub : /* TODO: unknown type  */,
    pub : /* TODO: unknown type  */,
    pub : /* TODO: unknown type  */,
}

Ideally, diesel_cli_ext would be completely indentation-invariant (should work no matter how indented the schema is), just like Rust code is..
What do you think? :)

@abbychau
Copy link
Owner

I have updated this to 0.3.5 so that

pub mod tenant {
    table! {
        use crate::diesel_types::org::*;

        asd (id) {
            id -> Int4,
            some_field -> Text,
        }
    }

    table! {
        my_other_schema.my_table (my_pk) {
            my_pk -> Int4,
            some_field -> Text,
        }
    }
}

will output

pub mod tenant {

    #[derive(Queryable, Debug, Identifiable)]
    pub struct Asd {
        pub id: i32,
        pub some_field: String,
    }

    #[derive(Queryable, Debug, Identifiable)]
    #[primary_key(my_pk)]
    pub struct myTable {
        pub my_pk: i32,
        pub some_field: String,
    }

}

Does it make sense to you?

@Boscop
Copy link
Author

Boscop commented Dec 23, 2019

We need this but without the surrounding pub mod tenant { } because it will already be piped into a file named tenant.rs.
But if it outputs this, we can work around it by stripping the first and last line by piping it through unix tools before piping it into the file..

@abbychau
Copy link
Owner

abbychau commented Sep 9, 2020

but without surrounding with pub mod, schema like schema_localmodded.rs in test_data may have naming collision.

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

No branches or pull requests

2 participants