mirror of
https://github.com/plankanban/planka.git
synced 2025-07-22 06:39:44 +02:00
parent
d627a19935
commit
aa69bb8d1e
33 changed files with 370 additions and 104 deletions
49
server/utils/migrations.js
Normal file
49
server/utils/migrations.js
Normal file
|
@ -0,0 +1,49 @@
|
|||
const POSITION_GAP = 65535;
|
||||
|
||||
const addPosition = async (knex, tableName, parentFieldName) => {
|
||||
await knex.schema.table(tableName, (table) => {
|
||||
/* Columns */
|
||||
|
||||
table.specificType('position', 'double precision');
|
||||
|
||||
/* Indexes */
|
||||
|
||||
table.index('position');
|
||||
});
|
||||
|
||||
const records = await knex(tableName).orderBy([parentFieldName, 'id']);
|
||||
|
||||
let prevParentId;
|
||||
let position;
|
||||
|
||||
// eslint-disable-next-line no-restricted-syntax
|
||||
for (record of records) {
|
||||
if (record[parentFieldName] === prevParentId) {
|
||||
position += POSITION_GAP;
|
||||
} else {
|
||||
prevParentId = record[parentFieldName];
|
||||
position = POSITION_GAP;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line no-await-in-loop
|
||||
await knex(tableName)
|
||||
.update({
|
||||
position,
|
||||
})
|
||||
.where('id', record.id);
|
||||
}
|
||||
|
||||
return knex.schema.table(tableName, (table) => {
|
||||
table.dropNullable('position');
|
||||
});
|
||||
};
|
||||
|
||||
const removePosition = (knex, tableName) =>
|
||||
knex.schema.table(tableName, (table) => {
|
||||
table.dropColumn('position');
|
||||
});
|
||||
|
||||
module.exports = {
|
||||
addPosition,
|
||||
removePosition,
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue