Skip to content

Commit

Permalink
Create migrations for video tables
Browse files Browse the repository at this point in the history
  • Loading branch information
Aleksbgbg committed Feb 18, 2024
1 parent 61d33e2 commit 602da88
Show file tree
Hide file tree
Showing 10 changed files with 465 additions and 3 deletions.
3 changes: 3 additions & 0 deletions backend-rs/streamfox/src/models.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@ pub mod base;
pub mod id;
pub mod migrations;
pub mod user;
pub mod video;
pub mod view;
pub mod watch;
3 changes: 3 additions & 0 deletions backend-rs/streamfox/src/models/migrations.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
pub mod m20240204_000001_users;
pub mod m20240218_000002_videos;
pub mod m20240218_000003_views;
pub mod m20240218_000004_watches;
pub mod migrator;
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use crate::models::migrations::m20240204_000001_users::User;
use sea_orm_migration::prelude::*;

pub struct Migration;

impl MigrationName for Migration {
fn name(&self) -> &str {
"m_20240218_000002_videos"
}
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Video::Table)
.col(
ColumnDef::new(Video::Id)
.big_integer()
.not_null()
.primary_key(),
)
.col(
ColumnDef::new(Video::CreatedAt)
.timestamp_with_time_zone()
.not_null(),
)
.col(
ColumnDef::new(Video::UpdatedAt)
.timestamp_with_time_zone()
.not_null(),
)
.col(ColumnDef::new(Video::Status).small_integer().not_null())
.col(ColumnDef::new(Video::MimeType).text().not_null())
.col(ColumnDef::new(Video::DurationSecs).integer().not_null())
.col(ColumnDef::new(Video::SizeBytes).big_integer().not_null())
.col(
ColumnDef::new(Video::SubtitlesExtracted)
.boolean()
.not_null(),
)
.col(ColumnDef::new(Video::CreatorId).big_integer().not_null())
.col(ColumnDef::new(Video::Name).text().not_null())
.col(ColumnDef::new(Video::Description).text().not_null())
.col(ColumnDef::new(Video::Visibility).small_integer().not_null())
.foreign_key(
ForeignKey::create()
.name("fk-video-creator")
.from(Video::Table, Video::CreatorId)
.to(User::Table, User::Id),
)
.to_owned(),
)
.await
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Video::Table).to_owned())
.await
}
}

#[derive(Iden)]
pub enum Video {
Table,
Id,
CreatedAt,
UpdatedAt,
Status,
MimeType,
DurationSecs,
SizeBytes,
SubtitlesExtracted,
CreatorId,
Name,
Description,
Visibility,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
use crate::models::migrations::m20240204_000001_users::User;
use crate::models::migrations::m20240218_000002_videos::Video;
use sea_orm_migration::prelude::*;

pub struct Migration;

impl MigrationName for Migration {
fn name(&self) -> &str {
"m_20240218_000003_views"
}
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(View::Table)
.col(
ColumnDef::new(View::Id)
.big_integer()
.not_null()
.primary_key(),
)
.col(
ColumnDef::new(View::CreatedAt)
.timestamp_with_time_zone()
.not_null(),
)
.col(
ColumnDef::new(View::UpdatedAt)
.timestamp_with_time_zone()
.not_null(),
)
.col(ColumnDef::new(View::UserId).big_integer().not_null())
.col(ColumnDef::new(View::VideoId).big_integer().not_null())
.col(
ColumnDef::new(View::StartedAt)
.timestamp_with_time_zone()
.not_null(),
)
.col(ColumnDef::new(View::BytesStreamed).big_integer().not_null())
.foreign_key(
ForeignKey::create()
.name("fk-view-user")
.from(View::Table, View::UserId)
.to(User::Table, User::Id),
)
.foreign_key(
ForeignKey::create()
.name("fk-view-video")
.from(View::Table, View::VideoId)
.to(Video::Table, Video::Id),
)
.to_owned(),
)
.await
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(View::Table).to_owned())
.await
}
}

#[derive(Iden)]
pub enum View {
Table,
Id,
CreatedAt,
UpdatedAt,
UserId,
VideoId,
StartedAt,
BytesStreamed,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
use crate::models::migrations::m20240204_000001_users::User;
use crate::models::migrations::m20240218_000002_videos::Video;
use sea_orm_migration::prelude::*;

pub struct Migration;

impl MigrationName for Migration {
fn name(&self) -> &str {
"m_20240218_000004_watches"
}
}

#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Watch::Table)
.col(
ColumnDef::new(Watch::UserId)
.big_integer()
.not_null()
.primary_key(),
)
.col(ColumnDef::new(Watch::VideoId).big_integer().not_null())
.col(
ColumnDef::new(Watch::ViewId)
.big_integer()
.not_null()
.unique_key(),
)
.col(
ColumnDef::new(Watch::CreatedAt)
.timestamp_with_time_zone()
.not_null(),
)
.col(
ColumnDef::new(Watch::UpdatedAt)
.timestamp_with_time_zone()
.not_null(),
)
.col(
ColumnDef::new(Watch::StartedAt)
.timestamp_with_time_zone()
.not_null(),
)
.col(
ColumnDef::new(Watch::BytesStreamed)
.big_integer()
.not_null(),
)
.foreign_key(
ForeignKey::create()
.name("fk-watch-user")
.from(Watch::Table, Watch::UserId)
.to(User::Table, User::Id),
)
.foreign_key(
ForeignKey::create()
.name("fk-watch-video")
.from(Watch::Table, Watch::VideoId)
.to(Video::Table, Video::Id),
)
.to_owned(),
)
.await
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Watch::Table).to_owned())
.await
}
}

#[derive(Iden)]
pub enum Watch {
Table,
UserId,
VideoId,
ViewId,
CreatedAt,
UpdatedAt,
StartedAt,
BytesStreamed,
}
11 changes: 9 additions & 2 deletions backend-rs/streamfox/src/models/migrations/migrator.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
use crate::models::migrations::m20240204_000001_users;
use crate::models::migrations::{
m20240204_000001_users, m20240218_000002_videos, m20240218_000003_views, m20240218_000004_watches,
};
use sea_orm_migration::prelude::*;

pub struct Migrator;

#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20240204_000001_users::Migration)]
vec![
Box::new(m20240204_000001_users::Migration),
Box::new(m20240218_000002_videos::Migration),
Box::new(m20240218_000003_views::Migration),
Box::new(m20240218_000004_watches::Migration),
]
}
}
27 changes: 26 additions & 1 deletion backend-rs/streamfox/src/models/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,32 @@ impl Model {
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
pub enum Relation {
#[sea_orm(has_many = "super::video::Entity")]
Video,
#[sea_orm(has_many = "super::view::Entity")]
View,
#[sea_orm(has_many = "super::watch::Entity")]
Watch,
}

impl Related<super::video::Entity> for Entity {
fn to() -> RelationDef {
Relation::Video.def()
}
}

impl Related<super::view::Entity> for Entity {
fn to() -> RelationDef {
Relation::View.def()
}
}

impl Related<super::watch::Entity> for Entity {
fn to() -> RelationDef {
Relation::Watch.def()
}
}

impl ActiveModelBehavior for ActiveModel {}

Expand Down
79 changes: 79 additions & 0 deletions backend-rs/streamfox/src/models/video.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use crate::models::id::Id;
use sea_orm::entity::prelude::*;
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize)]
#[sea_orm(rs_type = "i16", db_type = "SmallInteger")]
#[serde(untagged)]
pub enum Status {
Created = 0,
Uploading = 1,
Processing = 2,
Complete = 3,
}

#[derive(Clone, Debug, PartialEq, Eq, EnumIter, DeriveActiveEnum, Serialize, Deserialize)]
#[sea_orm(rs_type = "i16", db_type = "SmallInteger")]
#[serde(untagged)]
pub enum Visibility {
Private = 0,
Unlisted = 1,
Public = 2,
}

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "video")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: Id,
pub created_at: DateTimeWithTimeZone,
pub updated_at: DateTimeWithTimeZone,
pub status: Status,
#[sea_orm(column_type = "Text")]
pub mime_type: String,
pub duration_secs: i32,
pub size_bytes: i64,
pub subtitles_extracted: bool,
pub creator_id: Id,
#[sea_orm(column_type = "Text")]
pub name: String,
#[sea_orm(column_type = "Text")]
pub description: String,
pub visibility: Visibility,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::user::Entity",
from = "Column::CreatorId",
to = "super::user::Column::Id",
on_update = "NoAction",
on_delete = "NoAction"
)]
User,
#[sea_orm(has_many = "super::view::Entity")]
View,
#[sea_orm(has_many = "super::watch::Entity")]
Watch,
}

impl Related<super::user::Entity> for Entity {
fn to() -> RelationDef {
Relation::User.def()
}
}

impl Related<super::view::Entity> for Entity {
fn to() -> RelationDef {
Relation::View.def()
}
}

impl Related<super::watch::Entity> for Entity {
fn to() -> RelationDef {
Relation::Watch.def()
}
}

impl ActiveModelBehavior for ActiveModel {}
Loading

0 comments on commit 602da88

Please sign in to comment.