mirror of
https://github.com/Unleash/unleash.git
synced 2025-01-20 00:08:02 +01:00
feat: support updating existing projects without existing settings (#3358)
This commit is contained in:
parent
600d461278
commit
902ebefe7e
@ -221,19 +221,35 @@ class ProjectStore implements IProjectStore {
|
||||
return this.mapRow({ ...row[0], ...settingsRow[0] });
|
||||
}
|
||||
|
||||
private async hasProjectSettings(projectId: string): Promise<boolean> {
|
||||
const result = await this.db.raw(
|
||||
`SELECT EXISTS(SELECT 1 FROM ${SETTINGS_TABLE} WHERE project = ?) AS present`,
|
||||
[projectId],
|
||||
);
|
||||
const { present } = result.rows[0];
|
||||
return present;
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
|
||||
async update(data): Promise<void> {
|
||||
try {
|
||||
await this.db(TABLE)
|
||||
.where({ id: data.id })
|
||||
.update(this.fieldToRow(data));
|
||||
await this.db(SETTINGS_TABLE)
|
||||
.where({ project: data.id })
|
||||
.update({
|
||||
if (await this.hasProjectSettings(data.id)) {
|
||||
await this.db(SETTINGS_TABLE)
|
||||
.where({ project: data.id })
|
||||
.update({
|
||||
project_mode: data.mode,
|
||||
default_stickiness: data.defaultStickiness,
|
||||
});
|
||||
} else {
|
||||
await this.db(SETTINGS_TABLE).insert({
|
||||
project: data.id,
|
||||
project_mode: data.mode,
|
||||
default_stickiness: data.defaultStickiness,
|
||||
})
|
||||
.returning('*');
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
this.logger.error('Could not update project, error: ', err);
|
||||
}
|
||||
|
@ -236,6 +236,35 @@ test('should update project', async () => {
|
||||
expect(updatedProject.mode).toBe('protected');
|
||||
});
|
||||
|
||||
test('should update project without existing settings', async () => {
|
||||
const project = {
|
||||
id: 'test-update-legacy',
|
||||
name: 'New project',
|
||||
description: 'Blah',
|
||||
mode: 'open' as const,
|
||||
};
|
||||
|
||||
const updatedProject = {
|
||||
id: 'test-update-legacy',
|
||||
name: 'New name',
|
||||
description: 'Blah longer desc',
|
||||
mode: 'protected' as const,
|
||||
};
|
||||
|
||||
await projectService.createProject(project, user);
|
||||
await db
|
||||
.rawDatabase('project_settings')
|
||||
.del()
|
||||
.where({ project: project.id });
|
||||
await projectService.updateProject(updatedProject, user);
|
||||
|
||||
const readProject = await projectService.getProject(project.id);
|
||||
|
||||
expect(updatedProject.name).toBe(readProject.name);
|
||||
expect(updatedProject.description).toBe(readProject.description);
|
||||
expect(updatedProject.mode).toBe('protected');
|
||||
});
|
||||
|
||||
test('should give error when getting unknown project', async () => {
|
||||
try {
|
||||
await projectService.getProject('unknown');
|
||||
|
Loading…
Reference in New Issue
Block a user