Skip to content

Commit

Permalink
Add a migration to update the usernames field in Strapi (#152)
Browse files Browse the repository at this point in the history
* chore: use pnpm

* feat: create endpoint to update usernames

* feat: create migration to update the username field

* refactor: revert pnpm changes

* fix: audit fix

* fix: update packages

* fix: remove unused export from methods
  • Loading branch information
andrea-smiesna authored Jan 28, 2025
1 parent 9cb6733 commit fa012ca
Show file tree
Hide file tree
Showing 7 changed files with 2,125 additions and 1,266 deletions.
3,313 changes: 2,061 additions & 1,252 deletions app/pnpm-lock.yaml

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions app/utils/requests/innoUsers/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,18 @@ export const GetInnoUserByProviderIdQuery = graphql(
[InnoUserFragment],
);

export const GetAllInnoUsers = graphql(`
query GetInnoUsers($limit: Int) {
innoUsers(pagination: { limit: $limit }) {
data {
id
attributes {
username
export const GetAllInnoUsers = graphql(
`
query GetInnoUsers($limit: Int) {
innoUsers(pagination: { limit: $limit }) {
data {
...InnoUser
}
}
}
}
`);
`,
[InnoUserFragment],
);

export const GetEmailsByUsernamesQuery = graphql(`
query GetEmailsByUsernames($usernames: [String!]) {
Expand Down
23 changes: 18 additions & 5 deletions app/utils/requests/innoUsers/requests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,29 @@ export async function getInnoUserByUsername(username: string): Promise<User | nu
}
}

export async function getAllInnoUsers() {
async function getAllInnoUserNames() {
try {
const response = await getAllInnoUsers();
if (!response) {
throw new Error('No users data available');
}
const users = response.map((user) => ({ username: user.attributes.username }));
return users;
} catch (err) {
const error = strapiError('Getting All Inno users', err as RequestError);
logger.error(error);
throw err;
}
}

async function getAllInnoUsers() {
try {
const response = await strapiGraphQLFetcher(GetAllInnoUsers, { limit: 1000 });
if (!response.innoUsers?.data) {
throw new Error('No users data available');
}

const users = response.innoUsers?.data.map((user) => ({ username: user.attributes.username }));

return users;
return response.innoUsers.data;
} catch (err) {
const error = strapiError('Getting All Inno users', err as RequestError);
logger.error(error);
Expand Down Expand Up @@ -209,7 +222,7 @@ async function uploadImage(imageUrl: string, fileName: string) {

export async function fetchMentionData(search: string): Promise<Mention[]> {
try {
const data = await getAllInnoUsers();
const data = await getAllInnoUserNames();

const formattedData = data.map((user) => ({ username: user.username as string }));
return formattedData.filter((user) => user.username?.toLowerCase().includes(search.toLowerCase()));
Expand Down
10 changes: 10 additions & 0 deletions strapi/create-migration.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

read -p "Enter the name for the migration file: " userFileName
currentDateTime=$(date +"%Y.%m.%dT%H.%M.%S")
fileName="${currentDateTime}.${userFileName}.js"

mkdir -p ./database/migrations
touch "./database/migrations/$fileName"

echo "Migration file created: ./database/migrations/$fileName"
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module.exports = {
async up() {
console.info('Starting the "update_innousers_username" migration');
await strapi.db.transaction(async () => {
const innoUsers = await strapi.entityService.findMany('api::inno-user.inno-user');
innoUsers.map(async (user) => {
if (!user.username) {
const username = user.email.split('@')[0];
await strapi.entityService.update(
"api::inno-user.inno-user",
user.id,
{
data: {
username: username
},
}
)
strapi.log.info(
`[Migration]: Username for the InnoUser with id ${user.id} was updated.`
)
}
});
strapi.log.info('Usernames for all Inno Users were updated.');
});
},
};
1 change: 1 addition & 0 deletions strapi/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"build": "pnpm run build:strapi && pnpm run build:health-plugin",
"build:health-plugin": "cd src/plugins/healthcheck && pnpm run build",
"build:strapi": "strapi build",
"create-migration": "./create-migration.sh",
"develop": "strapi develop",
"start": "strapi start",
"strapi": "strapi"
Expand Down

0 comments on commit fa012ca

Please sign in to comment.