Skip to content

Commit

Permalink
Merge pull request #261 from mnfst/257-failing-if-entity-relation-nam…
Browse files Browse the repository at this point in the history
…e-is-same-as-entity-name

fix(seeder): cannot seed if entity name is reserved word, Closes #257
  • Loading branch information
brunobuddy authored Jan 9, 2025
2 parents 87321e9 + 9c113a7 commit 9626c51
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/kind-badgers-share.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'manifest': patch
---

Fix seed failing if entity name is a reserved word on SQLite
3 changes: 2 additions & 1 deletion packages/core/manifest/src/seed/services/seeder.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,12 @@ export class SeederService {

// Truncate all tables.
const queryRunner: QueryRunner = this.dataSource.createQueryRunner()

await queryRunner.query('PRAGMA foreign_keys = OFF')
await Promise.all(
entityMetadatas.map(async (entity: EntityMetadata) =>
queryRunner
.query(`DELETE FROM ${entity.tableName}`)
.query(`DELETE FROM [${entity.tableName}]`)
.then(() =>
queryRunner.query(
`DELETE FROM sqlite_sequence WHERE name = '${entity.tableName}'`
Expand Down
35 changes: 32 additions & 3 deletions packages/core/manifest/src/seed/tests/seeder.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { StorageService } from '../../storage/services/storage/storage.service'
import { SeederService } from '../services/seeder.service'
import { EntityService } from '../../entity/services/entity.service'
import { RelationshipService } from '../../entity/services/relationship.service'
import { DataSource } from 'typeorm'
import { DataSource, EntityMetadata } from 'typeorm'
import { EntityManifestService } from '../../manifest/services/entity-manifest.service'

describe('SeederService', () => {
Expand All @@ -15,14 +15,28 @@ describe('SeederService', () => {
let relationshipService: RelationshipService
let dataSource: DataSource

const dummyEntityMetadatas: EntityMetadata[] = [
{
tableName: 'table1'
},
{
tableName: 'table2'
}
] as EntityMetadata[]

const queryRunner: any = {
query: jest.fn(() => Promise.resolve())
}

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
providers: [
SeederService,
{
provide: EntityService,
useValue: {
createEntity: jest.fn()
createEntity: jest.fn(),
getEntityMetadatas: jest.fn(() => dummyEntityMetadatas)
}
},
{
Expand All @@ -46,7 +60,8 @@ describe('SeederService', () => {
{
provide: DataSource,
useValue: {
getRepository: jest.fn()
getRepository: jest.fn(),
createQueryRunner: jest.fn(() => queryRunner)
}
}
]
Expand All @@ -65,4 +80,18 @@ describe('SeederService', () => {
it('should be defined', () => {
expect(service).toBeDefined()
})

describe('seed', () => {
it('should truncate all tables escaping table names', async () => {
jest.spyOn(dataSource, 'createQueryRunner').mockReturnValue(queryRunner)

await service.seed()

dummyEntityMetadatas.forEach((entityMetadata) => {
expect(queryRunner.query).toHaveBeenCalledWith(
`DELETE FROM [${entityMetadata.tableName}]`
)
})
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ describe('Custom validators', () => {
})
)

console.log(goodValidations, badValidations)

expect(goodValidations.every((validation) => validation.length === 0)).toBe(
true
)
Expand Down

0 comments on commit 9626c51

Please sign in to comment.