mirror of
https://github.com/Unleash/unleash.git
synced 2025-06-04 01:18:20 +02:00
chore: add a new project column to segments table (#3263)
## About the changes Adds a migration and persistence layer with a new column `segment_project_id` to bind a segment to a project.
This commit is contained in:
parent
e4b84bc2a3
commit
98d462db27
@ -15,7 +15,6 @@ import EventEmitter from 'events';
|
|||||||
import FeatureToggleStore from './feature-toggle-store';
|
import FeatureToggleStore from './feature-toggle-store';
|
||||||
import { ensureStringValue } from '../util/ensureStringValue';
|
import { ensureStringValue } from '../util/ensureStringValue';
|
||||||
import { mapValues } from '../util/map-values';
|
import { mapValues } from '../util/map-values';
|
||||||
import { IFlagResolver } from '../types/experimental';
|
|
||||||
import Raw = Knex.Raw;
|
import Raw = Knex.Raw;
|
||||||
import { Db } from './db';
|
import { Db } from './db';
|
||||||
|
|
||||||
@ -51,28 +50,16 @@ export default class FeatureToggleClientStore
|
|||||||
|
|
||||||
private logger: Logger;
|
private logger: Logger;
|
||||||
|
|
||||||
private inlineSegmentConstraints: boolean;
|
|
||||||
|
|
||||||
private timer: Function;
|
private timer: Function;
|
||||||
|
|
||||||
private flagResolver: IFlagResolver;
|
constructor(db: Db, eventBus: EventEmitter, getLogger: LogProvider) {
|
||||||
|
|
||||||
constructor(
|
|
||||||
db: Db,
|
|
||||||
eventBus: EventEmitter,
|
|
||||||
getLogger: LogProvider,
|
|
||||||
inlineSegmentConstraints: boolean,
|
|
||||||
flagResolver: IFlagResolver,
|
|
||||||
) {
|
|
||||||
this.db = db;
|
this.db = db;
|
||||||
this.logger = getLogger('feature-toggle-client-store.ts');
|
this.logger = getLogger('feature-toggle-client-store.ts');
|
||||||
this.inlineSegmentConstraints = inlineSegmentConstraints;
|
|
||||||
this.timer = (action) =>
|
this.timer = (action) =>
|
||||||
metricsHelper.wrapTimer(eventBus, DB_TIME, {
|
metricsHelper.wrapTimer(eventBus, DB_TIME, {
|
||||||
store: 'feature-toggle',
|
store: 'feature-toggle',
|
||||||
action,
|
action,
|
||||||
});
|
});
|
||||||
this.flagResolver = flagResolver;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private async getAll({
|
private async getAll({
|
||||||
|
@ -85,8 +85,6 @@ export const createStores = (
|
|||||||
db,
|
db,
|
||||||
eventBus,
|
eventBus,
|
||||||
getLogger,
|
getLogger,
|
||||||
config.inlineSegmentConstraints,
|
|
||||||
config.flagResolver,
|
|
||||||
),
|
),
|
||||||
environmentStore: new EnvironmentStore(db, eventBus, getLogger),
|
environmentStore: new EnvironmentStore(db, eventBus, getLogger),
|
||||||
featureTagStore: new FeatureTagStore(db, eventBus, getLogger),
|
featureTagStore: new FeatureTagStore(db, eventBus, getLogger),
|
||||||
|
@ -17,6 +17,7 @@ const COLUMNS = [
|
|||||||
'id',
|
'id',
|
||||||
'name',
|
'name',
|
||||||
'description',
|
'description',
|
||||||
|
'segment_project_id',
|
||||||
'created_by',
|
'created_by',
|
||||||
'created_at',
|
'created_at',
|
||||||
'constraints',
|
'constraints',
|
||||||
@ -26,6 +27,7 @@ interface ISegmentRow {
|
|||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: string;
|
||||||
description?: string;
|
description?: string;
|
||||||
|
segment_project_id?: string;
|
||||||
created_by?: string;
|
created_by?: string;
|
||||||
created_at?: Date;
|
created_at?: Date;
|
||||||
constraints: IConstraint[];
|
constraints: IConstraint[];
|
||||||
@ -66,6 +68,7 @@ export default class SegmentStore implements ISegmentStore {
|
|||||||
id: segment.id,
|
id: segment.id,
|
||||||
name: segment.name,
|
name: segment.name,
|
||||||
description: segment.description,
|
description: segment.description,
|
||||||
|
segment_project_id: segment.project,
|
||||||
constraints: JSON.stringify(segment.constraints),
|
constraints: JSON.stringify(segment.constraints),
|
||||||
created_by: user.username || user.email,
|
created_by: user.username || user.email,
|
||||||
})
|
})
|
||||||
@ -80,6 +83,7 @@ export default class SegmentStore implements ISegmentStore {
|
|||||||
.update({
|
.update({
|
||||||
name: segment.name,
|
name: segment.name,
|
||||||
description: segment.description,
|
description: segment.description,
|
||||||
|
segment_project_id: segment.project,
|
||||||
constraints: JSON.stringify(segment.constraints),
|
constraints: JSON.stringify(segment.constraints),
|
||||||
})
|
})
|
||||||
.returning(COLUMNS);
|
.returning(COLUMNS);
|
||||||
@ -199,6 +203,7 @@ export default class SegmentStore implements ISegmentStore {
|
|||||||
id: row.id,
|
id: row.id,
|
||||||
name: row.name,
|
name: row.name,
|
||||||
description: row.description,
|
description: row.description,
|
||||||
|
project: row.segment_project_id,
|
||||||
constraints: row.constraints,
|
constraints: row.constraints,
|
||||||
createdBy: row.created_by,
|
createdBy: row.created_by,
|
||||||
createdAt: row.created_at,
|
createdAt: row.created_at,
|
||||||
|
@ -51,8 +51,6 @@ export const createFeatureToggleService = (
|
|||||||
db,
|
db,
|
||||||
eventBus,
|
eventBus,
|
||||||
getLogger,
|
getLogger,
|
||||||
config.inlineSegmentConstraints,
|
|
||||||
flagResolver,
|
|
||||||
);
|
);
|
||||||
const projectStore = new ProjectStore(
|
const projectStore = new ProjectStore(
|
||||||
db,
|
db,
|
||||||
|
@ -384,6 +384,7 @@ export interface ISegment {
|
|||||||
id: number;
|
id: number;
|
||||||
name: string;
|
name: string;
|
||||||
description?: string;
|
description?: string;
|
||||||
|
project?: string;
|
||||||
constraints: IConstraint[];
|
constraints: IConstraint[];
|
||||||
createdBy?: string;
|
createdBy?: string;
|
||||||
createdAt: Date;
|
createdAt: Date;
|
||||||
|
@ -0,0 +1,13 @@
|
|||||||
|
exports.up = function (db, cb) {
|
||||||
|
db.runSql(
|
||||||
|
`ALTER TABLE segments ADD COLUMN segment_project_id varchar(255) REFERENCES projects(id) ON DELETE CASCADE;`,
|
||||||
|
cb,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
exports.down = function (db, cb) {
|
||||||
|
db.runSql(
|
||||||
|
`ALTER TABLE segments DROP COLUMN IF EXISTS segment_project_id;`,
|
||||||
|
cb,
|
||||||
|
);
|
||||||
|
};
|
32
src/test/e2e/stores/feature-toggle-client-store.e2e.test.ts
Normal file
32
src/test/e2e/stores/feature-toggle-client-store.e2e.test.ts
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
import dbInit from '../helpers/database-init';
|
||||||
|
import getLogger from '../../fixtures/no-logger';
|
||||||
|
import { setupApp } from '../helpers/test-helper';
|
||||||
|
|
||||||
|
let stores;
|
||||||
|
let app;
|
||||||
|
let db;
|
||||||
|
let featureToggleClientStore;
|
||||||
|
|
||||||
|
beforeAll(async () => {
|
||||||
|
getLogger.setMuteError(true);
|
||||||
|
db = await dbInit('feature_toggle_client_store_serial', getLogger);
|
||||||
|
app = await setupApp(db.stores);
|
||||||
|
stores = db.stores;
|
||||||
|
featureToggleClientStore = stores.featureToggleClientStore;
|
||||||
|
});
|
||||||
|
|
||||||
|
afterAll(async () => {
|
||||||
|
await app.destroy();
|
||||||
|
await db.destroy();
|
||||||
|
});
|
||||||
|
|
||||||
|
test('should be able to fetch client toggles', async () => {
|
||||||
|
const response = await app.request
|
||||||
|
.post('/api/admin/state/import?drop=true')
|
||||||
|
.attach('file', 'src/test/examples/exported-segments.json');
|
||||||
|
|
||||||
|
expect(response.status).toBe(202);
|
||||||
|
|
||||||
|
const clientToggles = await featureToggleClientStore.getClient();
|
||||||
|
expect(clientToggles).toHaveLength(1);
|
||||||
|
});
|
Loading…
Reference in New Issue
Block a user