ledger-core
migrations.hpp
1 /*
2  *
3  * migrations
4  * ledger-core
5  *
6  * Created by Pierre Pollastri on 25/04/2017.
7  *
8  * The MIT License (MIT)
9  *
10  * Copyright (c) 2016 Ledger
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy
13  * of this software and associated documentation files (the "Software"), to deal
14  * in the Software without restriction, including without limitation the rights
15  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16  * copies of the Software, and to permit persons to whom the Software is
17  * furnished to do so, subject to the following conditions:
18  *
19  * The above copyright notice and this permission notice shall be included in all
20  * copies or substantial portions of the Software.
21  *
22  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28  * SOFTWARE.
29  *
30  */
31 
32 #ifndef LEDGER_CORE_MIGRATIONS_HPP
33 #define LEDGER_CORE_MIGRATIONS_HPP
34 
35 #include <soci.h>
36 #include <iostream>
37 #include "utils/Exception.hpp"
38 
39 namespace ledger {
40  namespace core {
42  int getDatabaseMigrationVersion(soci::session& sql);
43 
44  template <int migrationNumber>
45  void migrate(soci::session& sql) {
46  std::cerr << "No specified migration for version " << migrationNumber << std::endl;
47  throw make_exception(api::ErrorCode::RUNTIME_ERROR, "No specified migration for version {}", migrationNumber);
48  }
49 
50  template <int version>
51  bool migrate(soci::session& sql, int currentVersion) {
52  bool previousResult = migrate<version - 1>(sql, currentVersion);
53 
54  if (currentVersion < version) {
55  migrate<version>(sql);
56  sql << "UPDATE __database_meta__ SET version = :version", soci::use(version);
57  return true;
58  }
59 
60  return previousResult;
61  };
62 
63  template <int version>
64  void rollback(soci::session& sql) {
65  //std::cerr << "No specified rollback for version " << version << std::endl;
66  //throw make_exception(api::ErrorCode::RUNTIME_ERROR, "No specified rollback for version {}", version);
67  }
68 
74  template <int version>
75  void rollback(soci::session& sql, int currentVersion) {
76  if (currentVersion == version) {
77  // we’re in sync with the database; perform the rollback normally
78  rollback<version>(sql);
79 
80  if (version >= 0) {
81  // after rolling back this migration, we won’t have anything left, so we only
82  // update the version for > 0
83  if (version != 0) {
84  auto prevVersion = version - 1;
85  sql << "UPDATE __database_meta__ SET version = :version", soci::use(prevVersion);
86  }
87 
88  rollback<version - 1>(sql, currentVersion - 1);
89  }
90  } else if (currentVersion < version) {
91  // we’re trying to rollback a migration that hasn’t been applied; try the previous
92  // rollback
93  rollback<version - 1>(sql, currentVersion);
94  } else {
95  // we’re trying to rollback a migration but we have missed some others; apply the
96  // next ones first
97  throw make_exception(api::ErrorCode::RUNTIME_ERROR, "Missing rollback migrations: {} to {}", version + 1, currentVersion);
98  }
99  }
100 
101  template <> bool migrate<-1>(soci::session& sql, int currentVersion);
102  template <> void rollback<-1>(soci::session& sql, int currentVersion);
103 
104  // Migrations
105  template <> void migrate<0>(soci::session& sql);
106  template <> void rollback<0>(soci::session& sql);
107 
108  template <> void migrate<1>(soci::session& sql);
109  template <> void rollback<1>(soci::session& sql);
110 
111  template <> void migrate<2>(soci::session& sql);
112  template <> void rollback<2>(soci::session& sql);
113 
114  template <> void migrate<3>(soci::session& sql);
115  template <> void rollback<3>(soci::session& sql);
116 
117  template <> void migrate<4>(soci::session& sql);
118  template <> void rollback<4>(soci::session& sql);
119 
120  template <> void migrate<5>(soci::session& sql);
121  template <> void rollback<5>(soci::session& sql);
122 
123  template <> void migrate<6>(soci::session& sql);
124  template <> void rollback<6>(soci::session& sql);
125 
126  template <> void migrate<7>(soci::session& sql);
127  template <> void rollback<7>(soci::session& sql);
128 
129  // add ripple’s memo field
130  template <> void migrate<8>(soci::session& sql);
131  template <> void rollback<8>(soci::session& sql);
132 
133  // Migrate input_data from VARCHAR(255) to TEXT
134  template <> void migrate<9>(soci::session& sql);
135  template <> void rollback<9>(soci::session& sql);
136 
137  // Add block_height column to erc20_operations table
138  template <> void migrate<10>(soci::session& sql);
139  template <> void rollback<10>(soci::session& sql);
140 
141  // Tezos Support
142  template <> void migrate<11>(soci::session& sql);
143  template <> void rollback<11>(soci::session& sql);
144 
145  // Add internal transactions for ETH
146  template <> void migrate<12>(soci::session& sql);
147  template <> void rollback<12>(soci::session& sql);
148 
149  // Add block height to outputs
150  template <> void migrate<13>(soci::session& sql);
151  template <> void rollback<13>(soci::session& sql);
152 
153  // Add XRP sequence
154  template <> void migrate<14>(soci::session& sql);
155  template <> void rollback<14>(soci::session& sql);
156 
157  // Add XRP destination_tag
158  template <> void migrate<15>(soci::session& sql);
159  template <> void rollback<15>(soci::session& sql);
160 
161  // Replace XTZ address column by public_key one
162  template <> void migrate<16>(soci::session& sql);
163  template <> void rollback<16>(soci::session& sql);
164 
165  // Add status column on XTZ transactions
166  template <> void migrate<17>(soci::session& sql);
167  template <> void rollback<17>(soci::session& sql);
168  }
169 }
170 
171 #endif //LEDGER_CORE_MIGRATIONS_HPP
Definition: Account.cpp:8
int getDatabaseMigrationVersion(soci::session &sql)
Get the current database migration version.
Definition: migrations.cpp:36