mirror of
https://github.com/frappe/books.git
synced 2024-11-09 15:20:56 +00:00
fix: clear non existing SingleValue links
This commit is contained in:
parent
d229f514cb
commit
9864761fe7
@ -1,8 +1,14 @@
|
||||
import { Fyo } from 'fyo';
|
||||
import { ConfigFile, ConfigKeys } from 'fyo/core/types';
|
||||
import { Doc } from 'fyo/model/doc';
|
||||
import { getRegionalModels, models } from 'models/index';
|
||||
import { ModelNameEnum } from 'models/types';
|
||||
import { getRandomString, getValueMapFromList } from 'utils/index';
|
||||
import { TargetField } from 'schemas/types';
|
||||
import {
|
||||
getMapFromList,
|
||||
getRandomString,
|
||||
getValueMapFromList,
|
||||
} from 'utils/index';
|
||||
|
||||
export async function initializeInstance(
|
||||
dbPath: string,
|
||||
@ -20,6 +26,7 @@ export async function initializeInstance(
|
||||
const regionalModels = await getRegionalModels(countryCode);
|
||||
await fyo.initializeAndRegister(models, regionalModels);
|
||||
|
||||
await checkSingleLinks(fyo);
|
||||
await setSingles(fyo);
|
||||
await setCreds(fyo);
|
||||
await setVersion(fyo);
|
||||
@ -47,6 +54,56 @@ async function setSingles(fyo: Fyo) {
|
||||
}
|
||||
}
|
||||
|
||||
async function checkSingleLinks(fyo: Fyo) {
|
||||
/**
|
||||
* Required cause SingleValue tables don't maintain
|
||||
* referential integrity. Hence values Linked in the
|
||||
* Singles table can be deleted.
|
||||
*
|
||||
* These deleted links can prevent the db from loading.
|
||||
*/
|
||||
|
||||
const linkFields = Object.values(fyo.db.schemaMap)
|
||||
.filter((schema) => schema?.isSingle)
|
||||
.map((schema) => schema!.fields)
|
||||
.flat()
|
||||
.filter((field) => field.fieldtype === 'Link' && field.target)
|
||||
.map((field) => ({
|
||||
fieldKey: `${field.schemaName}.${field.fieldname}`,
|
||||
target: (field as TargetField).target,
|
||||
}));
|
||||
const linkFieldsMap = getMapFromList(linkFields, 'fieldKey');
|
||||
|
||||
const singleValues = (await fyo.db.getAllRaw('SingleValue', {
|
||||
fields: ['name', 'parent', 'fieldname', 'value'],
|
||||
})) as { name: string; parent: string; fieldname: string; value: string }[];
|
||||
|
||||
const exists: Record<string, Record<string, boolean>> = {};
|
||||
|
||||
for (const { name, fieldname, parent, value } of singleValues) {
|
||||
const fieldKey = `${parent}.${fieldname}`;
|
||||
if (!linkFieldsMap[fieldKey]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
const { target } = linkFieldsMap[fieldKey];
|
||||
if (typeof value !== 'string' || !value || !target) {
|
||||
continue;
|
||||
}
|
||||
|
||||
exists[target] ??= {};
|
||||
if (exists[target][value] === undefined) {
|
||||
exists[target][value] = await fyo.db.exists(target, value);
|
||||
}
|
||||
|
||||
if (exists[target][value]) {
|
||||
continue;
|
||||
}
|
||||
|
||||
await fyo.db.delete('SingleValue', name);
|
||||
}
|
||||
}
|
||||
|
||||
async function setCreds(fyo: Fyo) {
|
||||
const email = (await fyo.getValue(
|
||||
ModelNameEnum.AccountingSettings,
|
||||
|
@ -14,7 +14,7 @@ import { handleErrorWithDialog } from 'src/errorHandling';
|
||||
import { fyo } from 'src/initFyo';
|
||||
import router from 'src/router';
|
||||
import { IPC_ACTIONS } from 'utils/messages';
|
||||
import { App, createApp, h, ref } from 'vue';
|
||||
import { App, createApp, h } from 'vue';
|
||||
import { RouteLocationRaw } from 'vue-router';
|
||||
import { stringifyCircular } from './';
|
||||
import { evaluateHidden } from './doc';
|
||||
@ -74,7 +74,6 @@ export async function openQuickEdit({
|
||||
if (forWhat[0] === 'not in' && forWhat[1] === 'Sales') {
|
||||
defaults = Object.assign({ for: 'Purchases' });
|
||||
}
|
||||
console.log(method, schemaName, name);
|
||||
|
||||
router[method]({
|
||||
query: {
|
||||
|
Loading…
Reference in New Issue
Block a user