Menu

[r1811]: / blog / trunk / sql / migrate.cpp  Maximize  Restore  History

Download this file

126 lines (119 with data), 3.8 kB

  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
123
124
125
#include <cppdb/frontend.h>
#include <apps/dbversion.h>
#include <iostream>
void copy(cppdb::session &from,cppdb::session &to)
{
cppdb::result r;
std::cout << "- checking versions" << std::endl;
r = from << "SELECT value from text_options where id='dbversion'" << cppdb::row;
if(r.empty() || r.get<std::string>(0)!=CPPBLOG_DBVERSION) {
throw std::runtime_error("Invalid source DB version");
}
r = to << "SELECT value from text_options where id='dbversion'" << cppdb::row;
if(r.empty() || r.get<std::string>(0)!=CPPBLOG_DBVERSION) {
throw std::runtime_error("Invalid target DB version");
}
to << "DELETE from text_options where id='dbversion'" << cppdb::exec;
std::cout << "- users" << std::endl;
r = from << "SELECT id,username,password FROM users";
while(r.next()) {
to << "INSERT into users(id,username,password) values(?,?,?)"
<<r.get<int>(0)
<< r.get<std::string>(1)
<< r.get<std::string>(2)
<< cppdb::exec;
}
std::cout << "- posts" << std::endl;
r = from << "SELECT id,author_id,title,abstract,content,publish,is_open,comment_count from posts";
while(r.next()) {
to << "INSERT into posts(id,author_id,title,abstract,content,publish,is_open,comment_count) values(?,?,?,?,?,?,?,?)"
<<r.get<int>(0)
<<r.get<int>(1)
<< r.get<std::string>(2)
<< r.get<std::string>(3)
<< r.get<std::string>(4)
<< r.get<std::tm>(5)
<< r.get<int>(6)
<< r.get<int>(7)
<< cppdb::exec;
}
std::cout << "- comments" << std::endl;
r = from << "SELECT id,post_id,author,email,url,publish_time,content from comments";
while(r.next()) {
to << "insert into comments values(?,?,?,?,?,?,?)"
<<r.get<int>(0)
<<r.get<int>(1)
<< r.get<std::string>(2)
<< r.get<std::string>(3)
<< r.get<std::string>(4)
<< r.get<std::tm>(5)
<< r.get<std::string>(6)
<< cppdb::exec;
}
std::cout << "- text_options" << std::endl;
r = from << "SELECT id,value from text_options";
while(r.next()) {
to << "insert into text_options values(?,?)"
<< r.get<std::string>(0)
<< r.get<std::string>(1)
<< cppdb::exec;
}
std::cout << "- cats" << std::endl;
r = from << "SELECT id,name from cats";
while(r.next()) {
to << "insert into cats values(?,?)"
<< r.get<int>(0)
<< r.get<std::string>(1)
<< cppdb::exec;
}
std::cout << "- post2cat" << std::endl;
r = from << "SELECT post_id,cat_id,publish,is_open from post2cat";
while(r.next()) {
to << "insert into post2cat values(?,?,?,?)"
<< r.get<int>(0)
<< r.get<int>(1)
<< r.get<std::tm>(2)
<< r.get<int>(3)
<< cppdb::exec;
}
std::cout << "- pages" << std::endl;
r = from << "SELECT id,author_id,title,content,is_open from pages";
while(r.next()) {
to << "insert into pages values(?,?,?,?,?)"
<< r.get<int>(0)
<< r.get<int>(1)
<< r.get<std::string>(2)
<< r.get<std::string>(3)
<< r.get<int>(4)
<< cppdb::exec;
}
}
int main(int argc,char **argv)
{
if(argc!=3) {
std::cerr << "Usage src-connection-string tgt-connection-string" << std::endl;
return 1;
}
try {
cppdb::session from(argv[1]),to(argv[2]);
cppdb::transaction trf(from);
cppdb::transaction trt(to);
copy(from,to);
if(to.engine()=="postgresql") {
to<<"select setval('users_id_seq',(select max(id) from users))"<<cppdb::row;
to<<"select setval('posts_id_seq',(select max(id) from posts))"<<cppdb::row;
to<<"select setval('comments_id_seq',(select max(id) from comments))"<<cppdb::row;
to<<"select setval('pages_id_seq',(select max(id) from pages))"<<cppdb::row;
to<<"select setval('links_id_seq',(select max(id) from links))"<<cppdb::row;
to<<"select setval('cats_id_seq',(select max(id) from cats))"<<cppdb::row;
}
trt.commit();
trf.commit();
}
catch(std::exception const &e) {
std::cerr << e.what() << std::endl;
return 1;
}
std::cout << "Done" <<std::endl;
return 0;
}
MongoDB Logo MongoDB