mirror of
https://github.com/Unleash/unleash.git
synced 2025-02-23 00:22:19 +01:00
chore:system user and events created by userid migrations (#5612)
## About the changes Migrations for: - Adds column is_system to users - Inserts unleash_system_user id -1337 to users includes `is_system: false` in the activeUsers and activeAccounts where filter Tested by running: ` select * into users_pre_check from users where id > -1; delete from users where id > -1; ` before starting unleash, then inspecting users table after unleash has started and verifying that an 'admin' user has been created. --------- Co-authored-by: Christopher Kolstad <chriswk@getunleash.ai>
This commit is contained in:
parent
d8a7d76f7b
commit
1dadd23594
@ -74,6 +74,7 @@ export class AccountStore implements IAccountStore {
|
||||
activeAccounts(): any {
|
||||
return this.db(TABLE).where({
|
||||
deleted_at: null,
|
||||
is_system: false,
|
||||
});
|
||||
}
|
||||
|
||||
|
@ -116,6 +116,7 @@ class UserStore implements IUserStore {
|
||||
return this.db(TABLE).where({
|
||||
deleted_at: null,
|
||||
is_service: false,
|
||||
is_system: false,
|
||||
});
|
||||
}
|
||||
|
||||
|
24
src/migrations/20231222071533-unleash-system-user.js
Normal file
24
src/migrations/20231222071533-unleash-system-user.js
Normal file
@ -0,0 +1,24 @@
|
||||
'use strict';
|
||||
|
||||
exports.up = function (db, callback) {
|
||||
db.runSql(
|
||||
`
|
||||
ALTER TABLE users ADD COLUMN IF NOT EXISTS is_system BOOLEAN NOT NULL DEFAULT FALSE;
|
||||
INSERT INTO users
|
||||
(id, name, username, email, created_by_user_id, is_system)
|
||||
VALUES
|
||||
(-1337, 'Unleash System', 'unleash_system_user', 'system@getunleash.io', -1337, true);
|
||||
`,
|
||||
callback,
|
||||
);
|
||||
};
|
||||
|
||||
exports.down = function (db, callback) {
|
||||
db.runSql(
|
||||
`
|
||||
ALTER TABLE users DROP COLUMN IF EXISTS is_system;
|
||||
DELETE FROM users WHERE id = -1337;
|
||||
`,
|
||||
callback,
|
||||
);
|
||||
};
|
76
src/test/e2e/system-user-migration.test.ts
Normal file
76
src/test/e2e/system-user-migration.test.ts
Normal file
@ -0,0 +1,76 @@
|
||||
import { getDbConfig } from './helpers/database-config';
|
||||
import { createTestConfig } from '../config/test-config';
|
||||
import { getInstance } from 'db-migrate';
|
||||
import { log } from 'db-migrate-shared';
|
||||
import { Client } from 'pg';
|
||||
import { IDBOption } from 'lib/types';
|
||||
|
||||
log.setLogLevel('error');
|
||||
|
||||
async function initSchema(db: IDBOption): Promise<void> {
|
||||
const client = new Client(db);
|
||||
await client.connect();
|
||||
await client.query(`DROP SCHEMA IF EXISTS ${db.schema} CASCADE`);
|
||||
await client.query(`CREATE SCHEMA IF NOT EXISTS ${db.schema}`);
|
||||
await client.end();
|
||||
}
|
||||
|
||||
test('System user creation migration correctly sets is_system', async () => {
|
||||
jest.setTimeout(15000);
|
||||
const config = createTestConfig({
|
||||
db: {
|
||||
...getDbConfig(),
|
||||
pool: { min: 1, max: 4 },
|
||||
schema: 'system_user_migration_test',
|
||||
ssl: false,
|
||||
},
|
||||
});
|
||||
|
||||
await initSchema(config.db);
|
||||
|
||||
const e2e = {
|
||||
...config.db,
|
||||
connectionTimeoutMillis: 2000,
|
||||
};
|
||||
|
||||
// disable Intellij/WebStorm from setting verbose CLI argument to db-migrator
|
||||
process.argv = process.argv.filter((it) => !it.includes('--verbose'));
|
||||
const dbm = getInstance(true, {
|
||||
cwd: `${__dirname}/../../`, // relative to src/test/e2e
|
||||
config: { e2e },
|
||||
env: 'e2e',
|
||||
});
|
||||
|
||||
// Run all migrations up to, and including, this one, the last one before the system user migration
|
||||
await dbm.up('20231221143955-feedback-table.js');
|
||||
|
||||
// Set up the test data
|
||||
const client = new Client(config.db);
|
||||
await client.connect();
|
||||
|
||||
await client.query(`
|
||||
INSERT INTO "system_user_migration_test"."users"
|
||||
(name, username, email, created_by_user_id)
|
||||
VALUES
|
||||
('Test Person', 'testperson', 'testperson@getunleash.io', 1);
|
||||
`);
|
||||
|
||||
// Run the migration
|
||||
await dbm.up('20231222071533-unleash-system-user.js');
|
||||
|
||||
// Check the results
|
||||
const { rows: userResults } = await client.query(`
|
||||
SELECT * FROM "system_user_migration_test"."users" ORDER BY id;
|
||||
`);
|
||||
|
||||
await client.end();
|
||||
await dbm.reset();
|
||||
|
||||
expect(userResults.length).toEqual(2);
|
||||
expect(userResults[0].is_system).toEqual(true);
|
||||
expect(userResults[0].id).toEqual(-1337);
|
||||
expect(userResults[0].username).toEqual('unleash_system_user');
|
||||
expect(userResults[1].is_system).toEqual(false);
|
||||
expect(userResults[1].id).toEqual(1);
|
||||
expect(userResults[1].username).toEqual('testperson');
|
||||
});
|
Loading…
Reference in New Issue
Block a user