Skip to content
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

Fix error when seeding in AWS non-production #1400

Merged
merged 4 commits into from
Oct 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 90 additions & 14 deletions db/seeds/09-users.seed.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,29 @@ async function seed () {
const password = _generateHashedPassword()

for (const user of users) {
await _upsert(user, password)
const exists = await _exists(user)

if (exists) {
await _update(user, password)
} else {
await _insert(user, password)
}
}
}

async function _exists (user) {
const { application, username } = user

const result = await UserModel.query()
.select('id')
.where('application', application)
.andWhere('username', username)
.limit(1)
.first()

return !!result
}

function _generateHashedPassword () {
// 10 is the number of salt rounds to perform to generate the salt. The legacy code uses
// const salt = bcrypt.genSaltSync(10) to pre-generate the salt before passing it to hashSync(). But this is
Expand All @@ -31,20 +50,77 @@ function _generateHashedPassword () {
return bcrypt.hashSync(DatabaseConfig.defaultUserPassword, 10)
}

async function _upsert (user, password) {
async function _idInUse (id) {
const result = await UserModel.query()
.findById(id)

return !!result
}

async function _insert (user, password) {
const {
application,
badLogins,
enabled,
id,
lastLogin,
resetGuid,
resetGuidCreatedAt,
resetRequired,
username
} = user

// NOTE: Seeding users is a pain (!) because of the previous teams choice to use a custom sequence for the ID instead
// of sticking with UUIDs. This means it is possible that, for example, a user with
//
// `username = '[email protected]' && application = 'water_admin'`
//
// does not exist. _But_ a user with ID 100000 does! So, we do want to insert our record, but we can't use the ID
// because it is already in use. We only really face this problem when running the seed in our AWS environments.
const idInUse = await _idInUse(id)

if (idInUse) {
return UserModel.query().insert({
application,
badLogins,
enabled,
lastLogin,
password,
resetGuid,
resetGuidCreatedAt,
resetRequired,
username
})
}

return UserModel.query().insert({ ...user, password })
}

async function _update (user, password) {
const {
application,
badLogins,
enabled,
lastLogin,
resetGuid,
resetGuidCreatedAt,
resetRequired,
username
} = user

return UserModel.query()
.insert({ ...user, password, updatedAt: timestampForPostgres() })
.onConflict(['application', 'username'])
.merge([
'badLogins',
'enabled',
'lastLogin',
'password',
'resetGuid',
'resetGuidCreatedAt',
'resetRequired',
'updatedAt'
])
.patch({
badLogins,
enabled,
lastLogin,
password,
resetGuid,
resetGuidCreatedAt,
resetRequired,
updatedAt: timestampForPostgres()
})
.where('application', application)
.andWhere('username', username)
}

module.exports = {
Expand Down
Loading