commit 834119102f8276f0b7cf083bc68d958659cb5bca Author: DrMint Date: Sat May 14 23:31:38 2022 +0200 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..81a38be --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.env* diff --git a/README.md b/README.md new file mode 100644 index 0000000..43b0af2 --- /dev/null +++ b/README.md @@ -0,0 +1,18 @@ +# migrations.accords-library.com + +Provides to Accord's Library the tools to make migrations of the database. + +## Credentials to Strapi + +Create a .env.local file with the following information: +```txt +URL_GRAPHQL=https://path.to.strapi.com/graphql +ACCESS_TOKEN=token_generated_by_strapi +``` + +Then run the script using: + +```bash +python3 prog.py +``` + diff --git a/prog.py b/prog.py new file mode 100644 index 0000000..e0f6576 --- /dev/null +++ b/prog.py @@ -0,0 +1,151 @@ +from gql import Client, gql +from gql.transport.aiohttp import AIOHTTPTransport + + +# STEP 1: SETUP + +urlGraphQL = "" +accessToken = "" + +with open(".env.local", "r") as configFile: + for line in configFile.readlines(): + if line.startswith("URL_GRAPHQL="): + urlGraphQL = line[len("URL_GRAPHQL="):-1] + elif line.startswith("ACCESS_TOKEN="): + accessToken = line[len("ACCESS_TOKEN="):-1] + + +# Select your transport with a defined url endpoint +transport = AIOHTTPTransport(url=urlGraphQL, headers={ + 'Authorization': "Bearer " + accessToken}) + +# Create a GraphQL client using the defined transport +client = Client(transport=transport, fetch_schema_from_transport=True) + +# Provide a GraphQL query +migrationContentsIds = gql( + """ + query migrationContentsIds { + contents(pagination: { limit: -1 }) { + data { + id + } + } + } + """ +) + +mutation = gql( + """ + mutation updateContent($id: ID!, $data: ContentInput!) { + updateContent(id: $id, data: $data) { + data { + id + } + } + } + """ +) + +migrationContent = gql( + """ + query migrationContent($id: ID!) { + content(id: $id) { + data { + attributes { + titles { + language { + data { + id + } + } + pre_title + title + subtitle + description + } + text_set { + language { + data { + id + } + } + status + source_language { + data { + id + } + } + transcribers { + data { + id + } + } + translators { + data { + id + } + } + proofreaders { + data { + id + } + } + notes + text + } + } + } + } + } + """ +) + + +# STEP 2: GET LIST OF IDS + +# Execute the query on the transport +ids = client.execute(migrationContentsIds)["contents"]["data"] +ids = [id["id"] for id in ids] + + +for id in ids: + # STEP 3: GET DATA FROM SPECIFIC ID + content = client.execute(migrationContent, variable_values={"id": id})[ + "content"]["data"]["attributes"] + + # STEP 4: TRANSFORM DATA + newContent = {} + for title in content["titles"]: + langid = title["language"]["data"]["id"] + newContent[langid] = {} + newContent[langid]["language"] = langid + newContent[langid]["pre_title"] = title["pre_title"] + newContent[langid]["title"] = title["title"] + newContent[langid]["subtitle"] = title["subtitle"] + newContent[langid]["description"] = title["description"] + + for textSet in content["text_set"]: + langid = textSet["language"]["data"]["id"] + if langid not in newContent: + newContent[langid] = {} + newContent[langid]["language"] = langid + newContent[langid]["title"] = "MISSING TITLE" + newContent[langid]["text_set"] = {} + newContent[langid]["text_set"]["status"] = textSet["status"] + newContent[langid]["text_set"]["source_language"] = textSet["source_language"]["data"]["id"] + newContent[langid]["text_set"]["text"] = textSet["text"] + newContent[langid]["text_set"]["transcribers"] = [e["id"] + for e in textSet["transcribers"]["data"]] + newContent[langid]["text_set"]["translators"] = [e["id"] + for e in textSet["translators"]["data"]] + newContent[langid]["text_set"]["proofreaders"] = [e["id"] + for e in textSet["proofreaders"]["data"]] + newContent[langid]["text_set"]["notes"] = textSet["notes"] + + # STEP 5: UPDATE ON THE CMS + + client.execute(mutation, variable_values={"id": id, + "data": { + "translations": [newContent[e] for e in newContent] + }})