MythTV  master
archivedbcheck.cpp
Go to the documentation of this file.
1 #include <iostream>
2 
3 // Qt
4 #include <QString>
5 #include <QSqlError>
6 
7 // MythTV
8 #include <libmyth/mythcontext.h>
9 #include <libmythbase/mythdb.h>
11 
12 // mytharchive
13 #include "archivedbcheck.h"
14 
15 const QString currentDatabaseVersion = "1006";
16 const QString MythArchiveVersionName = "ArchiveDBSchemaVer";
17 
19 {
20  QString dbver = gCoreContext->GetSetting("ArchiveDBSchemaVer");
21 
22  if (dbver == currentDatabaseVersion)
23  return true;
24 
25  if (dbver == "")
26  {
27  LOG(VB_GENERAL, LOG_INFO,
28  "Inserting MythArchive initial database information.");
29 
30  DBUpdates updates
31  {
32  "DROP TABLE IF EXISTS archiveitems;",
33 
34  "CREATE TABLE IF NOT EXISTS archiveitems ("
35  " intid INT UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,"
36  " type set ('Recording','Video','File'),"
37  " title VARCHAR(128),"
38  " subtitle VARCHAR(128),"
39  " description TEXT,"
40  " startdate VARCHAR(30),"
41  " starttime VARCHAR(30),"
42  " size INT UNSIGNED NOT NULL,"
43  " filename TEXT NOT NULL,"
44  " hascutlist BOOL NOT NULL DEFAULT 0,"
45  " cutlist TEXT,"
46  " INDEX (title)"
47  ");"
48  };
49  if (!performActualUpdate("MythArchive", MythArchiveVersionName,
50  updates, "1000", dbver))
51  return false;
52  }
53 
54  if (dbver == "1000")
55  {
56  DBUpdates updates
57  {
58  "ALTER TABLE archiveitems MODIFY size BIGINT UNSIGNED NOT NULL;"
59  };
60 
61  if (!performActualUpdate("MythArchive", MythArchiveVersionName,
62  updates, "1001", dbver))
63  return false;
64  }
65 
66 
67  if (dbver == "1001")
68  {
69  DBUpdates updates
70  {
71  qPrintable(QString("ALTER DATABASE %1 DEFAULT CHARACTER SET latin1;")
72  .arg(GetMythDB()->GetDatabaseName())),
73  "ALTER TABLE archiveitems"
74  " MODIFY title varbinary(128) default NULL,"
75  " MODIFY subtitle varbinary(128) default NULL,"
76  " MODIFY description blob,"
77  " MODIFY startdate varbinary(30) default NULL,"
78  " MODIFY starttime varbinary(30) default NULL,"
79  " MODIFY filename blob,"
80  " MODIFY cutlist blob;"
81  };
82 
83  if (!performActualUpdate("MythArchive", MythArchiveVersionName,
84  updates, "1002", dbver))
85  return false;
86  }
87 
88 
89  if (dbver == "1002")
90  {
91  DBUpdates updates
92  {
93  qPrintable(QString("ALTER DATABASE %1 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;")
94  .arg(GetMythDB()->GetDatabaseName())),
95  "ALTER TABLE archiveitems"
96  " DEFAULT CHARACTER SET utf8,"
97  " MODIFY title varchar(128) CHARACTER SET utf8 NULL,"
98  " MODIFY subtitle varchar(128) CHARACTER SET utf8 NULL,"
99  " MODIFY description text CHARACTER SET utf8,"
100  " MODIFY startdate varchar(30) CHARACTER SET utf8 NULL,"
101  " MODIFY starttime varchar(30) CHARACTER SET utf8 NULL,"
102  " MODIFY filename text CHARACTER SET utf8 NOT NULL,"
103  " MODIFY cutlist text CHARACTER SET utf8;"
104  };
105 
106  if (!performActualUpdate("MythArchive", MythArchiveVersionName,
107  updates, "1003", dbver))
108  return false;
109  }
110 
111  if (dbver == "1003")
112  {
113  DBUpdates updates
114  {
115  "ALTER TABLE `archiveitems` "
116  "ADD duration INT UNSIGNED NOT NULL DEFAULT 0, "
117  "ADD cutduration INT UNSIGNED NOT NULL DEFAULT 0, "
118  "ADD videowidth INT UNSIGNED NOT NULL DEFAULT 0, "
119  "ADD videoheight INT UNSIGNED NOT NULL DEFAULT 0, "
120  "ADD filecodec VARCHAR(50) NOT NULL DEFAULT '', "
121  "ADD videocodec VARCHAR(50) NOT NULL DEFAULT '', "
122  "ADD encoderprofile VARCHAR(50) NOT NULL DEFAULT 'NONE';"
123  };
124 
125  if (!performActualUpdate("MythArchive", MythArchiveVersionName,
126  updates, "1004", dbver))
127  return false;
128  }
129 
130  if (dbver == "1004")
131  {
132  DBUpdates updates
133  {
134  "DELETE FROM keybindings "
135  " WHERE action = 'DELETEITEM' AND context = 'Archive';"
136  };
137 
138  if (!performActualUpdate("MythArchive", MythArchiveVersionName,
139  updates, "1005", dbver))
140  return false;
141  }
142 
143  // Repeat 1003 DBs pre MySQL v8 systems that may have not be set to utf8
144 
145  if (dbver == "1005")
146  {
147  DBUpdates updates
148  {
149  "ALTER TABLE archiveitems"
150  " DEFAULT CHARACTER SET utf8,"
151  " MODIFY title varchar(128) CHARACTER SET utf8 NULL,"
152  " MODIFY subtitle varchar(128) CHARACTER SET utf8 NULL,"
153  " MODIFY startdate varchar(30) CHARACTER SET utf8 NULL,"
154  " MODIFY starttime varchar(30) CHARACTER SET utf8 NULL;"
155  };
156 
157  if (!performActualUpdate("MythArchive", MythArchiveVersionName,
158  updates, "1006", dbver))
159  return false;
160  }
161 
162  return true;
163 }
164 
mythdb.h
MythArchiveVersionName
const QString MythArchiveVersionName
Definition: archivedbcheck.cpp:16
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
GetMythDB
MythDB * GetMythDB(void)
Definition: mythdb.cpp:50
mythdbcheck.h
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:55
performActualUpdate
bool performActualUpdate(const QString &component, const QString &versionkey, const DBUpdates &updates, const QString &version, QString &dbver)
Definition: dbcheckcommon.cpp:113
archivedbcheck.h
currentDatabaseVersion
const QString currentDatabaseVersion
Definition: archivedbcheck.cpp:15
mythcontext.h
UpgradeArchiveDatabaseSchema
bool UpgradeArchiveDatabaseSchema(void)
Definition: archivedbcheck.cpp:18
DBUpdates
std::vector< std::string > DBUpdates
Definition: mythdbcheck.h:9
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:898