Skip to content

add initial type mapping support for sqlite only - #5148

Draft
arthmis wants to merge 2 commits into
diesel-rs:mainfrom
arthmis:type_mapping
Draft

add initial type mapping support for sqlite only#5148
arthmis wants to merge 2 commits into
diesel-rs:mainfrom
arthmis:type_mapping

Conversation

@arthmis

@arthmis arthmis commented Aug 5, 2026

Copy link
Copy Markdown
  • I checked for similar changes and make sure to reference them
  • I included a changelog entry for relevant new features or changes

This adds basic type mapping support to sqlite. This is an implementation of this proposal #4113 (comment). The issue was referenced in these conversations #4192.

This only provides support for Sqlite as a starting point because, one I needed it, and two Sqlite is a lot more permissive with "types" so this would be a good starting point. Also wanted to gather feedback before making anymore broader changes to the other backends. This supports configuring output schema type for specific table, column, or schema or combination of them.

One major question I had was if it was worth it to add a CLI flag for this feature? Or if it is a requirement that there is a CLI flag for all configuration options. I opted to only have this work if using diesel.toml because it's easier to write the table of fields that way.

@weiznich
weiznich requested a review from a team August 7, 2026 11:04
@weiznich

weiznich commented Aug 7, 2026

Copy link
Copy Markdown
Member

Thanks for working on this. I did not have the capacity to review the changes this week, hopefully I find some time next week.

One major question I had was if it was worth it to add a CLI flag for this feature? Or if it is a requirement that there is a CLI flag for all configuration options.

If it's meaningful possible to support this as CLI flags as well, I would prefer having the CLI flag.

@arthmis

arthmis commented Aug 7, 2026

Copy link
Copy Markdown
Author

Thanks for working on this. I did not have the capacity to review the changes this week, hopefully I find some time next week.

No problem!

If it's meaningful possible to support this as CLI flags as well, I would prefer having the CLI flag.

I'll see what I can do there then.

@weiznich weiznich left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The basic approach looks right to me. I would strongly prefer having this for all supported backends. I would be fine if the initial implementation only supports renaming all instances of a type, as we always can extend this later to be more sophisticated.

Comment on lines +376 to +396
let Some(type_mappings) = &config.type_map else {
return None;
};

let Some(type_map) = type_mappings.iter().find(|map| {
map.database_typename.to_lowercase() == type_name
&& map
.table_name
.as_deref()
.is_none_or(|t| t == table.sql_name)
&& map
.schema_name
.as_deref()
.is_none_or(|s| Some(s) == table.schema.as_deref())
&& map
.column_name
.as_deref()
.is_none_or(|c| c == attr.column_name)
}) else {
return None;
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to have this rather as function on SchemaTypeMap instead so that it can be shared between all backends.

Something like:

impl SchemaTypeMap {
    fn map_type(&self, db_type: &str, table_name: &str, schema_name: Option<&str>, column_name: &str) -> &str {…}
}

Comment thread diesel_cli/src/config.rs
Comment on lines +532 to 535
pub table_name: Option<String>,
pub schema_name: Option<String>,
pub column_name: Option<String>,
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Matching on these is currently not implemented, would be great to have that, although that's not a requirement for the initial PR to land.

If you decide not to implement this, I would rather drop these fields for now. We always can add them later.

Comment thread .zed/debug.json

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file shouldn't be there


#[test]
#[cfg(feature = "sqlite")]
fn print_schema_type_mapping() {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do want to have a generate_migration test as well, as we need to reverse that mapping there

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants