Software /
code /
prosody
Annotate
tools/migration/prosody_sql.lua @ 4197:bef732980436
tools/migration/*.lua: Convert to unix line endings
author | Matthew Wild <mwild1@gmail.com> |
---|---|
date | Wed, 23 Feb 2011 18:31:48 +0000 |
parent | 4193:3fecfe58d633 |
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; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
3 local DBI = require "DBI"; |
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"; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
10 local mtools = require "mtools"; |
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 |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
14 module "prosody_sql" |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
15 |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
16 local function create_table(connection, params) |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
17 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
|
18 if params.driver == "PostgreSQL" then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
19 create_sql = create_sql:gsub("`", "\""); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
20 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
21 |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
22 local stmt = connection:prepare(create_sql); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
23 if stmt then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
24 local ok = stmt:execute(); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
25 local commit_ok = connection:commit(); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
26 if ok and commit_ok then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
27 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
|
28 if params.driver == "PostgreSQL" then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
29 index_sql = index_sql:gsub("`", "\""); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
30 elseif params.driver == "MySQL" then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
31 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
|
32 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
33 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
|
34 local ok, commit_ok, commit_err; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
35 if stmt then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
36 ok, err = assert(stmt:execute()); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
37 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
|
38 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
39 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
40 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
41 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
42 |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
43 local function serialize(value) |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
44 local t = type(value); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
45 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
|
46 return t, tostring(value); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
47 elseif t == "table" then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
48 local value,err = json.encode(value); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
49 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
|
50 return nil, err; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
51 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
52 return nil, "Unhandled value type: "..t; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
53 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
54 local function deserialize(t, value) |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
55 if t == "string" then return value; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
56 elseif t == "boolean" then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
57 if value == "true" then return true; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
58 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
|
59 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
|
60 elseif t == "json" then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
61 return json.decode(value); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
62 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
63 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
64 |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
65 local function decode_user(item) |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
66 local userdata = { |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
67 user = item[1][1].user; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
68 host = item[1][1].host; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
69 stores = {}; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
70 }; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
71 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
|
72 local result = {}; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
73 local store = item[i]; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
74 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
|
75 local row = store[i]; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
76 local k = row.key; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
77 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
|
78 if k and v then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
79 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
|
80 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
|
81 result[a] = b; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
82 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
83 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
84 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
85 userdata.stores[store[1].store] = result; |
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 return userdata; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
89 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
90 |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
91 function reader(input) |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
92 local dbh = assert(DBI.Connect( |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
93 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
|
94 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
|
95 input.username, input.password, |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
96 input.host, input.port |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
97 )); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
98 assert(dbh:ping()); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
99 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
|
100 assert(stmt:execute()); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
101 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
|
102 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
|
103 -- get SQL rows, sorted |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
104 local iter = mtools.sorted { |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
105 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
|
106 filter = function(x) |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
107 for i=1,#keys do |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
108 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
|
109 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
110 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
|
111 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
|
112 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
|
113 return x; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
114 end; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
115 sorter = function(a, b) |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
116 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
|
117 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
|
118 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
|
119 end; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
120 }; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
121 -- merge rows to get stores |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
122 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
|
123 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
|
124 end); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
125 -- merge stores to get users |
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[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
|
128 end); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
129 return function() |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
130 local x = iter(); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
131 return x and decode_user(x); |
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 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
134 |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
135 function writer(output, iter) |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
136 local dbh = assert(DBI.Connect( |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
137 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
|
138 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
|
139 output.username, output.password, |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
140 output.host, output.port |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
141 )); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
142 assert(dbh:ping()); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
143 create_table(dbh, output); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
144 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
|
145 assert(stmt:execute()); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
146 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
|
147 assert(stmt:execute()); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
148 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
|
149 |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
150 return function(item) |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
151 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
|
152 local host = item.host or ""; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
153 local user = item.user or ""; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
154 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
|
155 -- TODO transactions |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
156 local extradata = {}; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
157 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
|
158 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
|
159 local t, value = assert(serialize(value)); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
160 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
|
161 else |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
162 extradata[key] = value; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
163 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
164 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
165 if next(extradata) ~= nil then |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
166 local t, extradata = assert(serialize(extradata)); |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
167 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
|
168 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
169 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
170 end; |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
171 end |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
172 |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
173 |
bef732980436
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4193
diff
changeset
|
174 return _M; |