Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

[videodb] fix seasons update query for mysql #4985

Merged
merged 2 commits into from

9 participants

@jmarshallnz
Owner

Mysql doesn't like SELECT subqueries on the same table.

@popcornmix mind checking that this does the trick?

Jonathan Marshall [videodb] fix seasons update query for mysql, which doesn't like SELE…
…CT subqueries on the same table
0ed87f2
@jmarshallnz jmarshallnz referenced this pull request from a commit
Jonathan Marshall [videodb] remove duplicate tvshow rows in favour of multiple links in…
… tvshowlinkpath
a151604
@MilhouseVH

Hi, just testing this PR and while it's fixed the reported error with seasons, there's another problem.

06:30:25  20.112259 T:3058262560   ERROR: Unable to open database: MyVideos85 [1049](Unknown database 'MyVideos85')
06:30:25  20.121288 T:3058262560   ERROR: Unable to open database: MyVideos84 [1049](Unknown database 'MyVideos84')
06:30:25  20.132496 T:3058262560   ERROR: Unable to open database: MyVideos83 [1049](Unknown database 'MyVideos83')
06:30:25  20.140810 T:3058262560   ERROR: Unable to open database: MyVideos82 [1049](Unknown database 'MyVideos82')
06:30:25  20.149313 T:3058262560   ERROR: Unable to open database: MyVideos81 [1049](Unknown database 'MyVideos81')
06:30:25  20.159119 T:3058262560   ERROR: Unable to open database: MyVideos80 [1049](Unknown database 'MyVideos80')
06:30:25  20.181171 T:3058262560  NOTICE: Old database found - updating from version 79 to 85
06:30:36  31.121687 T:3058262560  NOTICE: Attempting to update the database MyVideos85 from version 79 to 85
06:30:50  45.674557 T:3058262560   ERROR: SQL: Undefined MySQL error: Code (1349)
                                            Query: CREATE VIEW tvshowview AS SELECT   tvshow.*,  path.idParentPath AS idParentPath,  path.strPath AS strPath,  path.dateAdded AS dateAdded,  lastPlayed, totalCount, watchedcount, totalSeasons FROM tvshow  LEFT JOIN tvshowlinkpath ON    tvshowlinkpath.idShow=tvshow.idShow  LEFT JOIN path ON    path.idPath=tvshowlinkpath.idPath  INNER JOIN (    SELECT tvshow.idShow AS idShow,      MAX(files.lastPlayed) AS lastPlayed,      NULLIF(COUNT(episode.c12), 0) AS totalCount,      COUNT(files.playCount) AS watchedcount,      NULLIF(COUNT(DISTINCT(episode.c12)), 0) AS totalSeasons     FROM tvshow      LEFT JOIN episode ON        episode.idShow=tvshow.idShow      LEFT JOIN files ON        files.idFile=episode.idFile     GROUP BY tvshow.idShow) AS counts ON  tvshow.idShow = counts.idShow
06:30:50  45.689072 T:3058262560   ERROR: Exception updating database MyVideos85 from version 79 to 85
06:30:50  45.689293 T:3058262560   ERROR: Error updating database MyVideos85 from version 79 to 85
06:30:50  45.711559 T:3058262560  NOTICE: Old database found - updating from version 78 to 85
06:30:50  45.716167 T:3058262560   ERROR: SQL: Can't create database for copy: 'MyVideos78' (1007)
06:30:50  45.716526 T:3058262560   ERROR: Unable to copy old database MyVideos78 to new version MyVideos85

I dropped the previously created MyVideos85 so that the test with the new build + this PR created MyVideos85 afresh.

@MilhouseVH
Error Code: 1349. View's SELECT contains a subquery in the FROM clause
@jmarshallnz
Owner

Thanks. I'll get to it at some point in the next few days.

@sccplg

You probably need to split the second select embedded in creation of the main view in an 'other secondary view and put them in the join.

@popcornmix
Collaborator
@jmarshallnz
Owner

Updated with @sccplg's fix.

@jmarshallnz
Owner

@koying @Montellese are these suitable do you think?

@mkortstiege
Collaborator

I think you want to bump version as well. Otherwise the view is not updated.

@MilhouseVH

Looks like a clean upgrade with the latest update, many thanks.

Seasons and TV appear to be working correctly.

07:42:18  20.459352 T:3058213408  NOTICE: Running database version MyMusic48
07:42:18  20.466063 T:3058213408   ERROR: Unable to open database: MyVideos85 [1049](Unknown database 'MyVideos85')
07:42:18  20.474089 T:3058213408   ERROR: Unable to open database: MyVideos84 [1049](Unknown database 'MyVideos84')
07:42:18  20.484037 T:3058213408   ERROR: Unable to open database: MyVideos83 [1049](Unknown database 'MyVideos83')
07:42:18  20.514097 T:3058213408   ERROR: Unable to open database: MyVideos82 [1049](Unknown database 'MyVideos82')
07:42:18  20.520548 T:3058213408   ERROR: Unable to open database: MyVideos81 [1049](Unknown database 'MyVideos81')
07:42:18  20.526615 T:3058213408   ERROR: Unable to open database: MyVideos80 [1049](Unknown database 'MyVideos80')
07:42:18  20.573580 T:3058213408  NOTICE: Old database found - updating from version 79 to 85
07:42:44  46.504429 T:3058213408  NOTICE: Attempting to update the database MyVideos85 from version 79 to 85
07:43:30  93.018929 T:3058213408  NOTICE: Running database version TV24
@popcornmix
Collaborator

@mkortstiege I'm not sure a database version bump would help, as the previous MyVideos85 written is broken. I doubt it can be updated to MyVideos86.

I can confirm this PR updates database without errors.

@mkortstiege
Collaborator

Sure, but what about sqlite and the in between versions?

EDIT: Nvm, got it sorted internally :)

@popcornmix
Collaborator

Possible failure to update with build that includes this PR:
http://forum.xbmc.org/showthread.php?tid=192380&pid=1748083#pid1748083

@Montellese
Owner

Wow is MySQL's SQL support really that limited. What I find odd is that the error states that it's not allowed to have a "SELECT" subquery in the FROM part of another query but in our case the subquery is part of the JOIN.

Well I guess there's no other way around this even though I find it ugly.

@jmarshallnz
Owner

@Montellese - the error is 1349 (no subquery in FROM clause of views) so it's valid. The normal workaround appears to be as proposed here, or to rewrite so the subquery is part of the SELECT.

jenkins build this please

@popcornmix
Collaborator
@jmarshallnz
Owner

v46 was Gotham, so it seems unlikely that the music update is broken in any way.

Said user has a broken database.

@peol

Verified to work on latest openelec XBMC master with this patch.

I just tried it both on an existing v79 database and from scratch, it works without problems.

@nemphys

Apart from this issue (which is fixed by the proposed PR, had already written exactly the same workaround before noticing this), I am also facing another issue when upgrading v78->v85 in MySQL. The table tvshowlinkpath ends up with multiple duplicate entries (same idShow and idPath) and the upgrade fails when trying to create a UNIQUE index on this table after the updates.
Should I post a new issue or does this stay here since it's MySQL related?

@jmarshallnz
Owner

@nemphys please post a bug on trac and cc me. In particular, check your database before and after the various update stages so we can identify the problem.

@jmarshallnz jmarshallnz merged commit 11145fd into from
@jmarshallnz jmarshallnz deleted the branch
@MartijnKaijser MartijnKaijser added this to the Helix 14.0-alpha1 milestone
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jul 5, 2014
  1. [videodb] fix seasons update query for mysql, which doesn't like SELE…

    Jonathan Marshall authored
    …CT subqueries on the same table
Commits on Jul 6, 2014
This page is out of date. Refresh to see the latest.
Showing with 19 additions and 15 deletions.
  1. +19 −15 xbmc/video/VideoDatabase.cpp
View
34 xbmc/video/VideoDatabase.cpp
@@ -387,6 +387,21 @@ void CVideoDatabase::CreateViews()
/* NOTE: The tvshowview needs to have "GROUP BY tvshow.idShow" added to any usage if you wish to
avoid duplicates due to multiple paths per tvshow (from the join on tvshowlinkpath) */
+ CLog::Log(LOGINFO, "create tvshowcounts");
+ CStdString tvshowcounts = PrepareSQL("CREATE VIEW tvshowcounts AS SELECT "
+ " tvshow.idShow AS idShow,"
+ " MAX(files.lastPlayed) AS lastPlayed,"
+ " NULLIF(COUNT(episode.c12), 0) AS totalCount,"
+ " COUNT(files.playCount) AS watchedcount,"
+ " NULLIF(COUNT(DISTINCT(episode.c12)), 0) AS totalSeasons "
+ " FROM tvshow"
+ " LEFT JOIN episode ON"
+ " episode.idShow=tvshow.idShow"
+ " LEFT JOIN files ON"
+ " files.idFile=episode.idFile "
+ " GROUP BY tvshow.idShow");
+ m_pDS->exec(tvshowcounts.c_str());
+
CLog::Log(LOGINFO, "create tvshowview");
CStdString tvshowview = PrepareSQL("CREATE VIEW tvshowview AS SELECT "
" tvshow.*,"
@@ -399,19 +414,8 @@ void CVideoDatabase::CreateViews()
" tvshowlinkpath.idShow=tvshow.idShow"
" LEFT JOIN path ON"
" path.idPath=tvshowlinkpath.idPath"
- " INNER JOIN ("
- " SELECT tvshow.idShow AS idShow,"
- " MAX(files.lastPlayed) AS lastPlayed,"
- " NULLIF(COUNT(episode.c12), 0) AS totalCount,"
- " COUNT(files.playCount) AS watchedcount,"
- " NULLIF(COUNT(DISTINCT(episode.c12)), 0) AS totalSeasons "
- " FROM tvshow"
- " LEFT JOIN episode ON"
- " episode.idShow=tvshow.idShow"
- " LEFT JOIN files ON"
- " files.idFile=episode.idFile "
- " GROUP BY tvshow.idShow) AS counts ON"
- " tvshow.idShow = counts.idShow");
+ " INNER JOIN tvshowcounts ON"
+ " tvshow.idShow = tvshowcounts.idShow");
m_pDS->exec(tvshowview.c_str());
CLog::Log(LOGINFO, "create seasonview");
@@ -4598,7 +4602,7 @@ void CVideoDatabase::UpdateTables(int iVersion)
}
}
// cleanup duplicate seasons
- m_pDS->exec("DELETE FROM seasons WHERE idSeason NOT IN (SELECT min(idSeason) FROM seasons GROUP BY idShow,season)");
+ m_pDS->exec("DELETE FROM seasons WHERE idSeason NOT IN (SELECT idSeason FROM (SELECT min(idSeason) FROM seasons GROUP BY idShow,season) AS sub)");
}
}
if (iVersion < 84)
@@ -4637,7 +4641,7 @@ void CVideoDatabase::UpdateTables(int iVersion)
int CVideoDatabase::GetSchemaVersion() const
{
- return 85;
+ return 86;
}
bool CVideoDatabase::LookupByFolders(const CStdString &path, bool shows)
Something went wrong with that request. Please try again.