Skip to content

Commit

Permalink
chore: add a new project column to segments table (#3263)
Browse files Browse the repository at this point in the history
## About the changes
Adds a migration and persistence layer with a new column
`segment_project_id` to bind a segment to a project.
  • Loading branch information
gastonfournier committed Mar 7, 2023
1 parent e4b84bc commit 98d462d
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 18 deletions.
15 changes: 1 addition & 14 deletions src/lib/db/feature-toggle-client-store.ts
Expand Up @@ -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';

Expand Down Expand Up @@ -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({
Expand Down
2 changes: 0 additions & 2 deletions src/lib/db/index.ts
Expand Up @@ -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),
Expand Down
5 changes: 5 additions & 0 deletions src/lib/db/segment-store.ts
Expand Up @@ -17,6 +17,7 @@ const COLUMNS = [
'id',
'name',
'description',
'segment_project_id',
'created_by',
'created_at',
'constraints',
Expand All @@ -26,6 +27,7 @@ interface ISegmentRow {
id: number;
name: string;
description?: string;
segment_project_id?: string;
created_by?: string;
created_at?: Date;
constraints: IConstraint[];
Expand Down Expand Up @@ -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,
})
Expand All @@ -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);
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 0 additions & 2 deletions src/lib/features/feature-toggle/createFeatureToggleService.ts
Expand Up @@ -51,8 +51,6 @@ export const createFeatureToggleService = (
db,
eventBus,
getLogger,
config.inlineSegmentConstraints,
flagResolver,
);
const projectStore = new ProjectStore(
db,
Expand Down
1 change: 1 addition & 0 deletions src/lib/types/model.ts
Expand Up @@ -384,6 +384,7 @@ export interface ISegment {
id: number;
name: string;
description?: string;
project?: string;
constraints: IConstraint[];
createdBy?: string;
createdAt: Date;
Expand Down
13 changes: 13 additions & 0 deletions src/migrations/20230306103400-add-project-column-to-segments.js
@@ -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 changes: 32 additions & 0 deletions src/test/e2e/stores/feature-toggle-client-store.e2e.test.ts
@@ -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);
});

0 comments on commit 98d462d

Please sign in to comment.