1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01: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:
Gastón Fournier 2023-03-07 14:56:20 +01:00 committed by GitHub
parent e4b84bc2a3
commit 98d462db27
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 52 additions and 18 deletions

View File

@ -15,7 +15,6 @@ import EventEmitter from 'events';
import FeatureToggleStore from './feature-toggle-store';
import { ensureStringValue } from '../util/ensureStringValue';
import { mapValues } from '../util/map-values';
import { IFlagResolver } from '../types/experimental';
import Raw = Knex.Raw;
import { Db } from './db';
@ -51,28 +50,16 @@ export default class FeatureToggleClientStore
private logger: Logger;
private inlineSegmentConstraints: boolean;
private timer: Function;
private flagResolver: IFlagResolver;
constructor(
db: Db,
eventBus: EventEmitter,
getLogger: LogProvider,
inlineSegmentConstraints: boolean,
flagResolver: IFlagResolver,
) {
constructor(db: Db, eventBus: EventEmitter, getLogger: LogProvider) {
this.db = db;
this.logger = getLogger('feature-toggle-client-store.ts');
this.inlineSegmentConstraints = inlineSegmentConstraints;
this.timer = (action) =>
metricsHelper.wrapTimer(eventBus, DB_TIME, {
store: 'feature-toggle',
action,
});
this.flagResolver = flagResolver;
}
private async getAll({

View File

@ -85,8 +85,6 @@ export const createStores = (
db,
eventBus,
getLogger,
config.inlineSegmentConstraints,
config.flagResolver,
),
environmentStore: new EnvironmentStore(db, eventBus, getLogger),
featureTagStore: new FeatureTagStore(db, eventBus, getLogger),

View File

@ -17,6 +17,7 @@ const COLUMNS = [
'id',
'name',
'description',
'segment_project_id',
'created_by',
'created_at',
'constraints',
@ -26,6 +27,7 @@ interface ISegmentRow {
id: number;
name: string;
description?: string;
segment_project_id?: string;
created_by?: string;
created_at?: Date;
constraints: IConstraint[];
@ -66,6 +68,7 @@ export default class SegmentStore implements ISegmentStore {
id: segment.id,
name: segment.name,
description: segment.description,
segment_project_id: segment.project,
constraints: JSON.stringify(segment.constraints),
created_by: user.username || user.email,
})
@ -80,6 +83,7 @@ export default class SegmentStore implements ISegmentStore {
.update({
name: segment.name,
description: segment.description,
segment_project_id: segment.project,
constraints: JSON.stringify(segment.constraints),
})
.returning(COLUMNS);
@ -199,6 +203,7 @@ export default class SegmentStore implements ISegmentStore {
id: row.id,
name: row.name,
description: row.description,
project: row.segment_project_id,
constraints: row.constraints,
createdBy: row.created_by,
createdAt: row.created_at,

View File

@ -51,8 +51,6 @@ export const createFeatureToggleService = (
db,
eventBus,
getLogger,
config.inlineSegmentConstraints,
flagResolver,
);
const projectStore = new ProjectStore(
db,

View File

@ -384,6 +384,7 @@ export interface ISegment {
id: number;
name: string;
description?: string;
project?: string;
constraints: IConstraint[];
createdBy?: string;
createdAt: Date;

View File

@ -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,
);
};

View 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);
});