From 72bd08225fee181bcc97e876810b539cc37c3615 Mon Sep 17 00:00:00 2001 From: "D. Scott Boggs" Date: Mon, 26 Jun 2023 11:36:16 -0400 Subject: [PATCH] Add users-to-tracks many-to-many association --- .../m20230626_083036_create_users_table.rs | 2 +- ...30626_150551_associate_users_and_tracks.rs | 57 +++++++++++++++++++ server/src/migrator/mod.rs | 2 + 3 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 server/src/migrator/m20230626_150551_associate_users_and_tracks.rs diff --git a/server/src/migrator/m20230626_083036_create_users_table.rs b/server/src/migrator/m20230626_083036_create_users_table.rs index 50ab989..6c8e7b7 100644 --- a/server/src/migrator/m20230626_083036_create_users_table.rs +++ b/server/src/migrator/m20230626_083036_create_users_table.rs @@ -34,7 +34,7 @@ impl MigrationTrait for Migration { /// Learn more at https://docs.rs/sea-query#iden #[derive(Iden)] -enum User { +pub(crate) enum User { Table, Id, Name, diff --git a/server/src/migrator/m20230626_150551_associate_users_and_tracks.rs b/server/src/migrator/m20230626_150551_associate_users_and_tracks.rs new file mode 100644 index 0000000..186de49 --- /dev/null +++ b/server/src/migrator/m20230626_150551_associate_users_and_tracks.rs @@ -0,0 +1,57 @@ +use super::{ + m20230606_000001_create_tracks_table::Tracks, m20230626_083036_create_users_table::User, +}; +use sea_orm_migration::prelude::*; + +#[derive(DeriveMigrationName)] +pub struct Migration; + +#[async_trait::async_trait] +impl MigrationTrait for Migration { + async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .create_table( + Table::create() + .table(UserTracks::Table) + .if_not_exists() + .col( + ColumnDef::new(UserTracks::Id) + .integer() + .not_null() + .primary_key() + .auto_increment(), + ) + .col(ColumnDef::new(UserTracks::UserId).integer().not_null()) + .col(ColumnDef::new(UserTracks::TrackId).integer().not_null()) + .foreign_key( + ForeignKey::create() + .name("fk-user_tracks-user_id") + .from(UserTracks::Table, UserTracks::UserId) + .to(User::Table, User::Id), + ) + .foreign_key( + ForeignKey::create() + .name("fk-user_tracks-track_id") + .from(UserTracks::Table, UserTracks::TrackId) + .to(Tracks::Table, Tracks::Id), + ) + .to_owned(), + ) + .await + } + + async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { + manager + .drop_table(Table::drop().table(UserTracks::Table).to_owned()) + .await + } +} + +/// Learn more at https://docs.rs/sea-query#iden +#[derive(Iden)] +enum UserTracks { + Table, + Id, + UserId, + TrackId, +} diff --git a/server/src/migrator/mod.rs b/server/src/migrator/mod.rs index 6d4f915..edf6a1f 100644 --- a/server/src/migrator/mod.rs +++ b/server/src/migrator/mod.rs @@ -3,6 +3,7 @@ mod m20230606_000002_create_ticks_table; mod m20230606_000003_create_groups_table; mod m20230606_000004_create_track2groups_table; mod m20230626_083036_create_users_table; +mod m20230626_150551_associate_users_and_tracks; use sea_orm_migration::prelude::*; @@ -17,6 +18,7 @@ impl MigratorTrait for Migrator { Box::new(m20230606_000003_create_groups_table::Migration), Box::new(m20230606_000004_create_track2groups_table::Migration), Box::new(m20230626_083036_create_users_table::Migration), + Box::new(m20230626_150551_associate_users_and_tracks::Migration), ] } }