-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
854620c
commit 23c44d2
Showing
4 changed files
with
75 additions
and
8 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import Logger from '../../logger.js'; | ||
|
||
function needsMigration(db) { | ||
try { | ||
const hasNormalizedColumn = db.prepare(` | ||
SELECT COUNT(*) as count | ||
FROM pragma_table_info('users') | ||
WHERE name = 'username_normalized' | ||
`).get(); | ||
|
||
return hasNormalizedColumn.count === 0; | ||
} catch (error) { | ||
Logger.error('v1.5.0-usernames - Error checking migration status:', error); | ||
throw error; | ||
} | ||
} | ||
|
||
async function up_v1_5_0_usernames(db) { | ||
if (!needsMigration(db)) { | ||
Logger.debug('v1.5.0-usernames - Migration not needed'); | ||
return; | ||
} | ||
|
||
Logger.debug('v1.5.0-usernames - Starting migration...'); | ||
|
||
try { | ||
db.transaction(() => { | ||
db.exec(` | ||
ALTER TABLE users ADD COLUMN username_normalized TEXT; | ||
CREATE UNIQUE INDEX IF NOT EXISTS idx_users_username_normalized | ||
ON users(username_normalized COLLATE NOCASE); | ||
`); | ||
|
||
const users = db.prepare('SELECT id, username FROM users').all(); | ||
const updateStmt = db.prepare( | ||
'UPDATE users SET username_normalized = ? WHERE id = ?' | ||
); | ||
|
||
for (const user of users) { | ||
updateStmt.run(user.username.toLowerCase(), user.id); | ||
} | ||
})(); | ||
|
||
Logger.debug('v1.5.0-usernames - Migration completed successfully'); | ||
} catch (error) { | ||
Logger.error('v1.5.0-usernames - Migration failed:', error); | ||
throw error; | ||
} | ||
} | ||
|
||
export { up_v1_5_0_usernames }; |
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