Software /
code /
prosody
Annotate
tools/migration/migrator/prosody_sql.lua @ 4234:ce92aafc9c03
tools/migration/migrator/prosody_sql: Throw a friendlier error when LuaDBI is not found
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Tue, 05 Apr 2011 12:58:14 +0100 |
parent | 4216:ff80a8471e86 |
child | 4247:6a372135b4c4 |
rev | line source |
---|---|
4197
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
1 |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
2 local assert = assert; |
4234
ce92aafc9c03
tools/migration/migrator/prosody_sql: Throw a friendlier error when LuaDBI is not found
Matthew Wild <mwild1@gmail.com>
parents:
4216
diff
changeset
|
3 local have_DBI, DBI = pcall(require,"DBI"); |
4197
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
4 local print = print; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
5 local type = type; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
6 local next = next; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
7 local pairs = pairs; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
8 local t_sort = table.sort; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
9 local json = require "util.json"; |
4216
ff80a8471e86
tools/migration/*: Numerous changes and restructuring, and the addition of a Makefile
Matthew Wild <mwild1@gmail.com>
parents:
4197
diff
changeset
|
10 local mtools = require "migrator.mtools"; |
4197
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
11 local tostring = tostring; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
12 local tonumber = tonumber; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
13 |
4234
ce92aafc9c03
tools/migration/migrator/prosody_sql: Throw a friendlier error when LuaDBI is not found
Matthew Wild <mwild1@gmail.com>
parents:
4216
diff
changeset
|
14 if not have_DBI then |
ce92aafc9c03
tools/migration/migrator/prosody_sql: Throw a friendlier error when LuaDBI is not found
Matthew Wild <mwild1@gmail.com>
parents:
4216
diff
changeset
|
15 error("LuaDBI (required for SQL support) was not found, please see http://prosody.im/doc/depends#luadbi", 0); |
ce92aafc9c03
tools/migration/migrator/prosody_sql: Throw a friendlier error when LuaDBI is not found
Matthew Wild <mwild1@gmail.com>
parents:
4216
diff
changeset
|
16 end |
ce92aafc9c03
tools/migration/migrator/prosody_sql: Throw a friendlier error when LuaDBI is not found
Matthew Wild <mwild1@gmail.com>
parents:
4216
diff
changeset
|
17 |
4197
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
18 module "prosody_sql" |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
19 |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
20 local function create_table(connection, params) |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
21 local create_sql = "CREATE TABLE `prosody` (`host` TEXT, `user` TEXT, `store` TEXT, `key` TEXT, `type` TEXT, `value` TEXT);"; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
22 if params.driver == "PostgreSQL" then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
23 create_sql = create_sql:gsub("`", "\""); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
24 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
25 |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
26 local stmt = connection:prepare(create_sql); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
27 if stmt then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
28 local ok = stmt:execute(); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
29 local commit_ok = connection:commit(); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
30 if ok and commit_ok then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
31 local index_sql = "CREATE INDEX `prosody_index` ON `prosody` (`host`, `user`, `store`, `key`)"; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
32 if params.driver == "PostgreSQL" then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
33 index_sql = index_sql:gsub("`", "\""); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
34 elseif params.driver == "MySQL" then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
35 index_sql = index_sql:gsub("`([,)])", "`(20)%1"); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
36 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
37 local stmt, err = connection:prepare(index_sql); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
38 local ok, commit_ok, commit_err; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
39 if stmt then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
40 ok, err = assert(stmt:execute()); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
41 commit_ok, commit_err = assert(connection:commit()); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
42 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
43 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
44 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
45 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
46 |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
47 local function serialize(value) |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
48 local t = type(value); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
49 if t == "string" or t == "boolean" or t == "number" then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
50 return t, tostring(value); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
51 elseif t == "table" then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
52 local value,err = json.encode(value); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
53 if value then return "json", value; end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
54 return nil, err; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
55 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
56 return nil, "Unhandled value type: "..t; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
57 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
58 local function deserialize(t, value) |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
59 if t == "string" then return value; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
60 elseif t == "boolean" then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
61 if value == "true" then return true; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
62 elseif value == "false" then return false; end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
63 elseif t == "number" then return tonumber(value); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
64 elseif t == "json" then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
65 return json.decode(value); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
66 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
67 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
68 |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
69 local function decode_user(item) |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
70 local userdata = { |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
71 user = item[1][1].user; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
72 host = item[1][1].host; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
73 stores = {}; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
74 }; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
75 for i=1,#item do -- loop over stores |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
76 local result = {}; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
77 local store = item[i]; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
78 for i=1,#store do -- loop over store data |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
79 local row = store[i]; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
80 local k = row.key; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
81 local v = deserialize(row.type, row.value); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
82 if k and v then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
83 if k ~= "" then result[k] = v; elseif type(v) == "table" then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
84 for a,b in pairs(v) do |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
85 result[a] = b; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
86 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
87 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
88 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
89 userdata.stores[store[1].store] = result; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
90 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
91 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
92 return userdata; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
93 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
94 |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
95 function reader(input) |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
96 local dbh = assert(DBI.Connect( |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
97 assert(input.driver, "no input.driver specified"), |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
98 assert(input.database, "no input.database specified"), |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
99 input.username, input.password, |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
100 input.host, input.port |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
101 )); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
102 assert(dbh:ping()); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
103 local stmt = assert(dbh:prepare("SELECT * FROM prosody")); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
104 assert(stmt:execute()); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
105 local keys = {"host", "user", "store", "key", "type", "value"}; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
106 local f,s,val = stmt:rows(true); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
107 -- get SQL rows, sorted |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
108 local iter = mtools.sorted { |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
109 reader = function() val = f(s, val); return val; end; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
110 filter = function(x) |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
111 for i=1,#keys do |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
112 if not x[keys[i]] then return false; end -- TODO log error, missing field |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
113 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
114 if x.host == "" then x.host = nil; end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
115 if x.user == "" then x.user = nil; end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
116 if x.store == "" then x.store = nil; end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
117 return x; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
118 end; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
119 sorter = function(a, b) |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
120 local a_host, a_user, a_store = a.host or "", a.user or "", a.store or ""; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
121 local b_host, b_user, b_store = b.host or "", b.user or "", b.store or ""; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
122 return a_host > b_host or (a_host==b_host and a_user > b_user) or (a_host==b_host and a_user==b_user and a_store > b_store); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
123 end; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
124 }; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
125 -- merge rows to get stores |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
126 iter = mtools.merged(iter, function(a, b) |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
127 return (a.host == b.host and a.user == b.user and a.store == b.store); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
128 end); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
129 -- merge stores to get users |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
130 iter = mtools.merged(iter, function(a, b) |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
131 return (a[1].host == b[1].host and a[1].user == b[1].user); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
132 end); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
133 return function() |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
134 local x = iter(); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
135 return x and decode_user(x); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
136 end; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
137 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
138 |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
139 function writer(output, iter) |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
140 local dbh = assert(DBI.Connect( |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
141 assert(output.driver, "no output.driver specified"), |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
142 assert(output.database, "no output.database specified"), |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
143 output.username, output.password, |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
144 output.host, output.port |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
145 )); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
146 assert(dbh:ping()); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
147 create_table(dbh, output); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
148 local stmt = assert(dbh:prepare("SELECT * FROM prosody")); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
149 assert(stmt:execute()); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
150 local stmt = assert(dbh:prepare("DELETE FROM prosody")); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
151 assert(stmt:execute()); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
152 local insert = assert(dbh:prepare("INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)")); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
153 |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
154 return function(item) |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
155 if not item then assert(dbh:commit()) return dbh:close(); end -- end of input |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
156 local host = item.host or ""; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
157 local user = item.user or ""; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
158 for store, data in pairs(item.stores) do |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
159 -- TODO transactions |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
160 local extradata = {}; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
161 for key, value in pairs(data) do |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
162 if type(key) == "string" and key ~= "" then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
163 local t, value = assert(serialize(value)); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
164 local ok, err = assert(insert:execute(host, user, store, key, t, value)); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
165 else |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
166 extradata[key] = value; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
167 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
168 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
169 if next(extradata) ~= nil then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
170 local t, extradata = assert(serialize(extradata)); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
171 local ok, err = assert(insert:execute(host, user, store, "", t, extradata)); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
172 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
173 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
174 end; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
175 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
176 |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
177 |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
178 return _M; |