Skip to content

Instantly share code, notes, and snippets.

@lordsarcastic
Created September 18, 2024 22:35
Show Gist options
  • Save lordsarcastic/351960fffa034498540cda1f41104e00 to your computer and use it in GitHub Desktop.
Save lordsarcastic/351960fffa034498540cda1f41104e00 to your computer and use it in GitHub Desktop.
This is a SeaORM migration file to create a table for number of employees in an organization
use sea_orm_migration::prelude::*;
use uuid::Uuid;
#[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(OrganizationEmployeeNumber::Table)
.if_not_exists()
.col(
ColumnDef::new(OrganizationEmployeeNumber::Id)
.uuid()
.not_null()
.primary_key(),
)
.col(
ColumnDef::new(OrganizationEmployeeNumber::Name)
.char()
.char_len(30)
.unique_key()
.not_null(),
)
.col(
ColumnDef::new(OrganizationEmployeeNumber::CreatedAt)
.timestamp()
.default(SimpleExpr::Keyword(Keyword::CurrentTimestamp))
.not_null(),
)
.col(
ColumnDef::new(OrganizationEmployeeNumber::UpdatedAt)
.timestamp()
.default(SimpleExpr::Keyword(Keyword::CurrentTimestamp))
.not_null(),
)
.to_owned(),
)
.await?;
let inserts = vec![
Query::insert()
.into_table(OrganizationEmployeeNumber::Table)
.columns([
OrganizationEmployeeNumber::Id,
OrganizationEmployeeNumber::Name,
])
.values_panic([
Uuid::now_v7().into(),
"1 - 10".into()
])
.to_owned(),
Query::insert()
.into_table(OrganizationEmployeeNumber::Table)
.columns([
OrganizationEmployeeNumber::Id,
OrganizationEmployeeNumber::Name,
])
.values_panic([Uuid::now_v7().into(), "11 - 20".into()])
.to_owned(),
Query::insert()
.into_table(OrganizationEmployeeNumber::Table)
.columns([
OrganizationEmployeeNumber::Id,
OrganizationEmployeeNumber::Name,
])
.values_panic([Uuid::now_v7().into(), "20 - 50".into()])
.to_owned(),
Query::insert()
.into_table(OrganizationEmployeeNumber::Table)
.columns([
OrganizationEmployeeNumber::Id,
OrganizationEmployeeNumber::Name,
])
.values_panic([Uuid::now_v7().into(), "50".into()])
.to_owned(),
];
for item in inserts.iter().take(3) {
manager.exec_stmt(item.to_owned()).await?;
}
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(
Table::drop()
.table(OrganizationEmployeeNumber::Table)
.to_owned(),
)
.await
}
}
#[derive(DeriveIden)]
pub enum OrganizationEmployeeNumber {
Table,
Id,
Name,
CreatedAt,
UpdatedAt,
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment