-
Notifications
You must be signed in to change notification settings - Fork 123
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 #252 from LarendsD/main
added migration to fill null values for slugs
- Loading branch information
Showing
6 changed files
with
55 additions
and
18 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
28 changes: 28 additions & 0 deletions
28
backend/src/migrations/1682678760453-fill-null-slugs-snippets.ts
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,28 @@ | ||
/* eslint-disable no-console */ | ||
/* eslint class-methods-use-this: ["error", { "exceptMethods": ["up", "down"] }] */ | ||
import { MigrationInterface, QueryRunner } from 'typeorm'; | ||
import { generateUniqSlug } from '../snippets/utils/generate-uniq-slug'; | ||
import { Snippets } from '../entities/snippet.entity'; | ||
|
||
export class FillNullSlugsSnippets1682678760453 implements MigrationInterface { | ||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
const snippets: Snippets[] = await queryRunner.query( | ||
`SELECT id, slug FROM snippets;`, | ||
); | ||
const nullSnippets = snippets.filter((snippet) => !snippet.slug); | ||
|
||
nullSnippets.forEach(async (nullSnippet) => { | ||
const slug = generateUniqSlug(snippets); | ||
|
||
await queryRunner.query( | ||
`UPDATE snippets SET slug='${slug}' WHERE id=${nullSnippet.id};`, | ||
); | ||
}); | ||
} | ||
|
||
public async down(): Promise<void> { | ||
console.log( | ||
`Revert migrations for filling null slugs in snippets is not specified!`, | ||
); | ||
} | ||
} |
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,9 @@ | ||
import { faker } from "@faker-js/faker"; | ||
import { Snippets } from "../../entities/snippet.entity"; | ||
|
||
export const generateUniqSlug = (snippets: Snippets[]): string => { | ||
const slug = faker.random.alpha({ count: 7, casing: 'mixed' }); | ||
return !snippets.find((snippet) => snippet.slug === slug) | ||
? slug | ||
: generateUniqSlug(snippets); | ||
}; |
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,5 @@ | ||
import { | ||
IsEmail, | ||
IsNotEmpty, | ||
IsString, | ||
Length, | ||
Matches, | ||
|