-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'fer/1832' into staging
- Loading branch information
Showing
18 changed files
with
526 additions
and
193 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
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
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,26 @@ | ||
import { configureBlockContent } from '../../editors' | ||
|
||
const richTextType = configureBlockContent({ | ||
h2: false, | ||
h3: false, | ||
externalLink: false, | ||
internalLink: false, | ||
lists: false, | ||
attachment: false, | ||
smallText: false, | ||
highlight: true, | ||
}) | ||
|
||
export default { | ||
title: 'Rich Text', | ||
name: 'tableRichText', | ||
type: 'object', | ||
fields: [ | ||
{ | ||
name: 'text', | ||
title: 'Text', | ||
type: 'array', | ||
of: [richTextType], | ||
}, | ||
], | ||
} |
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 |
---|---|---|
@@ -1,6 +1,13 @@ | ||
# Description | ||
# README.md | ||
|
||
This folder contains scripts used to migrate content. | ||
1. Set up the datasets in which the script should be run in the `./run.ts` file; | ||
|
||
- Change the target scripts array with the desired migration scripts. | ||
- `npx sanity exec 'automateScripts.mjs'` to run the migration scripts. | ||
2. Set up the scripts that should be run in the `./run.ts` file; | ||
|
||
3. Make sure you have Sanity CLI setup on your terminal | ||
|
||
4. Login into Sanity CLI using `sanity login --sso=equinor-sanity` | ||
|
||
5. Go to the script folders using `cd sanityv3/scripts` | ||
|
||
6. Run with `sanity exec run.js --with-user-token`; |
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,93 @@ | ||
import { customAlphabet } from 'nanoid' | ||
|
||
export default function script(client) { | ||
const nanoid = customAlphabet('1234567890abcdef', 12) | ||
|
||
const fetchDocuments = async () => | ||
client.fetch(/* groq */ ` | ||
*[_type == 'page' && | ||
(count(content[_type == "table"].tableRows[count(row[_type == 'textField']) > 0]) > 0) | ||
] [0...100] { | ||
_id, | ||
_rev, | ||
"table": content[_type == 'table' && count(tableRows[count(row[_type == 'textField']) > 0]) > 0] { | ||
"tableRows": tableRows[count(row[_type == 'textField']) > 0]{ | ||
"row": row[_type == 'textField'] | ||
} | ||
} | ||
} | ||
`) | ||
|
||
const buildPatches = (docs) => | ||
docs | ||
.map((doc) => | ||
doc.fields.map((el) => ({ | ||
id: doc.id, | ||
patch: { | ||
set: { | ||
[`content..[_key == "${el._key}"]`]: { | ||
_key: el._key, | ||
_type: 'richText', | ||
text: [ | ||
{ | ||
_key: nanoid(), | ||
style: 'normal', | ||
_type: 'block', | ||
children: [ | ||
{ | ||
_key: nanoid(), | ||
_type: 'span', | ||
marks: [], | ||
text: el.text, | ||
}, | ||
], | ||
markDefs: [], | ||
}, | ||
], | ||
}, | ||
}, | ||
// this will cause the migration to fail if any of the documents has been | ||
// modified since it was fetched. | ||
ifRevisionID: doc._rev, | ||
}, | ||
})), | ||
) | ||
.flat() | ||
|
||
const createTransaction = (patches) => | ||
patches.reduce((tx, patch) => tx.patch(patch.id, patch.patch), client.transaction()) | ||
|
||
const commitTransaction = (tx) => tx.commit() | ||
|
||
const migrateNextBatch = async () => { | ||
console.log('fetching...') | ||
const rawDocuments = await fetchDocuments() | ||
const documents = rawDocuments.map((document) => { | ||
return { | ||
id: document._id, | ||
fields: document.table?.flatMap((content) => | ||
content.tableRows?.flatMap((table) => table.row?.flatMap((text) => text)), | ||
), | ||
} | ||
}) | ||
|
||
const patches = buildPatches(documents) | ||
|
||
if (patches.length === 0) { | ||
console.log('No more documents to migrate!') | ||
return null | ||
} | ||
console.log( | ||
`Migrating batch:\n %s`, | ||
patches.map((patch) => `${patch.id} => ${JSON.stringify(patch.patch)}`).join('\n'), | ||
) | ||
const transaction = createTransaction(patches) | ||
await commitTransaction(transaction) | ||
return migrateNextBatch() | ||
} | ||
|
||
migrateNextBatch().catch((err) => { | ||
console.error(err) | ||
process.exit(1) | ||
}) | ||
} |
Oops, something went wrong.