-
-
Notifications
You must be signed in to change notification settings - Fork 730
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore:system user and events created by userid migrations #5612
Merged
daveleek
merged 12 commits into
main
from
chore/system-user-events-created-by-userid-migrations
Dec 22, 2023
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
205d844
chore: migration - add created-by-user-id column to events
daveleek 013b0d6
chore: add migration that adds is_system column and inserts system user
daveleek a1e2733
chore: remove pre-delete of -1337 user in up migration
daveleek c202573
chore: adding a system user shouldnt prevent adding default admin user
daveleek 3e85dfc
Update src/migrations/20231212094044-event-created-by-user-id.js
daveleek 125bc5a
Update src/migrations/20231212094044-event-created-by-user-id.js
daveleek 959991e
Merge branch 'main' into chore/system-user-events-created-by-userid-m…
daveleek b9090e4
chore: update migration file timestamp
daveleek e9c2892
chore: db migration rename to keep current
daveleek 004fd6f
chore: db migration test
daveleek fc5ca9b
chore: rename migration back to established name
daveleek 4207dd9
chore: filter out system users from activeaccounts
daveleek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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', '[email protected]', -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, | ||
); | ||
}; |
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,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', '[email protected]', 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'); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice 🏅 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even though I like
-1337
, I'm wondering if we should just use-1
instead.