Skip to content

Commit

Permalink
Deduplicate code
Browse files Browse the repository at this point in the history
  • Loading branch information
aqrln committed Nov 28, 2024
1 parent f7c2f09 commit e598207
Showing 1 changed file with 14 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use psl::{
},
ValidatedSchema,
};
use sql_schema_describer::{self as sql, PrismaValue};
use sql_schema_describer::{self as sql, PrismaValue, SqlSchema};
use std::collections::HashMap;

pub(crate) fn calculate_sql_schema(datamodel: &ValidatedSchema, flavour: &dyn SqlFlavour) -> SqlDatabaseSchema {
Expand Down Expand Up @@ -264,30 +264,19 @@ fn push_relation_tables(ctx: &mut Context<'_>) {

// Unique index or PK on AB
{
let index_id = match ctx.flavour.m2m_join_table_constraint() {
JoinTableUniquenessConstraint::PrimaryKey => {
const SUFFIX: &str = "_AB_pkey";
let constraint_name = format!(
"{}{SUFFIX}",
table_name
.chars()
.take(max_identifier_length - SUFFIX.len())
.collect::<String>()
);
ctx.schema.describer_schema.push_primary_key(table_id, constraint_name)
}
JoinTableUniquenessConstraint::UniqueIndex => {
const SUFFIX: &str = "_AB_unique";
let index_name = format!(
"{}{SUFFIX}",
table_name
.chars()
.take(max_identifier_length - SUFFIX.len())
.collect::<String>()
);
ctx.schema.describer_schema.push_unique_constraint(table_id, index_name)
}
};
let (constraint_suffix, push_constraint): (_, fn(_, _, _) -> _) =
match ctx.flavour.m2m_join_table_constraint() {
JoinTableUniquenessConstraint::PrimaryKey => ("_AB_pkey", SqlSchema::push_primary_key),
JoinTableUniquenessConstraint::UniqueIndex => ("_AB_unique", SqlSchema::push_unique_constraint),
};
let constraint_name = format!(
"{}{constraint_suffix}",
table_name
.chars()
.take(max_identifier_length - constraint_suffix.len())
.collect::<String>()
);
let index_id = push_constraint(&mut ctx.schema.describer_schema, table_id, constraint_name);

ctx.schema.describer_schema.push_index_column(sql::IndexColumn {
index_id,
Expand Down

0 comments on commit e598207

Please sign in to comment.