Skip to content

Commit 696e4ed

Browse files
committed
test: change "custom_model_and_schema_path" to be compile-able
1 parent f4ed47e commit 696e4ed

File tree

12 files changed

+218
-2
lines changed

12 files changed

+218
-2
lines changed

Cargo.lock

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,6 @@ members = [
5252
"test/simple_table_pg",
5353
"test/simple_table_mysql",
5454
"test/simple_table_sqlite",
55+
"test/custom_model_and_schema_path",
5556
]
5657
resolver = "2"
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
[lib]
2+
path = "lib.rs"
3+
4+
[package]
5+
name = "custom_model_and_schema_path"
6+
version = "0.1.0"
7+
edition = "2021"
8+
9+
[dependencies]
10+
diesel = { version = "*", default-features = false, features = [
11+
"postgres",
12+
"chrono",
13+
] }
14+
dsync = { path = "../../" }
15+
chrono = { version = "*", features = ["serde"] }
16+
serde = { version = "*", features = ["derive"] }
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod models;
2+
pub mod schema;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod table_a;
2+
pub mod table_b;
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/* @generated and managed by dsync */
2+
3+
use crate::diesel::*;
4+
use crate::data::schema::*;
5+
use diesel::QueryResult;
6+
7+
pub type ConnectionType = diesel::prelude::PgConnection;
8+
9+
/// Struct representing a row in table `tableA`
10+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Queryable, Selectable, QueryableByName)]
11+
#[diesel(table_name=tableA, primary_key(_id))]
12+
pub struct TableA {
13+
/// Field representing column `_id`
14+
pub _id: i32,
15+
}
16+
17+
/// Create Struct for a row in table `tableA` for [`TableA`]
18+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Insertable)]
19+
#[diesel(table_name=tableA)]
20+
pub struct CreateTableA {
21+
/// Field representing column `_id`
22+
pub _id: i32,
23+
}
24+
25+
/// Result of a `.paginate` function
26+
#[derive(Debug, serde::Serialize)]
27+
pub struct PaginationResult<T> {
28+
/// Resulting items that are from the current page
29+
pub items: Vec<T>,
30+
/// The count of total items there are
31+
pub total_items: i64,
32+
/// Current page, 0-based index
33+
pub page: i64,
34+
/// Size of a page
35+
pub page_size: i64,
36+
/// Number of total possible pages, given the `page_size` and `total_items`
37+
pub num_pages: i64,
38+
}
39+
40+
impl TableA {
41+
/// Insert a new row into `tableA` with a given [`CreateTableA`]
42+
pub fn create(db: &mut ConnectionType, item: &CreateTableA) -> QueryResult<Self> {
43+
use crate::data::schema::tableA::dsl::*;
44+
45+
insert_into(tableA).values(item).get_result::<Self>(db)
46+
}
47+
48+
/// Get a row from `tableA`, identified by the primary key
49+
pub fn read(db: &mut ConnectionType, param__id: i32) -> QueryResult<Self> {
50+
use crate::data::schema::tableA::dsl::*;
51+
52+
tableA.filter(_id.eq(param__id)).first::<Self>(db)
53+
}
54+
55+
/// Paginates through the table where page is a 0-based index (i.e. page 0 is the first page)
56+
pub fn paginate(db: &mut ConnectionType, page: i64, page_size: i64) -> QueryResult<PaginationResult<Self>> {
57+
use crate::data::schema::tableA::dsl::*;
58+
59+
let page_size = if page_size < 1 { 1 } else { page_size };
60+
let total_items = tableA.count().get_result(db)?;
61+
let items = tableA.limit(page_size).offset(page * page_size).load::<Self>(db)?;
62+
63+
Ok(PaginationResult {
64+
items,
65+
total_items,
66+
page,
67+
page_size,
68+
/* ceiling division of integers */
69+
num_pages: total_items / page_size + i64::from(total_items % page_size != 0)
70+
})
71+
}
72+
73+
/// Delete a row in `tableA`, identified by the primary key
74+
pub fn delete(db: &mut ConnectionType, param__id: i32) -> QueryResult<usize> {
75+
use crate::data::schema::tableA::dsl::*;
76+
77+
diesel::delete(tableA.filter(_id.eq(param__id))).execute(db)
78+
}
79+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod generated;
2+
pub use generated::*;
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/* @generated and managed by dsync */
2+
3+
use crate::diesel::*;
4+
use crate::data::models::table_a::TableA;
5+
use crate::data::schema::*;
6+
use diesel::QueryResult;
7+
8+
pub type ConnectionType = diesel::prelude::PgConnection;
9+
10+
/// Struct representing a row in table `tableB`
11+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Queryable, Selectable, QueryableByName, Associations, Identifiable)]
12+
#[diesel(table_name=tableB, primary_key(_id), belongs_to(TableA, foreign_key=link))]
13+
pub struct TableB {
14+
/// Field representing column `_id`
15+
pub _id: i32,
16+
/// Field representing column `link`
17+
pub link: i32,
18+
}
19+
20+
/// Create Struct for a row in table `tableB` for [`TableB`]
21+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, Insertable)]
22+
#[diesel(table_name=tableB)]
23+
pub struct CreateTableB {
24+
/// Field representing column `_id`
25+
pub _id: i32,
26+
/// Field representing column `link`
27+
pub link: i32,
28+
}
29+
30+
/// Update Struct for a row in table `tableB` for [`TableB`]
31+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize, AsChangeset, PartialEq, Default)]
32+
#[diesel(table_name=tableB)]
33+
pub struct UpdateTableB {
34+
/// Field representing column `link`
35+
pub link: Option<i32>,
36+
}
37+
38+
/// Result of a `.paginate` function
39+
#[derive(Debug, serde::Serialize)]
40+
pub struct PaginationResult<T> {
41+
/// Resulting items that are from the current page
42+
pub items: Vec<T>,
43+
/// The count of total items there are
44+
pub total_items: i64,
45+
/// Current page, 0-based index
46+
pub page: i64,
47+
/// Size of a page
48+
pub page_size: i64,
49+
/// Number of total possible pages, given the `page_size` and `total_items`
50+
pub num_pages: i64,
51+
}
52+
53+
impl TableB {
54+
/// Insert a new row into `tableB` with a given [`CreateTableB`]
55+
pub fn create(db: &mut ConnectionType, item: &CreateTableB) -> QueryResult<Self> {
56+
use crate::data::schema::tableB::dsl::*;
57+
58+
insert_into(tableB).values(item).get_result::<Self>(db)
59+
}
60+
61+
/// Get a row from `tableB`, identified by the primary key
62+
pub fn read(db: &mut ConnectionType, param__id: i32) -> QueryResult<Self> {
63+
use crate::data::schema::tableB::dsl::*;
64+
65+
tableB.filter(_id.eq(param__id)).first::<Self>(db)
66+
}
67+
68+
/// Paginates through the table where page is a 0-based index (i.e. page 0 is the first page)
69+
pub fn paginate(db: &mut ConnectionType, page: i64, page_size: i64) -> QueryResult<PaginationResult<Self>> {
70+
use crate::data::schema::tableB::dsl::*;
71+
72+
let page_size = if page_size < 1 { 1 } else { page_size };
73+
let total_items = tableB.count().get_result(db)?;
74+
let items = tableB.limit(page_size).offset(page * page_size).load::<Self>(db)?;
75+
76+
Ok(PaginationResult {
77+
items,
78+
total_items,
79+
page,
80+
page_size,
81+
/* ceiling division of integers */
82+
num_pages: total_items / page_size + i64::from(total_items % page_size != 0)
83+
})
84+
}
85+
86+
/// Update a row in `tableB`, identified by the primary key with [`UpdateTableB`]
87+
pub fn update(db: &mut ConnectionType, param__id: i32, item: &UpdateTableB) -> QueryResult<Self> {
88+
use crate::data::schema::tableB::dsl::*;
89+
90+
diesel::update(tableB.filter(_id.eq(param__id))).set(item).get_result(db)
91+
}
92+
93+
/// Delete a row in `tableB`, identified by the primary key
94+
pub fn delete(db: &mut ConnectionType, param__id: i32) -> QueryResult<usize> {
95+
use crate::data::schema::tableB::dsl::*;
96+
97+
diesel::delete(tableB.filter(_id.eq(param__id))).execute(db)
98+
}
99+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub mod generated;
2+
pub use generated::*;
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#![allow(non_snake_case)]
2+
pub mod data;

0 commit comments

Comments
 (0)