LeechCraft  0.6.70-13729-g7046a9d2a7
Modular cross-platform feature rich live environment.
migrate.h
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #pragma once
31 
32 #include "util/db/oral/oral.h"
33 
34 namespace LC
35 {
36 namespace Util
37 {
38 namespace oral
39 {
40  namespace detail
41  {
42  bool MatchesSchema (const QString& baseName, const QString& schema, QSqlDatabase& db)
43  {
44  auto result = Util::RunTextQuery (db,
45  QString { "SELECT sql FROM sqlite_master WHERE type = 'table' AND name = '%1'" }
46  .arg (baseName));
47  if (!result.next ())
48  return true;
49 
50  const auto& existingDDL = result.value (0).toString ();
51 
52  auto figureOutFields = [] (const QString& str)
53  {
54  auto firstOpen = str.indexOf ('(');
55  auto lastClose = str.lastIndexOf (')');
56  return str.midRef (firstOpen, lastClose - firstOpen);
57  };
58  auto existing = figureOutFields (existingDDL);
59  auto suggested = figureOutFields (schema);
60  return existing == suggested;
61  }
62  }
63 
64  template<typename Record, typename ImplFactory = SQLiteImplFactory>
65  void Migrate (QSqlDatabase& db)
66  {
67  const auto& baseName = Record::ClassName ();
68 
69  const auto& thisName = "copy" + baseName;
70  const auto& schema = detail::AdaptCreateTable<ImplFactory, Record> (detail::BuildCachedFieldsData<Record> (thisName));
71 
72  if (detail::MatchesSchema (baseName, schema, db))
73  {
74  qDebug () << Q_FUNC_INFO
75  << "not migrating"
76  << db.connectionName ();
77  return;
78  }
79 
80  qDebug () << Q_FUNC_INFO
81  << "migrating"
82  << db.connectionName ();
83 
84  Util::DBLock lock { db };
85  lock.Init ();
86 
87  Util::RunTextQuery (db, schema);
88 
89  const auto& fields = detail::GetFieldsNames<Record> {} ().join (", ");
90 
92  QString { "INSERT INTO %2 (%1) SELECT %1 FROM %3;" }
93  .arg (fields)
94  .arg (thisName)
95  .arg (baseName));
96 
98  QString { "DROP TABLE %1;" }
99  .arg (baseName));
100  Util::RunTextQuery (db,
101  QString { "ALTER TABLE %1 RENAME TO %2;" }
102  .arg (thisName)
103  .arg (baseName));
104 
105  lock.Good ();
106  }
107 }
108 }
109 }
UTIL_DB_API void Init()
Initializes the transaction.
Definition: dblock.cpp:61
Provides database transaction lock.
Definition: dblock.h:62
void Migrate(QSqlDatabase &db)
Definition: migrate.h:65
constexpr detail::MemberPtrs< Ptrs... > fields
Definition: oral.h:958
QSqlQuery RunTextQuery(const QSqlDatabase &db, const QString &text)
Runs the given query text on the given db.
Definition: util.cpp:40
bool MatchesSchema(const QString &baseName, const QString &schema, QSqlDatabase &db)
Definition: migrate.h:42
Definition: constants.h:35