Skip to content

Commit 07c6cef

Browse files
authored
Merge pull request diesel-rs#1110 from Eijebong/auto_renaming
Automatically rename columns which name would collide with a keyword
2 parents b429c9f + ce056c6 commit 07c6cef

20 files changed

Lines changed: 204 additions & 10 deletions

File tree

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/
2929

3030
* `infer_schema!` will now automatically detect which tables can be joined based
3131
on the presence of foreign key constraints.
32-
32+
3333
* Added support for `Add` and `Sub` to timestamp types.
3434

3535
* Added a way to rename columns in the table macro with `#[sql_name="the_column_name"]`
@@ -39,6 +39,11 @@ for Rust libraries in [RFC #1105](https://github.com/rust-lang/rfcs/blob/master/
3939
Diesel's CLI tool, pass the new `--with-docs` parameter:
4040
`diesel print-schema --with-docs`.
4141

42+
* infer_schema! now automatically renames columns that conflict with
43+
a Rust keyword by placing a _ at the end of the name. For example,
44+
a column called type will be referenced as type_ in Rust.
45+
46+
4247
### Changed
4348

4449
* The deprecated `debug_sql!` and `print_sql!` functions will now generate

diesel/src/macros/macros_from_codegen.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,12 @@
99
/// limitations of the Macros 1.1 system, but you can pass a string in the form
1010
/// `"env:SOME_ENV_VAR"` or `"dotenv:SOME_ENV_VAR"` to achieve the same effect.
1111
///
12-
/// This macro can only be used in combination with the `diesel_codegen` or
13-
/// `diesel_codegen_syntex` crates. It will not work on its own.
12+
/// This macro can only be used in combination with the `diesel_codegen` crate.
13+
/// It will not work on its own.
14+
///
15+
/// If any column name would collide with a rust keyword, a `_` will
16+
/// automatically be placed at the end of the name. For example, a column called
17+
/// `type` will be referenced as `type_` in the generated module.
1418
macro_rules! infer_schema {
1519
($database_url: expr) => {
1620
mod __diesel_infer_schema {

diesel_cli/src/print_schema.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,12 @@ impl<'a> Display for ColumnDefinitions<'a> {
155155
writeln!(out, "///{}{}", if d.is_empty() { "" } else { " " }, d)?;
156156
}
157157
}
158-
writeln!(out, "{} -> {},", column.name, column.ty)?;
158+
if let Some(ref rust_name) = column.rust_name {
159+
writeln!(out, r#"#[sql_name = {}]"#, column.sql_name)?;
160+
writeln!(out, "{} -> {},", rust_name, column.ty)?;
161+
} else {
162+
writeln!(out, "{} -> {},", column.sql_name, column.ty)?;
163+
}
159164
}
160165
}
161166
writeln!(f, "}}")?;

diesel_cli/tests/print_schema.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ fn print_schema_with_foreign_keys() {
4646
test_print_schema("print_schema_with_foreign_keys", vec!["--with-docs"]);
4747
}
4848

49+
#[test]
50+
fn print_schema_column_renaming() {
51+
test_print_schema("print_schema_column_renaming", vec!["--with-docs"]);
52+
}
53+
4954
#[cfg(feature = "sqlite")]
5055
const BACKEND: &str = "sqlite";
5156
#[cfg(feature = "postgres")]
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
table! {
2+
/// Representation of the `with_keywords` table.
3+
///
4+
/// (Automatically generated by Diesel.)
5+
with_keywords (fn_) {
6+
/// The `fn` column of the `with_keywords` table.
7+
///
8+
/// Its SQL type is `Integer`.
9+
///
10+
/// (Automatically generated by Diesel.)
11+
#[sql_name = fn]
12+
fn_ -> Integer,
13+
/// The `let` column of the `with_keywords` table.
14+
///
15+
/// Its SQL type is `Integer`.
16+
///
17+
/// (Automatically generated by Diesel.)
18+
#[sql_name = let]
19+
let_ -> Integer,
20+
/// The `extern` column of the `with_keywords` table.
21+
///
22+
/// Its SQL type is `Integer`.
23+
///
24+
/// (Automatically generated by Diesel.)
25+
#[sql_name = extern]
26+
extern_ -> Integer,
27+
}
28+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE with_keywords (
2+
fn INTEGER NOT NULL PRIMARY KEY,
3+
let INTEGER NOT NULL,
4+
extern INTEGER NOT NULL
5+
);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
table! {
2+
/// Representation of the `with_keywords` table.
3+
///
4+
/// (Automatically generated by Diesel.)
5+
with_keywords (fn_) {
6+
/// The `fn` column of the `with_keywords` table.
7+
///
8+
/// Its SQL type is `Int4`.
9+
///
10+
/// (Automatically generated by Diesel.)
11+
#[sql_name = fn]
12+
fn_ -> Int4,
13+
/// The `let` column of the `with_keywords` table.
14+
///
15+
/// Its SQL type is `Int4`.
16+
///
17+
/// (Automatically generated by Diesel.)
18+
#[sql_name = let]
19+
let_ -> Int4,
20+
/// The `extern` column of the `with_keywords` table.
21+
///
22+
/// Its SQL type is `Int4`.
23+
///
24+
/// (Automatically generated by Diesel.)
25+
#[sql_name = extern]
26+
extern_ -> Int4,
27+
}
28+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE with_keywords (
2+
fn INTEGER PRIMARY KEY,
3+
let INTEGER NOT NULL,
4+
extern INTEGER NOT NULL
5+
);
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
table! {
2+
/// Representation of the `with_keywords` table.
3+
///
4+
/// (Automatically generated by Diesel.)
5+
with_keywords (fn_) {
6+
/// The `fn` column of the `with_keywords` table.
7+
///
8+
/// Its SQL type is `Integer`.
9+
///
10+
/// (Automatically generated by Diesel.)
11+
#[sql_name = fn]
12+
fn_ -> Integer,
13+
/// The `let` column of the `with_keywords` table.
14+
///
15+
/// Its SQL type is `Integer`.
16+
///
17+
/// (Automatically generated by Diesel.)
18+
#[sql_name = let]
19+
let_ -> Integer,
20+
/// The `extern` column of the `with_keywords` table.
21+
///
22+
/// Its SQL type is `Integer`.
23+
///
24+
/// (Automatically generated by Diesel.)
25+
#[sql_name = extern]
26+
extern_ -> Integer,
27+
}
28+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
CREATE TABLE with_keywords (
2+
fn INTEGER NOT NULL PRIMARY KEY,
3+
let INTEGER NOT NULL,
4+
extern INTEGER NOT NULL
5+
);

0 commit comments

Comments
 (0)