feat: added sync group table and models
parent
320a46c0f3
commit
4a45bebc9f
|
@ -24,6 +24,9 @@ create table devices (
|
|||
user_id bigint not null
|
||||
references users (id)
|
||||
on delete cascade,
|
||||
sync_group_id bigint
|
||||
references sync_group (id)
|
||||
on delete set null,
|
||||
|
||||
caption text not null,
|
||||
type text not null,
|
||||
|
@ -31,6 +34,10 @@ create table devices (
|
|||
unique (user_id, device_id)
|
||||
);
|
||||
|
||||
create table sync_groups (
|
||||
id integer primary key not null
|
||||
);
|
||||
|
||||
create table device_subscriptions (
|
||||
id integer primary key not null,
|
||||
|
||||
|
|
|
@ -5,10 +5,12 @@ mod schema;
|
|||
use diesel::connection::InstrumentationEvent;
|
||||
use diesel::r2d2::CustomizeConnection;
|
||||
use diesel::Connection;
|
||||
|
||||
pub use models::device::{Device, DeviceType, NewDevice};
|
||||
pub use models::device_subscription::{DeviceSubscription, NewDeviceSubscription};
|
||||
pub use models::episode_action::{ActionType, EpisodeAction, NewEpisodeAction};
|
||||
pub use models::session::Session;
|
||||
pub use models::sync_group::SyncGroup;
|
||||
pub use models::user::{NewUser, User};
|
||||
|
||||
pub use repository::SqliteRepository;
|
||||
|
|
|
@ -21,6 +21,7 @@ pub struct Device {
|
|||
pub user_id: i64,
|
||||
pub caption: String,
|
||||
pub type_: DeviceType,
|
||||
pub sync_group_id: Option<i64>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Insertable)]
|
||||
|
|
|
@ -2,4 +2,5 @@ pub mod device;
|
|||
pub mod device_subscription;
|
||||
pub mod episode_action;
|
||||
pub mod session;
|
||||
pub mod sync_group;
|
||||
pub mod user;
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
use diesel::{
|
||||
dsl::{exists, not},
|
||||
prelude::*,
|
||||
};
|
||||
|
||||
use crate::db::schema::*;
|
||||
|
||||
#[derive(Queryable, Selectable)]
|
||||
#[diesel(table_name = sync_groups)]
|
||||
#[diesel(check_for_backend(diesel::sqlite::Sqlite))]
|
||||
pub struct SyncGroup {
|
||||
pub id: i64,
|
||||
}
|
||||
|
||||
impl SyncGroup {
|
||||
pub fn new(conn: &mut SqliteConnection) -> QueryResult<Self> {
|
||||
diesel::insert_into(sync_groups::table)
|
||||
.default_values()
|
||||
.returning(SyncGroup::as_returning())
|
||||
.get_result(conn)
|
||||
}
|
||||
|
||||
pub fn remove_unused(conn: &mut SqliteConnection) -> QueryResult<usize> {
|
||||
diesel::delete(
|
||||
sync_groups::table.filter(not(exists(
|
||||
devices::table
|
||||
.select(1.into_sql::<diesel::sql_types::Integer>())
|
||||
.filter(devices::sync_group_id.eq(sync_groups::id.nullable())),
|
||||
))),
|
||||
)
|
||||
.execute(conn)
|
||||
}
|
||||
}
|
|
@ -15,6 +15,7 @@ diesel::table! {
|
|||
id -> BigInt,
|
||||
device_id -> Text,
|
||||
user_id -> BigInt,
|
||||
sync_group_id -> Nullable<BigInt>,
|
||||
caption -> Text,
|
||||
#[sql_name = "type"]
|
||||
type_ -> Text,
|
||||
|
@ -45,6 +46,12 @@ diesel::table! {
|
|||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
sync_groups (id) {
|
||||
id -> BigInt,
|
||||
}
|
||||
}
|
||||
|
||||
diesel::table! {
|
||||
users (id) {
|
||||
id -> BigInt,
|
||||
|
@ -64,5 +71,6 @@ diesel::allow_tables_to_appear_in_same_query!(
|
|||
devices,
|
||||
episode_actions,
|
||||
sessions,
|
||||
sync_groups,
|
||||
users,
|
||||
);
|
||||
|
|
Loading…
Reference in New Issue