-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from sacconazzo:feat/v2
Feat/v2
- Loading branch information
Showing
23 changed files
with
178 additions
and
49 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
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
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,53 @@ | ||
const fs = require('fs') | ||
const root = require('find-root')() | ||
const Knex = require('knex') | ||
const { getMigrationKey } = require('./index') | ||
|
||
const { db: dbConfig, options, migrationPath } = require('../config') | ||
|
||
module.exports = async policy => { | ||
let knex | ||
try { | ||
knex = Knex(dbConfig) | ||
|
||
const [policyContent] = await knex('directus_policies').select().where({ id: policy }) | ||
if (!policyContent) throw new Error('Policy not valid') | ||
|
||
const tamplateContent = fs.readFileSync( | ||
`${root}/scripts/migrate/templates/policy-update${options.module ? '-es' : ''}.js`, | ||
'utf8', | ||
) | ||
|
||
const migrationContent = tamplateContent.replaceAll('$$$$', policy).replace('%%%%', JSON.stringify(policyContent)) | ||
|
||
const migrationName = `${getMigrationKey()}-policy-update.js` | ||
fs.writeFileSync(`${migrationPath}/${migrationName}`, migrationContent) | ||
|
||
console.log(`Migration created for policy ${policy}: ${migrationName}`) | ||
|
||
const permissionContent = await knex('directus_permissions').select().where({ policy }) | ||
permissionContent.forEach(p => { | ||
p.permissions = JSON.parse(p.permissions) | ||
p.validation = JSON.parse(p.validation) | ||
p.presets = JSON.parse(p.presets) | ||
}) | ||
|
||
const tamplatePContent = fs.readFileSync( | ||
`${root}/scripts/migrate/templates/permissions-update${options.module ? '-es' : ''}.js`, | ||
'utf8', | ||
) | ||
|
||
const migrationPContent = tamplatePContent | ||
.replace('$$$$', policy) | ||
.replace('%%%%', JSON.stringify(permissionContent)) | ||
|
||
const migrationPName = `${getMigrationKey()}-permissions-update.js` | ||
fs.writeFileSync(`${migrationPath}/${migrationPName}`, migrationPContent) | ||
|
||
console.log(`Migration created for permissions: ${migrationPName}`) | ||
} catch (err) { | ||
console.error(err.message || err.code || err) | ||
} finally { | ||
knex && knex.client.pool.destroy() | ||
} | ||
} |
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
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,20 @@ | ||
import uuid from 'uuid' | ||
|
||
const access = JSON.parse('%%%%') | ||
access.forEach(p => { | ||
p.id = uuid.v4() | ||
}) | ||
|
||
module.exports = { | ||
async up(knex) { | ||
const role = '$$$$' | ||
await knex('directus_access') | ||
.delete() | ||
.where('role', role !== 'null' ? role : null) | ||
return access.length ? knex('directus_access').insert(access) : true | ||
}, | ||
|
||
async down(knex) { | ||
return true | ||
}, | ||
} |
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,18 @@ | ||
const uuid = require('uuid') | ||
|
||
const access = JSON.parse('%%%%') | ||
access.forEach(p => { | ||
p.id = uuid.v4() | ||
}) | ||
|
||
module.exports = { | ||
up: async knex => { | ||
const role = '$$$$' | ||
await knex('directus_access') | ||
.delete() | ||
.where('role', role !== 'null' ? role : null) | ||
return access.length ? knex('directus_access').insert(access) : true | ||
}, | ||
|
||
down: async knex => true, | ||
} |
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 |
---|---|---|
|
@@ -18,7 +18,7 @@ export default { | |
} | ||
}, | ||
|
||
down(knex) { | ||
async down(knex) { | ||
return true | ||
}, | ||
} |
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 |
---|---|---|
|
@@ -18,5 +18,5 @@ module.exports = { | |
} | ||
}, | ||
|
||
down: knex => true, | ||
down: async knex => true, | ||
} |
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
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
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,14 @@ | ||
const policies = JSON.parse('%%%%') | ||
|
||
export default { | ||
async up(knex) { | ||
const [setPolicy] = await knex('directus_policies').select().where('id', '$$$$') | ||
return setPolicy | ||
? knex('directus_policies').update(policies).where('id', '$$$$') | ||
: knex('directus_policies').insert(policies) | ||
}, | ||
|
||
async down(knex) { | ||
return true | ||
}, | ||
} |
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,12 @@ | ||
const policies = JSON.parse('%%%%') | ||
|
||
module.exports = { | ||
up: async knex => { | ||
const [setPolicy] = await knex('directus_policies').select().where('id', '$$$$') | ||
return setPolicy | ||
? knex('directus_policies').update(policies).where('id', '$$$$') | ||
: knex('directus_policies').insert(policies) | ||
}, | ||
|
||
down: async knex => true, | ||
} |
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
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
Oops, something went wrong.