22 #include <unordered_map>
30 #include "MapDRelease.h"
32 namespace migrations {
36 const int database_id,
39 std::vector<int> tables_migrated = {};
40 std::unordered_map<int, std::vector<std::string>> tables_to_migrate;
41 sqlite.
query(
"BEGIN TRANSACTION");
44 "select name from sqlite_master WHERE type='table' AND "
45 "name='mapd_version_history'");
48 "CREATE TABLE mapd_version_history(version integer, migration_history text "
51 "CREATE TABLE mapd_date_in_days_column_migration_tmp(table_id integer primary "
55 "select * from mapd_version_history where migration_history = "
56 "'date_in_days_column'");
59 sqlite.
query(
"END TRANSACTION");
62 LOG(
INFO) <<
"Checking for date columns requiring metadata migration.";
64 "select name from sqlite_master where type='table' AND "
65 "name='mapd_date_in_days_column_migration_tmp'");
67 sqlite.
query(
"select table_id from mapd_date_in_days_column_migration_tmp");
70 tables_migrated.push_back(sqlite.
getData<
int>(
i, 0));
75 "CREATE TABLE mapd_date_in_days_column_migration_tmp(table_id integer "
80 "SELECT tables.tableid, tables.name, columns.name FROM mapd_tables tables, "
81 "mapd_columns columns where tables.tableid = columns.tableid AND "
82 "columns.coltype = ?1 AND columns.compression = ?2",
83 std::vector<std::string>{
88 tables_to_migrate[sqlite.
getData<
int>(
i, 0)] = {
92 }
catch (
const std::exception& e) {
93 LOG(
ERROR) <<
"Failed to complete migration on date in days column metadata: "
95 sqlite.
query(
"ROLLBACK");
98 sqlite.
query(
"END TRANSACTION");
100 for (
auto& id_names : tables_to_migrate) {
101 if (std::find(tables_migrated.begin(), tables_migrated.end(), id_names.first) ==
102 tables_migrated.end()) {
103 sqlite.
query(
"BEGIN TRANSACTION");
105 LOG(
INFO) <<
"Table: " << id_names.second[0]
106 <<
" may suffer from issues with DATE column: " << id_names.second[1]
107 <<
". Running an OPTIMIZE command to solve any issues with metadata.";
112 auto table_desc_itr = table_descriptors_by_id.find(id_names.first);
113 if (table_desc_itr == table_descriptors_by_id.end()) {
114 throw std::runtime_error(
"Table descriptor does not exist for table " +
115 id_names.second[0] +
" does not exist.");
117 auto td = table_desc_itr->second;
122 "INSERT INTO mapd_date_in_days_column_migration_tmp VALUES(?)",
124 }
catch (
const std::exception& e) {
125 LOG(
ERROR) <<
"Failed to complete metadata migration on date in days column: "
127 sqlite.
query(
"ROLLBACK");
130 sqlite.
query(
"COMMIT");
134 sqlite.
query(
"BEGIN TRANSACTION");
136 sqlite.
query(
"DROP TABLE mapd_date_in_days_column_migration_tmp");
138 "INSERT INTO mapd_version_history(version, migration_history) values(?,?)",
140 }
catch (
const std::exception& e) {
141 LOG(
ERROR) <<
"Failed to complete migraion on date in days column: " << e.what();
142 sqlite.
query(
"ROLLBACK");
145 sqlite.
query(
"END TRANSACTION");
146 LOG(
INFO) <<
"Successfully migrated all date in days column metadata.";
T getData(const int row, const int col)
class for a per-database catalog. also includes metadata for the current database and the current use...
virtual void query_with_text_params(std::string const &query_only)
virtual void query(const std::string &queryString)
Driver for running cleanup processes on a table. TableOptimizer provides functions for various cleanu...
Constants for Builtin SQL Types supported by OmniSci.
static std::shared_ptr< Executor > getExecutor(const ExecutorId id, const std::string &debug_dir="", const std::string &debug_file="", const SystemParameters system_parameters=SystemParameters())
std::map< int, TableDescriptor * > TableDescriptorMapById
static void migrateDateInDaysMetadata(const Catalog_Namespace::TableDescriptorMapById &table_descriptors_by_id, const int database_id, const Catalog_Namespace::Catalog *cat, SqliteConnector &sqlite)
static const int32_t MAPD_VERSION
virtual size_t getNumRows() const
void recomputeMetadata() const
Recomputes per-chunk metadata for each fragment in the table. Updates and deletes can cause chunk met...
static const ExecutorId UNITARY_EXECUTOR_ID