π₯ π₯ π₯ A Free & Self-hostable Airtable Alternative
TypeScript airtableairtable-alternativeautomatic-apihacktoberfestlow-codeno-codeno-code-databaseno-code-platformpostgresqlrest-apirestful-apispreadsheetsqliteswagger
fix: migration correction
DarkPhoenix270470461f578404
diff --git a/packages/nocodb/src/meta/migrations/v2/nc_091_unify_model.ts b/packages/nocodb/src/meta/migrations/v2/nc_091_unify_model.tsindex d4dccb08a7..6ea3e68169 100644--- a/packages/nocodb/src/meta/migrations/v2/nc_091_unify_model.ts+++ b/packages/nocodb/src/meta/migrations/v2/nc_091_unify_model.ts@@ -25,24 +25,51 @@ const up = async (knex: Knex) => {logger.log('Added columns and indexes');- logger.log('Starting migration of data from nc_dashboards_v2 to nc_models');+ logger.log('Starting migration of data from nc_models to nc_dashboards_v2');++ // First get all dashboard pairs+ const dashboardPairs = await knex+ .select('fk_workspace_id', 'base_id')+ .from(MetaTable.DASHBOARDS)+ .groupBy('fk_workspace_id', 'base_id');++ // Then get max orders only for those specific pairs+ const maxOrders = await knex+ .from(MetaTable.MODELS)+ .select('fk_workspace_id', 'base_id')+ .max('order as max_order')+ .whereIn(+ ['fk_workspace_id', 'base_id'],+ dashboardPairs.map((p) => [p.fk_workspace_id, p.base_id]),+ )+ .where((builder) => {+ builder.whereNull('source_id').orWhereExists((subquery) => {+ subquery+ .select('*')+ .from(MetaTable.SOURCES)+ .where('nc_sources_v2.id', 'nc_models_v2.source_id')+ .where('nc_sources_v2.is_meta', true)+ .where('nc_sources_v2.is_local', true);+ });+ })+ .groupBy('fk_workspace_id', 'base_id');++ // Create lookup map+ const maxOrderMap = new Map();+ maxOrders.forEach((row) => {+ maxOrderMap.set(+ `${row.fk_workspace_id}:${row.base_id}`,+ row.max_order || -1,+ );+ });await migrateTableInBatches(knex,MetaTable.DASHBOARDS,MetaTable.MODELS,async (dashboard) => {- // Find highest order value in nc_models for same fk_workspace_id and base_id and no source_id and increment by 1- const highestOrder = await knex- .select('order')- .from(MetaTable.MODELS)- .where('fk_workspace_id', dashboard.fk_workspace_id)- .where('base_id', dashboard.base_id)- .where('source_id', null)- .max('order')- .first();-- dashboard.order = Number(highestOrder?.order) + 1 || 0;+ const key = `${dashboard.fk_workspace_id}:${dashboard.base_id}`;+ dashboard.order = (maxOrderMap.get(key) || -1) + 1;return {id: dashboard.id,