adds unique has column because of mysql unique character limitation

Signed-off-by: Billy Stalnaker <bstalnaker@roadie.com>
This commit is contained in:
Billy Stalnaker
2025-03-25 13:14:22 -04:00
parent 4d772f6d74
commit 08972ee879
5 changed files with 63 additions and 15 deletions
@@ -13,29 +13,42 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const crypto = require('crypto');
exports.up = async function up(knex) {
await knex.schema.alterTable('user_settings', table => {
table.string('topic').nullable().after('origin');
});
await knex.schema.table('user_settings', table => {
table.string('settings_key_hash', 64).notNullable();
table.dropUnique([], 'user_settings_unique_idx');
});
await knex.schema.alterTable('user_settings', table => {
table.unique(['user(255)', 'channel(255)', 'origin(255)', 'topic(255)'], {
indexName: 'user_settings_unique_idx',
});
table.unique(['settings_key_hash'], 'user_settings_unique_idx');
});
const rows = await knex('user_settings').select('user', 'channel', 'origin');
for (const row of rows) {
const rawKey = `${row.user}|${row.channel}|${row.origin}|}`;
const hash = crypto.createHash('sha256').update(rawKey).digest('hex');
await knex('user_settings')
.where({
user: row.user,
channel: row.channel,
origin: row.origin,
topic: row.topic,
})
.update({ settings_key_hash: hash });
}
};
exports.down = async function down(knex) {
await knex.schema.table('user_settings', table => {
table.dropUnique([], 'user_settings_unique_idx');
table.dropColumn('settings_key_hash');
table.dropColumn('topic');
});
await knex.schema.alterTable('user_settings', table => {
table.dropColumn('topic');
table.unique(['user', 'channel', 'origin'], {
indexName: 'user_settings_unique_idx',
});