Improved socials table.

This commit is contained in:
DebaucheryLibrarian 2024-11-04 02:37:45 +01:00
parent 5ae2f8e3f4
commit a487f21059
2 changed files with 21 additions and 10 deletions

View File

@ -3,32 +3,43 @@ exports.up = async (knex) => {
table.dropUnique(['url', 'actor_id', 'profile_id']); table.dropUnique(['url', 'actor_id', 'profile_id']);
table.dropColumn('profile_id'); table.dropColumn('profile_id');
table.string('username'); table.string('handle');
table.unique(['actor_id', 'platform', 'username']); table.boolean('is_broken')
table.unique(['actor_id', 'url']); .notNullable()
.defaultTo(false);
table.datetime('pinged_at');
table.datetime('verified_at'); table.datetime('verified_at');
table.unique(['actor_id', 'platform', 'handle']);
table.unique(['actor_id', 'url']);
}); });
await knex.raw('ALTER TABLE actors_social ADD CONSTRAINT actors_url_or_username CHECK (num_nulls(username, url) = 1);'); await knex.raw('ALTER TABLE actors_social ADD CONSTRAINT socials_url_or_handle CHECK (num_nulls(handle, url) = 1);');
await knex.raw('ALTER TABLE actors_social ADD CONSTRAINT actors_username_and_platform CHECK (num_nulls(platform, username) = 2 or num_nulls(platform, username) = 0);'); await knex.raw('ALTER TABLE actors_social ADD CONSTRAINT socials_handle_and_platform CHECK (num_nulls(platform, handle) = 2 or num_nulls(platform, handle) = 0);');
await knex.schema.renameTable('actors_social', 'actors_socials');
}; };
exports.down = async (knex) => { exports.down = async (knex) => {
await knex.raw('ALTER TABLE actors_social DROP CONSTRAINT actors_url_or_username;'); await knex.raw('ALTER TABLE actors_socials DROP CONSTRAINT socials_url_or_handle;');
await knex.raw('ALTER TABLE actors_social DROP CONSTRAINT actors_username_and_platform;'); await knex.raw('ALTER TABLE actors_socials DROP CONSTRAINT socials_handle_and_platform;');
await knex.schema.renameTable('actors_socials', 'actors_social');
await knex.schema.alterTable('actors_social', (table) => { await knex.schema.alterTable('actors_social', (table) => {
table.dropUnique(['actor_id', 'platform', 'username']); table.dropUnique(['actor_id', 'platform', 'handle']);
table.dropUnique(['actor_id', 'url']); table.dropUnique(['actor_id', 'url']);
table.integer('profile_id') table.integer('profile_id')
.references('id') .references('id')
.inTable('actors_profiles'); .inTable('actors_profiles');
table.dropColumn('username'); table.dropColumn('handle');
table.dropColumn('verified_at'); table.dropColumn('verified_at');
table.dropColumn('pinged_at');
table.dropColumn('is_broken');
table.unique(['url', 'actor_id', 'profile_id']); table.unique(['url', 'actor_id', 'profile_id']);
}); });

View File

@ -709,7 +709,7 @@ async function associateSocials(profiles) {
return; return;
} }
await knex('actors_social') await knex('actors_socials')
.insert(profile.social.map((url) => ({ .insert(profile.social.map((url) => ({
url, url,
platform: new URL(url).hostname.match(/([\w-]+)?\.(\w+)$/)?.[1], platform: new URL(url).hostname.match(/([\w-]+)?\.(\w+)$/)?.[1],