Merging two library by editing DB

I know that this isn’t a very active topic but I’ll try my luck and hope that there’s someone here who can help.

I’m trying to merge two libraries together and changing where the files are located by editing Plex’s database. I managed to do it but when I initiate a scan Plex deletes movies that were stored in one of the two locations from the library.

I ran this script first to merge the two libraries together

UPDATE section_locations
SET library_section_id = 16
WHERE
library_section_id = 17;
UPDATE directories
SET library_section_id = 16
WHERE
library_section_id = 17;
UPDATE media_items
SET library_section_id = 16
WHERE
library_section_id = 17;
UPDATE media_items
SET section_location_id = 16
WHERE
section_location_id = 17;
UPDATE metadata_item_views
SET library_section_id = 16
WHERE
library_section_id = 17;
UPDATE metadata_items
SET library_section_id = 16
WHERE
library_section_id = 17

Then I ran this script to change the location of the media items in both libraries

UPDATE section_locations
SET root_path= replace(root_path, 'G:\Movies1', 'F:\Movies')
where root_path like '%G:\Movies1%';
UPDATE metadata_items
SET guid= replace(guid, 'file://G:\Movies1', 'file://F:\Movies')
where guid like '%file://G:\Movies1%';
UPDATE media_streams
SET url= replace(url, 'file://G:\Movies1', 'file://F:\Movies')
where url like '%file://G:\Movies1%';
UPDATE media_parts
SET file= replace(file, 'G:\Movies1', 'F:\Movies')
where file like '%G:\Movies1%';
UPDATE section_locations
SET root_path= replace(root_path, 'G:\Movies2', 'F:\Movies')
where root_path like '%G:\Movies2%';
UPDATE metadata_items
SET guid= replace(guid, 'file://G:\Movies2', 'file://F:\Movies')
where guid like '%file://G:\Movies2%';
UPDATE media_streams
SET url= replace(url, 'file://G:\Movies2', 'file://F:\Movies')
where url like '%file://G:\Movies2%';
UPDATE media_parts
SET file= replace(file, 'G:\Movies2', 'F:\Movies')
where file like '%G:\Movies2%'

This merges the two libraries together and changes their location fine. The files all play just fine after this edit, my only problem is that when I initiate a scan Plex deletes the movies that were stored in the first location from my library.

Anyone got any idea what I might be doing wrong?

Thanks for the help.

In the “directories” table, there is also a field for “parent_directory_id” which is the parent to the movie folder in question. This also needs to be updated. Otherwise, that parent folder can’t be found so PMS will think it’s missing when it does a scan.

Okay, I’m kind of a total newbie to this, I only did it a couple of times before and it worked but I don’t really remember how I did it or if there is something that changed with Plex’s database since I did it the last time.

How exactly I’m supposed to edit the parent_directory_id and replace it with the new location? there are thousands of entries in this column and they’re all just different numbers for each entry.

Also, shouldn’t updating the “library_section_id” in the “directories” table be enough to tell plex where the new folders/files are stored?

UPDATE directories
SET library_section_id = 16
WHERE
library_section_id = 17

Like so?

Sorry, ignore my last comment. I was thinking of something else.

I think the issue you are having is with

UPDATE media_items
SET section_location_id = 16
WHERE
section_location_id = 17;

Are you sure this is the correct IDs involved? Unless you have never deleted/edited the library folder settings, it is unlikely the section id and the section location id’s match up.

Yes, the IDs are correct. before editing anything I had two libraries one with ID 16 and the other ID 17.

I want to merge 17 into 16 and have them point to the same location.

If I remove the part of the script you quoted that’s related to section_location_id. Plex ends up duplicating the entries that it deleted if I had edited that part.

Oops. I quoted the wrong one. I meant

section_location_id is not the same as the library_section_id. Just wanted to make sure that the value for this should also change from 16 to 17.

Can you clarify this? So you have 2 locations in your library? If so, this is specifically related to the section_location_id I mentioned above.

I did change the section_location_id. section_location_id and library_section_id are the same so I changed both of them so that they would match. both of them are now set as 16 instead of 17

Regarding your second question. Yes, I had two libraries pointed towards two locations. But now I want merged the two directories together so I want to merge the two Plex libraries as well without messing with my metadata and custom matches.

EDIT: Does these changes to how Plex’s database work have anything to do with why I can’t merge two libraries together anymore? https://forums.plex.tv/t/can-no-longer-update-library-database-with-sqlite3/701405/3

P.S. I ran the scripts that drops these triggers before running the scripts I posted earlier and then recreated them after making the edits I wanted to the database.

I actually meant if either of the libraries involved pointed to more than 1 folder?

Nope, shouldn’t matter. Since it does initially work, it’s not the editing of the DB that’s a problem. The library scan is finding something that doesn’t match and that is what’s causing the entries to get removed, so it’s something with the edit itself. You must be missing something, but I can’t tell what. The easiest way for me to check is fi you share your DB with me. If you can zip it, upload to GDrive or similar, them PM me the link, I can take a look.

No, each of the libraries involved just point to one folder.

I sent you a message with my database and everything I tried to get it working. Thanks for the help @anon18523487

In case anyone else is following along. We figured this out. Has to do with this edit.

The directories table should only have 1 entry per library_section_id. Op already had a section 16 so this resulted in a duplicate. The fix is not to change this entry but to change all the subsequent entries to reuse the existing section id and to change the parent id for those entries to also point to the same existing section id.

3 Likes

I’m a bit lost. Can someone fill me in on what all needs to be updated exactly to move a movie from 1 directory/library to another pre-existing directory/library?

So far I was able to find library_section_id in metadata_items, changing this seems to be the easy part, but the actual movie folder location does not get changed here. I tried finding the others mentioned but, well failed.

Here’s what I want to do, which this sounds like the right track:
I have 2 libraries: Movies and MoviesTwo (Examples)
Each have their own separate directories as well (obviously).

I want to move all the movies in MoviesTwo into the directory and Library for Movies and keep all the metadata/watched status/not show in recently added…

Thanks in advance

This is what I used personally,

This will change the locations and file locations

UPDATE section_locations
SET root_path= replace(root_path, ‘MoviesTwo’, ‘Movies’)
where root_path like ‘%MoviesTwo%’;
UPDATE metadata_items
SET guid= replace(guid, ‘file://MoviesTwo’, ‘file://Movies’)
where guid like ‘%file://MoviesTwo%’;
UPDATE media_streams
SET url= replace(url, ‘file://MoviesTwo’, ‘file://Movies’)
where url like ‘%file://MoviesTwo%’;
UPDATE media_parts
SET file= replace(file, ‘MoviesTwo’, ‘Movies’)
where file like ‘%MoviesTwo%’

You then need to figure out the parent_directory_id of moviestwo and movies in the directories table and the library_section_id and section_location_id then replace them with this code, merging the two libraries together

UPDATE directories
set parent_directory_id = 2988
where parent_directory_id = 2989;
UPDATE section_locations
SET library_section_id = 16
WHERE
library_section_id = 17;
UPDATE directories
SET library_section_id = 16
WHERE
library_section_id = 17;
UPDATE media_items
SET library_section_id = 16
WHERE
library_section_id = 17;
UPDATE media_items
SET section_location_id = 16
WHERE
section_location_id = 17;
UPDATE metadata_item_views
SET library_section_id = 16
WHERE
library_section_id = 17;
UPDATE metadata_items
SET library_section_id = 16
WHERE
library_section_id = 17

2 Likes

So do I:

  1. shut down plex,
  2. move the files
  3. backup the database
  4. change the locations and file locations
  5. merge
  6. start plex
    ?

Yes, backup being the most important step here.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.