Commit Graph

144 Commits

Author SHA1 Message Date
3a94f5935d Split upgrade script for MBS-13514
* CreateConstraints.sql is not run on mirrors.

 * The original inline constraints *were* created on mirrors, so the `DROP`
   statements remain in a `*-mirror.sql` script.
2024-05-06 23:28:04 -05:00
162d130821 MBS-13421: Add support for genre collections () 2024-05-06 20:45:17 -05:00
b3720fa72b MBS-13403: Drop unused priority column from link_type ()
This was added in 2007 and AFAICT never used for anything.
All our relationship types had the default 0 value except for one,
which had 1 (I recently changed that to 0 as well)...
but that changed nothing since we do not use the value in any way.

I cannot drop this from the Dict in AddLinkType / EditLinkType
since it fails to load old edits without it, so I left it in
but added a comment explaining we no longer enter it.
2024-05-03 12:20:31 -05:00
411aa5a6a5 MBS-13514: Support 6-digit label codes ()
For some reason label code was being restricted with a constraint
added during table generation, meaning even mirrors have it.
This is really annoying now that the org generating label codes
has started generating 6-digit ones, which we block at the
db level, turning an otherwise easy fix into a schema change.

Additionally, we don't name the constraint so postgres does automatically;
it seems the name postgres chooses is label_label_code_check.

This moves the constraint to CreateConstraints, where it won't
be added to all mirrors (if we need to change it again in the future
it will be a lot easier), and it of course amends it to actually allow
6-digit label codes.
2024-05-03 09:35:44 -05:00
a43e267b52 MBS-13348: Fix edit_data_idx_link_type ()
The query in the edit search that is supposed to make use of this index was
changed in b21e5c579d0201325f5fbe385a51b663ae6caa69, but the actual index was
never updated to match (so it could not be used).

Note: This was already fixed in production. This patch provides an upgrade
script to run during the next schema change.
2023-11-06 11:25:33 -06:00
17265b36a6 Fix master_and_standalone key in upgrade.json 2023-05-12 11:17:42 -05:00
4ddcb78e44 MBS-11312: Add table to store changes to edit notes
While originally I was just planning to make changes
to the edit note table and nothing else, discussion with the team
suggested that it would be better to actually have a table
that stores the changes. This gives us several benefits:
1) we can easily check if an edit note has been deleted or edited
2) we can see who and when made the change
3) we can store the reason the change was made
4) we can see the original value (useful in case a malicious actor
gets access to an admin account somehow, and also useful
to find proof of misconduct if the editor posts an edit note,
then removes it (after it has been emailed to the intended insultee).

This table won't be replicated nor dumped
(other than in private dumps).
2023-05-12 10:03:01 -05:00
5bfbdb6d0c Add master-only upgrade script for dbmirror2.recordchange
See git log for admin/sql/dbmirror2/MasterSetup.sql.
2023-05-11 18:03:01 -05:00
6a0a72784b MBS-12800: Exclude cancelled releases from first_release_date
A release marked as cancelled by definition does not have a release date
and as such should not be used when calculating the first release date
of recordings or release groups connected to it.

This amends the get_release_first_release_date_rows function
to ignore releases with status 6 (cancelled).

It also updates the a_upd_release function to recalculate first dates
when a status is changed to or from 6 (cancelled).
2023-05-09 13:05:22 +03:00
c480ab8566 MBS-12573: Drop unused degraded column from cdtoc ()
This was apparently used ages ago to store
"a previous algorithm (less accurate) or things imported from freedb
that didn't have the accuracy of our ids", said Rob,
but "we may have dropped all those back in the day
and never nuked the column".
It contains always FALSE, at the moment,
and we seem to use it exactly nowhere.
2023-05-09 13:03:10 +03:00
cc605f1116 MBS-12704: Remove historical "Watch artist" SQL
This is pre-NGS code we aren't planning to get working again.
The feature is probably eventually coming to ListenBrainz instead.

This commit removes all the SQL schema for this feature.
2023-04-26 22:03:02 +03:00
74bdb4035e MBS-12370: Rename *.standalone.sql upgrade scripts to *.master_and_standalone.sql
This probably isn't as impactful as the s/mirror/all/ change, since master is a
subset of standalone, but I think it's still slightly clearer than only
specifying standalone.
2023-04-10 12:27:13 -05:00
054903313e MBS-12370: Rename *.extensions.sql upgrade scripts to *.all_extensions.sql
This makes it clearer that extensions are added on all nodes (master, mirror,
and standalone).
2023-04-10 12:27:13 -05:00
584303e7b7 MBS-12370: Rename *.mirror.sql upgrade scripts to *.all.sql
This makes it clearer that these scripts run on all nodes (master, mirror, and
standalone), not only mirrors.
2023-04-10 12:27:13 -05:00
4339745cf3 Reformat upgrade.json
Ran `jq . upgrade.json > upgrade2.json && mv upgrade2.json upgrade.json`.

The previous formatting was inconsistent and depended too much on the keys'
lengths to align items, which are changing with MBS-12370.
2023-04-10 12:23:37 -05:00
40ace35acc MBS-12497: Drop AC redirects when last use is removed
We automatically remove empty ACs after the last usage
is removed, using the dec_ref_count function.
The artist_credit_gid_redirect table has FKs to artist_credit though,
and it does not affect this process in any way.
That means that when an AC with only 1 usage has a redirect,
any edit that would remove it is blocked because
the redirect is not being removed and the associated FK still exists.

I talked with mwiencek and we decided to implement an intermediate
table where rows can be entered when the ref_count gets to 0,
rather than immediately deleting them. We then wait 7 days to see
if the artist credit got reused (ref_count went above 0 again)
in the meantime. This avoids losing redirects from an AC
that would get used again soon after, for example because it is being
removed from one recording and then added to another.

After 7 days the artist credit gets removed, including any redirects.
If it did get reused in the meantime, we just drop it from the
temporary table, obviously without removing the AC.

The temporary table setup should also work with the alternative
tracklist code in the future, which is why the table is generic
and stores a table name rather than being specifically AC-related.
2022-08-06 10:17:53 +03:00
08761b6ab9 MBS-12131 SQL fixes
array_cat_agg should only be created on master/mirror nodes, not
standalone.
2022-05-13 10:33:30 -05:00
fee50cb92d MBS-12250 SQL fixes
The dbmirror2 schema and associated objects should be created on
master/mirror nodes, not standalone.  However, the schema on its own
should still exist on standalone nodes (as it's created unconditionally
by InitDb.pl.)
2022-05-13 10:33:22 -05:00
0702cf0eb5 MBS-12369: Create documentation FKs on standalone DBs 2022-05-13 10:33:03 -05:00
cdf2ff0647 MBS-12249 SQL fixes 2022-05-13 10:33:03 -05:00
4aeacfb129 MBS-12225 SQL fixes 2022-05-13 10:33:02 -05:00
1ec55064fb Fix execution of 20220426-mbs-12131.sql
This script should generally run on all nodes, not only standalone.
array_cat_agg is an exception, and will be fixed in a following commit.
2022-05-13 09:39:15 -05:00
e6a26e630e MBS-12250: Create dbmirror2 schema on production and mirror servers 2022-05-11 13:43:08 +01:00
ec56f77fe3 MBS-12200: Drop schema objects related to Amazon cover art support 2022-05-11 12:23:10 +01:00
7574e9a0e3 Merge pull request from yvanzo/mbs-11456-sql
MBS-11456 (SQL): Add MBIDs to artist credits
2022-05-10 19:46:29 +01:00
6490f33e1b MBS-12190: Add Mood support (SQL)
Pretty much 100% a copy of genre
2022-05-10 18:57:57 +01:00
2c3c24770c MBS-12224: Keep tag counts updated via triggers
Three new trigger functions are added:

 * update_tag_counts_for_raw_insert
 * update_tag_counts_for_raw_update
 * update_tag_counts_for_raw_delete

These are executed for insertions/updates/deletions on the raw tag
tables, and update both the aggregate vote counts (area_tag.count, etc.)
and tag.ref_count.

Since these functions are written to work for any entity type, the
tagged entity type must be passed in as an argument to the function
where the trigger is invoked; a new ENUM, taggable_entity_type, has been
added to check that the argument is trusted.

The upgrade script also resolves MBS-5359 by rebuilding all the counts.

Existing Perl code that managed these counts has been removed from
Data::EntityTag.

For tests, see t/pgtap/tag_counts.sql.
2022-05-10 15:39:14 +01:00
62fe065fe2 MBS-12249, pt. 1: area_containment SQL 2022-05-09 20:29:43 +01:00
3a2609ace4 Block updates on relationship link tables
The link table and associated link_attribute tables mainly exist for
attribute caching: a lot of relationships happen to share the same link
type and attributes, so we can cache such data by a common ID and
minimize the amount of data that needs to be loaded across many
relationships.

Because they are used for caching and are shared across many (otherwise
unrelated) relationships, they are intended to be immutable: they should
only be inserted or deleted.

Updates to these tables should thus be blocked at the schema level.
This also helps ensure the data integrity of future materialized tables
that will rely on their immutability, like area_containment.
2022-05-09 20:28:29 +01:00
38861c768d MBS-12131: PostgreSQL 14 compatibility ()
This patch contains the schema changes required for PostgreSQL 14 to
work.  Note that these changes are also compatible with PostgreSQL 12.

For context, refer to the first incompatibility listed under
https://www.postgresql.org/docs/release/14.0/.

Because anycompatiblearray doesn't exist in v12, the ideal solution (and
the one used here) is to use more specific types.

 * The _median function is only used by the median aggregate, which is
   only used to calculate median recording lengths -- an integer value.

 * The array_accum aggregate was added in 33bd14e for tracklist_index, a
   table which was removed years ago.  The aggregate is unused and can
   be removed too.

 * array_cat_agg is only used internally by dbmirror.  (See
   https://github.com/metabrainz/dbmirror/blob/ca536c7/pending.c#L405.)
   Since pg_constraint.conkey is of type int2[], we can use that.  (See
   https://www.postgresql.org/docs/current/catalog-pg-constraint.html .)
2022-05-07 18:17:36 +01:00
0c570b31c9 MBS-11456 (V): Add update scripts to schema 27 2022-05-07 15:23:11 +01:00
3d9a2b1d00 Fix scripts ordering by date
Fix scripts ordering by date in upgrade.json and run the following:

    admin/CompileSchemaScripts.pl 27
2022-05-07 14:34:36 +01:00
bee0d390ab Merge pull request from mwiencek/mbs-12256
MBS-12256: Keep meta rating columns up-to-date with triggers
2022-05-06 19:25:06 +03:00
bbebbeba31 Merge pull request from reosarevok/MBS-12208
MBS-12208: Withdrawn releases should count as official for overview
2022-05-06 18:59:30 +03:00
62d7a29752 MBS-12256: Keep meta rating columns up-to-date with triggers 2022-05-06 15:19:51 +01:00
5910ed317b MBS-11457: Drop the series ordering_attribute column ()
This was added back when we thought we would have different
attributes to store item numbers for part of series relationships.
We don't, so this is always one and the same, 788 (number).

This changes the views to hardcode to 788 and removes the
ordering_attribute column elsewhere. It keeps the
SERIES_ORDERING_ATTRIBUTE constant on JS since that seems to be
used in fields and just points to the 'number' attribute,
but removes it on Perl because it was only used to store
the ID under ordering_attribute and never use it again, AFAICT.
2022-05-06 16:00:44 +03:00
b3dd3b819e MBS-12208: Withdrawn releases should count as official for overview
Withdrawn releases were by definition official until withdrawn,
so if a release group has only withdrawn releases, we should
probably still show it (this seems to be the majority community opinion,
anyway).
I understand two comparisons might be a lot more efficient
than any(1, 5), so doing that.

We need to run a one-off script to update RGs where a release was
*already* set to withdrawn, since otherwise these won't be set
to official(ish) until they are touched for some other reason.
Added the script to the upgrade file.
2022-04-29 18:56:33 +03:00
831d66462f MBS-12255: Add genre_alias_type table and make genre_alias consistent
For some reason (probably because it felt like overkill) we originally
implemented the genre_alias table without support
for alias types. That seems problematic though,
because there's at least the usual case of "typo" and
"name in a specific language", so "search hint" vs "genre name".
This adds types, and more generally changes
the genre_alias table to be consistent with the alias tables
for other entities.
To ensure the column order is also consistent,
we drop the old table and re-enter the old data
into a newly created one.
The genre_alias table should be empty in prod and any slaves,
but to be safe (in case some standalone users are using it)
this first copies any data to a temporary table and then
re-inserts it once done.
2022-04-14 12:10:39 +03:00
1ac13fd1cd MBS-12254: Add genre_annotation table
This has already been requested and is in any case good to have
for database parity between entities.
2022-04-14 12:10:20 +03:00
6c3a1b8ff4 MBS-12253: Add relationship tables for genres
This will allow adding relationships to genres.
At least genre-url and genre-genre rels have already
been requested.
2022-04-14 12:09:54 +03:00
bf83ddddd1 MBS-12252: Add edit_genre table
This will allows us to start entering edits for adding / editing genres
and for eventually adding aliases to them, so we have history.
2022-04-14 12:06:26 +03:00
2c200f2ec7 MBS-12225: slave -> mirror in functions and triggers 2022-04-06 11:24:37 +03:00
753d20853d MBS-12225: slave -> mirror in upgrade.json / upgrade scripts
Not changing every mention of "slave" on the old files,
but the file renaming is probably needed for compatibility.
2022-04-06 11:22:52 +03:00
03febd5adb MBS-12241: Drop the whitespace_collapsed database constraint
While we don't usually want titles and names to have blocks
of more than one space, that can be useful in cases
where the multiple spaces are in fact artist intent.
Additionally, this constraint is currently broken
by several entries in the DB.
For now, this is still enforced by Perl. Further changes will be needed
there if we want to allow it in some cases (e.g. track titles),
and we probably want to keep blocking it in others (e.g. tag names)
2022-03-09 22:27:32 +02:00
25ebcaa90b MBS-12141: Add constraints on tag.name 2021-12-18 06:33:04 -06:00
618b09233f MBS-11903: Restore subscriptions when collection is made public ()
We have been marking collection subscriptions as not available,
rather than removing them, whenever a collection is made private.
This takes what seems like the obvious next step and restores them
when the collection is made public again.
2021-11-04 07:51:27 +02:00
a0465f1462 MBS-10327: Enable subscribing to private collection you collaborate on
If you are able to participate in a private collection, you should also
be able to subscribe to it. Of course, if you're then removed as a
collaborator (thus losing the right to see the collection)
then you should also be removed as a subscriber.

This requires a schema change for the del_collection_sub_on_private
function, since otherwise the collection subscription will be
deactivated for collaborators if the collection was public
and is then made private.
2021-10-07 20:24:47 +03:00
22bdf47c5d MBS-11896: Remove the unique_primary_for_locale triggers and functions ()
Quoting from mwiencek's ticket directly since it explains
the whole reasoning:

"These triggers ensure that if an alias is set
to primary_for_locale = true, we unset primary_for_locale
on all other aliases for that locale & entity to avoid
uniqueness violations on the *_alias_idx_primary index.

There's a major problem here: cascading triggers
do not mix well with dbmirror, and can break replication.
This is because when row-level AFTER triggers are cascaded,
the innermost recordchange trigger is run first
(inverse of the actual statement order).
This is exactly what caused MBS-9366, and is a very hard problem
to fix in dbmirror properly; it's much safer to outright
ban cascading updates on replicated tables.

Even ignoring the replication issue, we don't need these triggers.
Avoiding duplicate primary locales can be handled
by the application (and already is). If it wasn't,
it even seems better to have the application fail
with a unique index violation (since that indicates it's not
accounting the changes properly) than silently change the data."
2021-09-24 10:46:02 +03:00
949d9d0714 MBS-11760: Add missing delete_unused_tag triggers
For some reason, we did not have delete_unused_tag triggers
for every taggable entity type. This was causing tags deleted
from the missing entity types to remain in the DB with 0 uses.
2021-07-02 16:17:33 +03:00
963007bfda a_upd_release_event: check for changes before doing work ()
Avoid re-calculating `*first_release_date` data if the release event
didn't actually change.

This can happen if the trigger fired on the `release_country` table and
only the country changed. (Rather than checking for that, it's still
correct to test the data we're concerned about: `UPDATE` triggers fire
even when no data changed.)

Additionally, make sure we're only calling
`set_release_first_release_date` once if the release column is
unchanged.  (This would occur on merges, and probably no other time.)
2021-06-08 15:57:51 -05:00