2
0
mirror of https://github.com/frappe/books.git synced 2024-11-10 07:40:55 +00:00

Merge pull request #237 from 18alantom/fix-db-load

fix: issues with db loading
This commit is contained in:
Alan 2021-11-06 01:18:21 +05:30 committed by GitHub
commit 49cf248247
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 73 additions and 39 deletions

View File

@ -45,9 +45,10 @@ export default {
watch: {
async activeScreen(value) {
if (!value) return;
const { width, height } = ipcRenderer.invoke(
const { width, height } = await ipcRenderer.invoke(
IPC_ACTIONS.GET_PRIMARY_DISPLAY_SIZE
);
let size = {
Desk: [width, height],
DatabaseSelector: [600, 600],
@ -69,12 +70,15 @@ export default {
WindowsTitleBar,
},
async mounted() {
let lastSelectedFilePath = config.get('lastSelectedFilePath', null);
if (!lastSelectedFilePath) {
this.activeScreen = 'DatabaseSelector';
} else {
await connectToLocalDatabase(lastSelectedFilePath);
const lastSelectedFilePath = config.get('lastSelectedFilePath', null);
const connectionSuccess = await connectToLocalDatabase(
lastSelectedFilePath
);
if (connectionSuccess) {
this.showSetupWizardOrDesk();
} else {
this.activeScreen = 'DatabaseSelector';
}
},
methods: {

View File

@ -139,7 +139,7 @@ export default {
};
},
mounted() {
this.files = config.get('files', []);
this.files = config.get('files', []).filter(({filepath}) => fs.existsSync(filepath));
this.showFiles = this.files.length > 0;
},
methods: {
@ -159,9 +159,14 @@ export default {
},
async connectToDatabase(filePath) {
this.loadingDatabase = true;
await connectToLocalDatabase(filePath);
const connectionSuccess = await connectToLocalDatabase(filePath);
this.loadingDatabase = false;
if(connectionSuccess) {
this.$emit('database-connect');
} else {
alert(frappe._("Please select an existing database or create a new one."))
}
},
getFileLastModified(filePath) {
let stats = fs.statSync(filePath);

View File

@ -8,17 +8,17 @@
<FormControl
:df="meta.getField('companyLogo')"
:value="doc.companyLogo"
@change="value => setValue('companyLogo', value)"
@change="(value) => setValue('companyLogo', value)"
/>
<div class="ml-2">
<FormControl
ref="companyField"
:df="meta.getField('companyName')"
:value="doc.companyName"
@change="value => setValue('companyName', value)"
@change="(value) => setValue('companyName', value)"
:input-class="
classes => [
'bg-transparent font-semibold text-xl text-white placeholder-blue-200 focus:outline-none focus:bg-blue-600 px-3 rounded py-1'
(classes) => [
'bg-transparent font-semibold text-xl text-white placeholder-blue-200 focus:outline-none focus:bg-blue-600 px-3 rounded py-1',
]
"
:autofocus="true"
@ -28,10 +28,10 @@
<FormControl
:df="meta.getField('email')"
:value="doc.email"
@change="value => setValue('email', value)"
@change="(value) => setValue('email', value)"
:input-class="
classes => [
'text-base bg-transparent text-white placeholder-blue-200 focus:bg-blue-600 focus:outline-none rounded px-3 py-1'
(classes) => [
'text-base bg-transparent text-white placeholder-blue-200 focus:bg-blue-600 focus:outline-none rounded px-3 py-1',
]
"
/>
@ -69,7 +69,7 @@ import Popover from '@/components/Popover';
import {
getErrorMessage,
handleErrorWithDialog,
showMessageDialog
showMessageDialog,
} from '@/utils';
export default {
@ -79,20 +79,20 @@ export default {
doc: null,
loading: false,
valuesFilled: false,
emailError: null
emailError: null,
};
},
provide() {
return {
doctype: 'SetupWizard',
name: 'SetupWizard'
name: 'SetupWizard',
};
},
components: {
TwoColumnForm,
FormControl,
Button,
Popover
Popover,
},
async mounted() {
this.doc = await frappe.newDoc({ doctype: 'SetupWizard' });
@ -103,7 +103,7 @@ export default {
methods: {
setValue(fieldname, value) {
this.emailError = null;
this.doc.set(fieldname, value).catch(e => {
this.doc.set(fieldname, value).catch((e) => {
// set error
if (fieldname === 'email') {
this.emailError = getErrorMessage(e, this.doc);
@ -112,7 +112,7 @@ export default {
},
allValuesFilled() {
let values = this.meta.quickEditFields.map(
fieldname => this.doc[fieldname]
(fieldname) => this.doc[fieldname]
);
return values.every(Boolean);
},
@ -127,10 +127,19 @@ export default {
this.$emit('setup-complete');
} catch (e) {
this.loading = false;
console.log(e, this.doc);
if (e.type === frappe.errors.DuplicateEntryError) {
// TODO: Add option to overwrite file from here.
const message = this._(
'Records already exist in the db. Please create a new database file and try again.'
);
showMessageDialog({ message });
} else {
handleErrorWithDialog(e, this.doc);
}
}
},
},
computed: {
meta() {
return frappe.getMeta('SetupWizard');
@ -140,7 +149,7 @@ export default {
},
buttonText() {
return this.loading ? this._('Setting Up...') : this._('Next');
}
}
},
},
};
</script>

View File

@ -1,5 +1,6 @@
import frappe from 'frappejs';
import countryList from '~/fixtures/countryInfo.json';
import generateTaxes from '../../../models/doctype/Tax/RegionalChanges';
import config from '@/config';
export default async function setupCompany(setupWizardValues) {
@ -96,10 +97,9 @@ async function setupChartOfAccounts(bankName) {
}
async function setupRegionalChanges(country) {
const generateRegionalTaxes = await import('~/models/doctype/Tax/RegionalChanges');
await generateRegionalTaxes(country);
await generateTaxes(country);
if (country === 'India') {
frappe.models.Party = await import('~/models/doctype/Party/RegionalChanges');
frappe.models.Party = await import('../../../models/doctype/Party/RegionalChanges');
await frappe.db.migrate();
}
}

View File

@ -36,7 +36,12 @@ export async function createNewDatabase() {
return filePath;
},
},
{ label: _('Cancel'), action() {} },
{
label: _('Cancel'),
action() {
return '';
},
},
],
});
} else {
@ -63,11 +68,19 @@ export async function loadExistingDatabase() {
}
export async function connectToLocalDatabase(filepath) {
if (!filepath) {
return false;
}
frappe.login('Administrator');
try {
frappe.db = new SQLite({
dbPath: filepath,
});
await frappe.db.connect();
} catch (error) {
return false;
}
await migrate();
await postStart();
@ -86,6 +99,7 @@ export async function connectToLocalDatabase(filepath) {
// set last selected file
config.set('lastSelectedFilePath', filepath);
return true;
}
export async function showMessageDialog({
@ -188,13 +202,15 @@ export function openQuickEdit({ doctype, name, hideFields, defaults = {} }) {
export function getErrorMessage(e, doc) {
let errorMessage = e.message || _('An error occurred');
if (e.type === frappe.errors.LinkValidationError) {
const { doctype, name } = doc;
const canElaborate = doctype && name;
if (e.type === frappe.errors.LinkValidationError && canElaborate) {
errorMessage = _('{0} {1} is linked with existing records.', [
doc.doctype,
doc.name,
doctype,
name,
]);
} else if (e.type === frappe.errors.DuplicateEntryError) {
errorMessage = _('{0} {1} already exists.', [doc.doctype, doc.name]);
} else if (e.type === frappe.errors.DuplicateEntryError && canElaborate) {
errorMessage = _('{0} {1} already exists.', [doctype, name]);
}
return errorMessage;
}