1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
module;
#include <cassert>
export module routemon:database.migrator$impl;
import :database.migrator;
namespace routemon::database {
auto load_migrations(
std::filesystem::path const& dir, std::int64_t current_version,
std::int64_t target_version) -> std::vector<std::string>
{
if (current_version < 0 || target_version < 0)
throw std::invalid_argument{"database versions must be nonnegative"};
if (target_version < current_version)
throw std::runtime_error{"migrating down is not supported yet"};
if (current_version == target_version)
return {};
assert(current_version < target_version);
// TODO: narrow_cast or something like that
auto migration_sqls =
std::vector<std::optional<std::pair<std::filesystem::path, std::string>>>{
static_cast<std::size_t>(target_version - current_version)
};
for (auto const& entry : std::filesystem::directory_iterator{dir})
{
if (!std::filesystem::is_regular_file(entry.status()))
continue;
auto const filename = std::string{entry.path().filename()};
if (!filename.ends_with("_up.sql"))
continue;
auto const [no_str, mrest] = util::split_on(filename, '_');
if (!mrest)
continue;
auto const mno = util::parse_int<std::int64_t>(no_str);
if (!mno || *mno < 1)
continue;
if (*mno <= current_version || target_version < *mno)
continue;
auto const i = static_cast<std::size_t>(*mno - current_version - 1);
if (migration_sqls[i])
throw std::runtime_error{std::format(
"found more than one (up) migration file for version {}", *mno)};
auto f = std::ifstream{entry.path(), std::ios::binary};
auto contents = std::string{
std::istreambuf_iterator<char>{f},
std::istreambuf_iterator<char>{},
};
migration_sqls[i] = {entry.path(), contents};
}
return migration_sqls | std::views::enumerate
| std::views::transform(
[current_version](auto&& pair) -> std::string
{
auto&& [i, mentry] = pair;
if (!mentry)
throw std::runtime_error{std::format(
"no migration found to version {}",
current_version + i + 1)};
return std::move(mentry->second);
})
| std::ranges::to<std::vector<std::string>>();
}
auto migrator::current_version() -> std::int64_t
{
if (auto const [has_migrations_table] =
dbc_.query(
"SELECT COUNT(*) FROM sqlite_schema WHERE "
"type IS 'table' AND "
"name IS 'migration'")
.scan_row_max1<std::int64_t>();
has_migrations_table)
return std::get<0>(dbc_.query("SELECT version FROM migration")
.scan_row_max1<std::int64_t>());
return 0;
}
migrator::migrator(
log::logger const& l, std::filesystem::path const& db_filename,
std::filesystem::path migrations_dir)
: l_{l.sub("migrator")}, dbc_{sqlite3::open(db_filename)},
migrations_dir_{std::move(migrations_dir)}
{
}
auto migrator::migrate_to(std::int64_t target_version) -> void
{
// Part of function contract. Consider throwing instead?
if (target_version < 0)
throw std::invalid_argument{"target database version must be nonnegative"};
auto current = current_version();
auto const migration_sqls =
load_migrations(migrations_dir_, current, target_version);
for (auto const& sql : migration_sqls)
{
l_.info("Migrating from schema version {} to {}", current, current + 1);
migrate_step(++current, sql);
}
}
auto migrator::migrate_step(std::int64_t target_version, util::zstring_view sql)
-> void
{
auto tx = sqlite3::tx{dbc_};
while (!sql.empty())
tx.exec_first(sql);
tx.exec(
"UPDATE migration SET version = :version",
[target_version](auto& s) { s.bind(":version", target_version); });
}
} // namespace routemon::database
|