@ -1,7 +1,8 @@
import { RelationTypes , UITypes } from 'nocodb-sdk' ;
import { RelationTypes , UITypes } from 'nocodb-sdk' ;
import type { NcUpgraderCtx } from '~/version-upgrader/NcUpgrader' ;
import type { NcUpgraderCtx } from '~/version-upgrader/NcUpgrader' ;
import type { MetaService } from '~/meta/meta.service' ;
import { MetaTable } from '~/utils/globals' ;
import { MetaTable } from '~/utils/globals' ;
import { MetaService } from "~/meta/meta.service" ;
import { Column } from '~/models' ;
/ * *
/ * *
* This upgrader look for any broken link and try to recover it
* This upgrader look for any broken link and try to recover it
@ -25,7 +26,7 @@ export default async function ({ ncMeta }: NcUpgraderCtx) {
. leftJoin (
. leftJoin (
MetaTable . COL_RELATIONS ,
MetaTable . COL_RELATIONS ,
` ${ MetaTable . COLUMNS } .id ` ,
` ${ MetaTable . COLUMNS } .id ` ,
` ${ MetaTable . COL_SELECT_OP TIONS } .fk_column_id ` ,
` ${ MetaTable . COL_RELA TIONS } .fk_column_id ` ,
)
)
. where ( ` ${ MetaTable . COLUMNS } .uidt ` , UITypes . LinkToAnotherRecord )
. where ( ` ${ MetaTable . COLUMNS } .uidt ` , UITypes . LinkToAnotherRecord )
. whereNull ( ` ${ MetaTable . COL_RELATIONS } .id ` ) ;
. whereNull ( ` ${ MetaTable . COL_RELATIONS } .id ` ) ;
@ -78,7 +79,10 @@ export default async function ({ ncMeta }: NcUpgraderCtx) {
const linksQb = ncMeta
const linksQb = ncMeta
. knex ( MetaTable . COL_RELATIONS )
. knex ( MetaTable . COL_RELATIONS )
. select ( ` ${ MetaTable . COL_RELATIONS } .* ` )
. select ( ` ${ MetaTable . COL_RELATIONS } .* ` )
. where ( ` ${ MetaTable . COL_RELATIONS } .fk_related_model_id ` , column . fk_model_id ) ;
. where (
` ${ MetaTable . COL_RELATIONS } .fk_related_model_id ` ,
column . fk_model_id ,
) ;
if ( relatedTableId ) {
if ( relatedTableId ) {
linksQb
linksQb
. join (
. join (
@ -87,30 +91,13 @@ export default async function ({ ncMeta }: NcUpgraderCtx) {
` ${ MetaTable . COLUMNS } .id ` ,
` ${ MetaTable . COLUMNS } .id ` ,
)
)
. where ( ` ${ MetaTable . COLUMNS } .fk_model_id ` , relatedTableId ) ;
. where ( ` ${ MetaTable . COLUMNS } .fk_model_id ` , relatedTableId ) ;
}
}
const links = await linksQb ;
const links = await linksQb ;
// iterate over all links which is related to current table and if found relation which doesn't have link in the related table then use it to populate colOptions
// iterate over all links which is related to current table and if found relation which doesn't have link in the related table then use it to populate colOptions
for ( const link of links ) {
for ( const link of links ) {
let columnInCurrTable null ;
let columnInCurrTable = null ;
if ( link . type === RelationTypes . HAS_MANY ) {
if ( link . type === RelationTypes . HAS_MANY ) {
// check for bt column in current table
// check for bt column in current table
columnInCurrTable = await ncMeta
columnInCurrTable = await ncMeta
@ -218,32 +205,83 @@ export default async function ({ ncMeta }: NcUpgraderCtx) {
}
}
if ( ! columnInCurrTable ) {
if ( ! columnInCurrTable ) {
// generate meta and insert into colOptions
// generate meta and insert into colOptions
const commonProps = {
id : ( ncMeta as MetaService ) . genNanoid ( MetaTable . COL_RELATIONS ) ,
fk_column_id : column.id ,
fk_related_model_id : relatedTableId ,
created_at : link.created_at ,
updated_at : link.updated_at ,
virtual : link.virtual ,
} ;
// based on type insert data into colOptions
// based on type insert data into colOptions
switch ( link . type ) {
switch ( link . type ) {
case RelationTypes . HAS_MANY :
case RelationTypes . HAS_MANY :
// insert data into colOptions
// insert data into colOptions
ncMeta . knex ( MetaTable . COL_RELATIONS ) . insert ( {
ncMeta . knex ( MetaTable . COL_RELATIONS ) . insert ( {
id : ( ncMeta as MetaService ) . genNanoid ( MetaTable . COL_RELATIONS ) ,
. . . commonProps ,
type : RelationTypes . BELONGS_TO ,
type : RelationTypes . BELONGS_TO ,
fk_column_id : column.id ,
fk_child_column_id : link.fk_child_column_id ,
fk_related_model_id : relatedTableId ,
fk_parent_column_id : link.fk_parent_column_id ,
} ) ;
break ;
break ;
case RelationTypes . ONE_TO_ONE :
case RelationTypes . ONE_TO_ONE :
// todo:
const meta = {
bt : false ,
} ;
// insert data into colOptions
// insert data into colOptions
ncMeta . knex ( MetaTable . COL_RELATIONS ) . insert ( {
. . . commonProps ,
type : RelationTypes . ONE_TO_ONE ,
fk_child_column_id : link.fk_child_column_id ,
fk_parent_column_id : link.fk_parent_column_id ,
meta ,
} ) ;
break ;
break ;
case RelationTypes . BELONGS_TO :
case RelationTypes . BELONGS_TO :
// insert data into colOptions
// insert data into colOptions
ncMeta . knex ( MetaTable . COL_RELATIONS ) . insert ( {
. . . commonProps ,
type : RelationTypes . HAS_MANY ,
fk_child_column_id : link.fk_child_column_id ,
fk_parent_column_id : link.fk_parent_column_id ,
} ) ;
break ;
break ;
case RelationTypes . MANY_TO_MANY :
case RelationTypes . MANY_TO_MANY :
// insert data into colOptions
// insert data into colOptions
ncMeta . knex ( MetaTable . COL_RELATIONS ) . insert ( {
. . . commonProps ,
type : RelationTypes . ONE_TO_ONE ,
fk_child_column_id : link.fk_parent_column_id ,
fk_parent_column_id : link.fk_child_column_id ,
fk_mm_model_id : link.fk_mm_model_id ,
fk_mm_child_column_id : link.fk_mm_parent_column_id ,
fk_mm_parent_column_id : link.fk_mm_child_column_id ,
} ) ;
break ;
break ;
}
}
break ;
break ;
}
} else {
logger . error (
` Couldn't find any column in current table which is related to the link ' ${ link . id } '. ` ,
) ;
// delete the link column since it's not useful anymore and not recoverable
await Column . delete (
{
workspace_id : column.workspace_id ,
base_id : column.base_id ,
} ,
column . id ,
ncMeta ,
) ;
}
}
}
}
}
}