-
-
Notifications
You must be signed in to change notification settings - Fork 736
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: implement created_by_user_id in features (#5994)
## About the changes Adds a scheduled task that every 5 seconds updates 500 entries in the features table setting `created_by_user_id`. It does this by looking at the related event, checks created_by and joins users table for match on username or email, and joins api_tokens table on username matches. Then picks either a users id if set, or uses -42 (admin token user)
- Loading branch information
Showing
8 changed files
with
259 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
195 changes: 195 additions & 0 deletions
195
src/test/e2e/features-created-by-user-id-data-migration.e2e.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
import { createFeatureToggleService } from '../../lib/features'; | ||
import { EventService, FeatureToggleService } from '../../lib/services'; | ||
import { | ||
ADMIN_TOKEN_USER, | ||
IUnleashConfig, | ||
IUnleashStores, | ||
} from '../../lib/types'; | ||
import { createTestConfig } from '../config/test-config'; | ||
import dbInit, { ITestDb } from './helpers/database-init'; | ||
|
||
let stores: IUnleashStores; | ||
let db: ITestDb; | ||
let service: FeatureToggleService; | ||
let eventService: EventService; | ||
let unleashConfig: IUnleashConfig; | ||
|
||
beforeAll(async () => { | ||
const config = createTestConfig(); | ||
db = await dbInit( | ||
'features_created_by_user_id_migration', | ||
config.getLogger, | ||
); | ||
unleashConfig = config; | ||
stores = db.stores; | ||
|
||
service = createFeatureToggleService(db.rawDatabase, config); | ||
|
||
eventService = new EventService(stores, config); | ||
}); | ||
|
||
afterAll(async () => { | ||
await db.rawDatabase('features').del(); | ||
await db.rawDatabase('events').del(); | ||
await db.rawDatabase('users').del(); | ||
await db.destroy(); | ||
}); | ||
|
||
beforeEach(async () => { | ||
await db.rawDatabase('features').del(); | ||
await db.rawDatabase('events').del(); | ||
await db.rawDatabase('users').del(); | ||
}); | ||
|
||
test('should set created_by_user_id on features', async () => { | ||
for (let i = 0; i < 100; i++) { | ||
await db.rawDatabase('features').insert({ | ||
name: `feature${i}`, | ||
type: 'release', | ||
project: 'default', | ||
description: '--created_by_test--', | ||
}); | ||
} | ||
|
||
await db.rawDatabase('users').insert({ | ||
username: 'test1', | ||
}); | ||
await db.rawDatabase('users').insert({ | ||
username: 'test2', | ||
}); | ||
await db.rawDatabase('users').insert({ | ||
username: 'test3', | ||
}); | ||
await db.rawDatabase('users').insert({ | ||
username: 'test4', | ||
}); | ||
|
||
for (let i = 0; i < 25; i++) { | ||
await db.rawDatabase('events').insert({ | ||
type: 'feature-created', | ||
created_by: 'test1', | ||
feature_name: `feature${i}`, | ||
data: `{"name":"feature${i}","description":null,"type":"release","project":"default","stale":false,"createdAt":"2024-01-08T10:36:32.866Z","lastSeenAt":null,"impressionData":false,"archivedAt":null,"archived":false}`, | ||
}); | ||
} | ||
|
||
for (let i = 25; i < 50; i++) { | ||
await db.rawDatabase('events').insert({ | ||
type: 'feature-created', | ||
created_by: 'test2', | ||
feature_name: `feature${i}`, | ||
data: `{"name":"feature${i}","description":null,"type":"release","project":"default","stale":false,"createdAt":"2024-01-08T10:36:32.866Z","lastSeenAt":null,"impressionData":false,"archivedAt":null,"archived":false}`, | ||
}); | ||
} | ||
|
||
for (let i = 50; i < 75; i++) { | ||
await db.rawDatabase('events').insert({ | ||
type: 'feature-created', | ||
created_by: 'test3', | ||
feature_name: `feature${i}`, | ||
data: `{"name":"feature${i}","description":null,"type":"release","project":"default","stale":false,"createdAt":"2024-01-08T10:36:32.866Z","lastSeenAt":null,"impressionData":false,"archivedAt":null,"archived":false}`, | ||
}); | ||
} | ||
|
||
for (let i = 75; i < 100; i++) { | ||
await db.rawDatabase('events').insert({ | ||
type: 'feature-created', | ||
created_by: 'test4', | ||
feature_name: `feature${i}`, | ||
data: `{"name":"feature${i}","description":null,"type":"release","project":"default","stale":false,"createdAt":"2024-01-08T10:36:32.866Z","lastSeenAt":null,"impressionData":false,"archivedAt":null,"archived":false}`, | ||
}); | ||
} | ||
|
||
await stores.featureToggleStore.setCreatedByUserId(200); | ||
|
||
const features = await db.rawDatabase('features').select('*'); | ||
const notSet = features.filter( | ||
(f) => !f.created_by_user_id && f.description === '--created_by_test--', | ||
); | ||
const test1 = features.filter((f) => f.created_by_user_id === 1); | ||
const test2 = features.filter((f) => f.created_by_user_id === 2); | ||
const test3 = features.filter((f) => f.created_by_user_id === 3); | ||
const test4 = features.filter((f) => f.created_by_user_id === 4); | ||
expect(notSet).toHaveLength(0); | ||
expect(test1).toHaveLength(25); | ||
expect(test2).toHaveLength(25); | ||
expect(test3).toHaveLength(25); | ||
expect(test4).toHaveLength(25); | ||
}); | ||
|
||
test('admin tokens get populated to admin token user', async () => { | ||
for (let i = 0; i < 5; i++) { | ||
await db.rawDatabase('features').insert({ | ||
name: `feature${i}`, | ||
type: 'release', | ||
project: 'default', | ||
description: '--created_by_test--', | ||
}); | ||
} | ||
|
||
await db.rawDatabase('users').insert({ | ||
username: 'input1', | ||
}); | ||
|
||
await db.rawDatabase('api_tokens').insert({ | ||
secret: 'token1', | ||
username: 'adm-token', | ||
type: 'admin', | ||
environment: 'default', | ||
token_name: 'admin-token', | ||
}); | ||
|
||
await db.rawDatabase('events').insert({ | ||
type: 'feature-created', | ||
created_by: 'input1', | ||
feature_name: 'feature0', | ||
data: `{"name":"feature0","description":null,"type":"release","project":"default","stale":false,"createdAt":"2024-01-08T10:36:32.866Z","lastSeenAt":null,"impressionData":false,"archivedAt":null,"archived":false}`, | ||
}); | ||
|
||
await db.rawDatabase('events').insert({ | ||
type: 'feature-created', | ||
created_by: 'input1', | ||
feature_name: 'feature1', | ||
data: `{"name":"feature1","description":null,"type":"release","project":"default","stale":false,"createdAt":"2024-01-08T10:36:32.866Z","lastSeenAt":null,"impressionData":false,"archivedAt":null,"archived":false}`, | ||
}); | ||
|
||
await db.rawDatabase('events').insert({ | ||
type: 'feature-created', | ||
created_by: 'adm-token', | ||
feature_name: 'feature2', | ||
data: `{"name":"feature2","description":null,"type":"release","project":"default","stale":false,"createdAt":"2024-01-08T10:36:32.866Z","lastSeenAt":null,"impressionData":false,"archivedAt":null,"archived":false}`, | ||
}); | ||
|
||
await db.rawDatabase('events').insert({ | ||
type: 'feature-created', | ||
created_by: 'deleted-user', | ||
feature_name: 'feature3', | ||
data: `{"name":"feature3","description":null,"type":"release","project":"default","stale":false,"createdAt":"2024-01-08T10:36:32.866Z","lastSeenAt":null,"impressionData":false,"archivedAt":null,"archived":false}`, | ||
}); | ||
|
||
await db.rawDatabase('events').insert({ | ||
type: 'feature-created', | ||
created_by: 'adm-token', | ||
feature_name: 'feature4', | ||
data: `{"name":"feature4","description":null,"type":"release","project":"default","stale":false,"createdAt":"2024-01-08T10:36:32.866Z","lastSeenAt":null,"impressionData":false,"archivedAt":null,"archived":false}`, | ||
}); | ||
|
||
await stores.featureToggleStore.setCreatedByUserId(200); | ||
|
||
const user = await db | ||
.rawDatabase('users') | ||
.where({ username: 'input1' }) | ||
.first('id'); | ||
|
||
const features = await db.rawDatabase('features').select('*'); | ||
const notSet = features.filter( | ||
(f) => !f.created_by_user_id && f.description === '--created_by_test--', | ||
); | ||
const test1 = features.filter((f) => f.created_by_user_id === user.id); | ||
const test2 = features.filter( | ||
(f) => f.created_by_user_id === ADMIN_TOKEN_USER.id, | ||
); | ||
expect(notSet).toHaveLength(1); | ||
expect(test1).toHaveLength(2); | ||
expect(test2).toHaveLength(2); | ||
}); |