-
Notifications
You must be signed in to change notification settings - Fork 730
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
patch: convert existing currency values to precision adjusted int str…
…ings
- Loading branch information
Showing
2 changed files
with
46 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import frappe from 'frappejs'; | ||
|
||
function getTablesToConvert() { | ||
// Do not change loops to map, doesn't work for some reason. | ||
const toConvert = []; | ||
for (let key in frappe.models) { | ||
const model = frappe.models[key]; | ||
|
||
const fieldsToConvert = []; | ||
for (let i in model.fields) { | ||
const field = model.fields[i]; | ||
|
||
if (field.fieldtype === 'Currency') { | ||
fieldsToConvert.push(field.fieldname); | ||
} | ||
} | ||
|
||
if (fieldsToConvert.length > 0 && !model.isSingle && !model.basedOn) { | ||
toConvert.push({ name: key, fields: fieldsToConvert }); | ||
} | ||
} | ||
|
||
return toConvert; | ||
} | ||
|
||
export default async function execute() { | ||
const toConvert = getTablesToConvert(); | ||
for (let { name, fields } of toConvert) { | ||
const rows = await frappe.db.knex(name); | ||
const convertedRows = rows.map((row) => { | ||
for (let field of fields) { | ||
if (row[field] === null) { | ||
continue; | ||
} | ||
row[field] = frappe.pesa(row[field]).internal.bigint.toString(); | ||
} | ||
return row; | ||
}); | ||
await frappe.db.prestigeTheTable(name, convertedRows); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters