@@ -34,7 +34,55 @@ async function getColumnType(tableName: string, columnName: string): Promise<str
3434 return rows [ 0 ] ?. column_type ?? null ;
3535}
3636
37+ async function hasConstraint ( tableName : string , constraintName : string ) : Promise < boolean > {
38+ const rows = await queryRows < { cnt : number } [ ] > (
39+ `SELECT COUNT(*) AS cnt
40+ FROM information_schema.TABLE_CONSTRAINTS
41+ WHERE TABLE_SCHEMA = DATABASE()
42+ AND TABLE_NAME = ?
43+ AND CONSTRAINT_NAME = ?` ,
44+ [ tableName , constraintName ]
45+ ) ;
46+
47+ return Number ( rows [ 0 ] ?. cnt ?? 0 ) > 0 ;
48+ }
49+
3750async function ensureSchemaUpgrades ( ) : Promise < void > {
51+ await execute (
52+ `CREATE TABLE IF NOT EXISTS account_folders (
53+ id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
54+ user_id BIGINT UNSIGNED NOT NULL,
55+ name VARCHAR(48) NOT NULL,
56+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
57+ updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
58+ UNIQUE KEY uq_account_folders_user_name (user_id, name),
59+ CONSTRAINT fk_account_folders_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
60+ )`
61+ ) ;
62+
63+ await execute (
64+ `CREATE TABLE IF NOT EXISTS account_tags (
65+ id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
66+ user_id BIGINT UNSIGNED NOT NULL,
67+ name VARCHAR(48) NOT NULL,
68+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
69+ updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
70+ UNIQUE KEY uq_account_tags_user_name (user_id, name),
71+ CONSTRAINT fk_account_tags_user FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
72+ )`
73+ ) ;
74+
75+ await execute (
76+ `CREATE TABLE IF NOT EXISTS account_tag_assignments (
77+ account_id BIGINT UNSIGNED NOT NULL,
78+ tag_id BIGINT UNSIGNED NOT NULL,
79+ created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
80+ PRIMARY KEY (account_id, tag_id),
81+ CONSTRAINT fk_account_tag_assignments_account FOREIGN KEY (account_id) REFERENCES user_accounts(id) ON DELETE CASCADE,
82+ CONSTRAINT fk_account_tag_assignments_tag FOREIGN KEY (tag_id) REFERENCES account_tags(id) ON DELETE CASCADE
83+ )`
84+ ) ;
85+
3886 if ( ! ( await hasColumn ( 'user_accounts' , 'encrypted_revocation_code' ) ) ) {
3987 await execute (
4088 'ALTER TABLE user_accounts ADD COLUMN encrypted_revocation_code LONGTEXT NULL AFTER encrypted_ma'
@@ -47,6 +95,16 @@ async function ensureSchemaUpgrades(): Promise<void> {
4795 ) ;
4896 }
4997
98+ if ( ! ( await hasColumn ( 'user_accounts' , 'folder_id' ) ) ) {
99+ await execute ( 'ALTER TABLE user_accounts ADD COLUMN folder_id BIGINT UNSIGNED NULL AFTER steamid' ) ;
100+ }
101+
102+ if ( ! ( await hasConstraint ( 'user_accounts' , 'fk_user_accounts_folder' ) ) ) {
103+ await execute (
104+ 'ALTER TABLE user_accounts ADD CONSTRAINT fk_user_accounts_folder FOREIGN KEY (folder_id) REFERENCES account_folders(id) ON DELETE SET NULL'
105+ ) ;
106+ }
107+
50108 const hasLegacyAutoConfirm = await hasColumn ( 'user_accounts' , 'auto_confirm' ) ;
51109 const hasAutoConfirmTrades = await hasColumn ( 'user_accounts' , 'auto_confirm_trades' ) ;
52110 const hasAutoConfirmLogins = await hasColumn ( 'user_accounts' , 'auto_confirm_logins' ) ;
0 commit comments