Software /
code /
prosody
Annotate
tools/migration/migrator/prosody_sql.lua @ 7990:3d17b2bf0e0c
MUC: Shorter label
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Tue, 21 Mar 2017 04:41:40 +0100 |
parent | 7885:236b5a6154b2 |
child | 8065:36d9c1226fbc |
rev | line source |
---|---|
4166
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
1 |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
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"); |
4166
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
4 local print = print; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
5 local type = type; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
6 local next = next; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
7 local pairs = pairs; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
8 local t_sort = table.sort; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
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:
4166
diff
changeset
|
10 local mtools = require "migrator.mtools"; |
4166
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
11 local tostring = tostring; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
12 local tonumber = tonumber; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
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 |
7359
a5a080c12c96
Update every link to the documentation to use HTTPS
Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
parents:
5776
diff
changeset
|
15 error("LuaDBI (required for SQL support) was not found, please see https://prosody.im/doc/depends#luadbi", 0); |
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
|
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 |
4166
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
18 |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
19 local function create_table(connection, params) |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
20 local create_sql = "CREATE TABLE `prosody` (`host` TEXT, `user` TEXT, `store` TEXT, `key` TEXT, `type` TEXT, `value` TEXT);"; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
21 if params.driver == "PostgreSQL" then |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
22 create_sql = create_sql:gsub("`", "\""); |
4294
d2406f0ce8a5
migrator/prosody_sql.lua: Create (and upgrade) MySQL tables to use MEDIUMTEXT for the 'value' column to avoid truncation
Matthew Wild <mwild1@gmail.com>
parents:
4247
diff
changeset
|
23 elseif params.driver == "MySQL" then |
d2406f0ce8a5
migrator/prosody_sql.lua: Create (and upgrade) MySQL tables to use MEDIUMTEXT for the 'value' column to avoid truncation
Matthew Wild <mwild1@gmail.com>
parents:
4247
diff
changeset
|
24 create_sql = create_sql:gsub("`value` TEXT", "`value` MEDIUMTEXT"); |
4166
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
25 end |
5776
bd0ff8ae98a8
Remove all trailing whitespace
Florian Zeitz <florob@babelmonkeys.de>
parents:
4310
diff
changeset
|
26 |
4166
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
27 local stmt = connection:prepare(create_sql); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
28 if stmt then |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
29 local ok = stmt:execute(); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
30 local commit_ok = connection:commit(); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
31 if ok and commit_ok then |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
32 local index_sql = "CREATE INDEX `prosody_index` ON `prosody` (`host`, `user`, `store`, `key`)"; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
33 if params.driver == "PostgreSQL" then |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
34 index_sql = index_sql:gsub("`", "\""); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
35 elseif params.driver == "MySQL" then |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
36 index_sql = index_sql:gsub("`([,)])", "`(20)%1"); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
37 end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
38 local stmt, err = connection:prepare(index_sql); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
39 local ok, commit_ok, commit_err; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
40 if stmt then |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
41 ok, err = assert(stmt:execute()); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
42 commit_ok, commit_err = assert(connection:commit()); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
43 end |
4310
52ccbf71d062
migrator/prosody_sql.lua: Fix for compatibility with non-MySQL databases
Matthew Wild <mwild1@gmail.com>
parents:
4294
diff
changeset
|
44 elseif params.driver == "MySQL" then -- COMPAT: Upgrade tables from 0.8.0 |
4294
d2406f0ce8a5
migrator/prosody_sql.lua: Create (and upgrade) MySQL tables to use MEDIUMTEXT for the 'value' column to avoid truncation
Matthew Wild <mwild1@gmail.com>
parents:
4247
diff
changeset
|
45 -- Failed to create, but check existing MySQL table here |
d2406f0ce8a5
migrator/prosody_sql.lua: Create (and upgrade) MySQL tables to use MEDIUMTEXT for the 'value' column to avoid truncation
Matthew Wild <mwild1@gmail.com>
parents:
4247
diff
changeset
|
46 local stmt = connection:prepare("SHOW COLUMNS FROM prosody WHERE Field='value' and Type='text'"); |
d2406f0ce8a5
migrator/prosody_sql.lua: Create (and upgrade) MySQL tables to use MEDIUMTEXT for the 'value' column to avoid truncation
Matthew Wild <mwild1@gmail.com>
parents:
4247
diff
changeset
|
47 local ok = stmt:execute(); |
d2406f0ce8a5
migrator/prosody_sql.lua: Create (and upgrade) MySQL tables to use MEDIUMTEXT for the 'value' column to avoid truncation
Matthew Wild <mwild1@gmail.com>
parents:
4247
diff
changeset
|
48 local commit_ok = connection:commit(); |
d2406f0ce8a5
migrator/prosody_sql.lua: Create (and upgrade) MySQL tables to use MEDIUMTEXT for the 'value' column to avoid truncation
Matthew Wild <mwild1@gmail.com>
parents:
4247
diff
changeset
|
49 if ok and commit_ok then |
d2406f0ce8a5
migrator/prosody_sql.lua: Create (and upgrade) MySQL tables to use MEDIUMTEXT for the 'value' column to avoid truncation
Matthew Wild <mwild1@gmail.com>
parents:
4247
diff
changeset
|
50 if stmt:rowcount() > 0 then |
d2406f0ce8a5
migrator/prosody_sql.lua: Create (and upgrade) MySQL tables to use MEDIUMTEXT for the 'value' column to avoid truncation
Matthew Wild <mwild1@gmail.com>
parents:
4247
diff
changeset
|
51 local stmt = connection:prepare("ALTER TABLE prosody MODIFY COLUMN `value` MEDIUMTEXT"); |
d2406f0ce8a5
migrator/prosody_sql.lua: Create (and upgrade) MySQL tables to use MEDIUMTEXT for the 'value' column to avoid truncation
Matthew Wild <mwild1@gmail.com>
parents:
4247
diff
changeset
|
52 local ok = stmt:execute(); |
d2406f0ce8a5
migrator/prosody_sql.lua: Create (and upgrade) MySQL tables to use MEDIUMTEXT for the 'value' column to avoid truncation
Matthew Wild <mwild1@gmail.com>
parents:
4247
diff
changeset
|
53 local commit_ok = connection:commit(); |
d2406f0ce8a5
migrator/prosody_sql.lua: Create (and upgrade) MySQL tables to use MEDIUMTEXT for the 'value' column to avoid truncation
Matthew Wild <mwild1@gmail.com>
parents:
4247
diff
changeset
|
54 if ok and commit_ok then |
d2406f0ce8a5
migrator/prosody_sql.lua: Create (and upgrade) MySQL tables to use MEDIUMTEXT for the 'value' column to avoid truncation
Matthew Wild <mwild1@gmail.com>
parents:
4247
diff
changeset
|
55 print("Database table automatically upgraded"); |
d2406f0ce8a5
migrator/prosody_sql.lua: Create (and upgrade) MySQL tables to use MEDIUMTEXT for the 'value' column to avoid truncation
Matthew Wild <mwild1@gmail.com>
parents:
4247
diff
changeset
|
56 end |
d2406f0ce8a5
migrator/prosody_sql.lua: Create (and upgrade) MySQL tables to use MEDIUMTEXT for the 'value' column to avoid truncation
Matthew Wild <mwild1@gmail.com>
parents:
4247
diff
changeset
|
57 end |
d2406f0ce8a5
migrator/prosody_sql.lua: Create (and upgrade) MySQL tables to use MEDIUMTEXT for the 'value' column to avoid truncation
Matthew Wild <mwild1@gmail.com>
parents:
4247
diff
changeset
|
58 repeat until not stmt:fetch(); |
d2406f0ce8a5
migrator/prosody_sql.lua: Create (and upgrade) MySQL tables to use MEDIUMTEXT for the 'value' column to avoid truncation
Matthew Wild <mwild1@gmail.com>
parents:
4247
diff
changeset
|
59 end |
4166
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
60 end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
61 end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
62 end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
63 |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
64 local function serialize(value) |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
65 local t = type(value); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
66 if t == "string" or t == "boolean" or t == "number" then |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
67 return t, tostring(value); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
68 elseif t == "table" then |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
69 local value,err = json.encode(value); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
70 if value then return "json", value; end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
71 return nil, err; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
72 end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
73 return nil, "Unhandled value type: "..t; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
74 end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
75 local function deserialize(t, value) |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
76 if t == "string" then return value; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
77 elseif t == "boolean" then |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
78 if value == "true" then return true; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
79 elseif value == "false" then return false; end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
80 elseif t == "number" then return tonumber(value); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
81 elseif t == "json" then |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
82 return json.decode(value); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
83 end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
84 end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
85 |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
86 local function decode_user(item) |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
87 local userdata = { |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
88 user = item[1][1].user; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
89 host = item[1][1].host; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
90 stores = {}; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
91 }; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
92 for i=1,#item do -- loop over stores |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
93 local result = {}; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
94 local store = item[i]; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
95 for i=1,#store do -- loop over store data |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
96 local row = store[i]; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
97 local k = row.key; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
98 local v = deserialize(row.type, row.value); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
99 if k and v then |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
100 if k ~= "" then result[k] = v; elseif type(v) == "table" then |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
101 for a,b in pairs(v) do |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
102 result[a] = b; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
103 end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
104 end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
105 end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
106 userdata.stores[store[1].store] = result; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
107 end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
108 end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
109 return userdata; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
110 end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
111 |
7881
4e3067272fae
tools/migration/migrator/*: Remove use of module()
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
112 local function reader(input) |
4166
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
113 local dbh = assert(DBI.Connect( |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
114 assert(input.driver, "no input.driver specified"), |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
115 assert(input.database, "no input.database specified"), |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
116 input.username, input.password, |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
117 input.host, input.port |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
118 )); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
119 assert(dbh:ping()); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
120 local stmt = assert(dbh:prepare("SELECT * FROM prosody")); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
121 assert(stmt:execute()); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
122 local keys = {"host", "user", "store", "key", "type", "value"}; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
123 local f,s,val = stmt:rows(true); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
124 -- get SQL rows, sorted |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
125 local iter = mtools.sorted { |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
126 reader = function() val = f(s, val); return val; end; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
127 filter = function(x) |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
128 for i=1,#keys do |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
129 if not x[keys[i]] then return false; end -- TODO log error, missing field |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
130 end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
131 if x.host == "" then x.host = nil; end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
132 if x.user == "" then x.user = nil; end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
133 if x.store == "" then x.store = nil; end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
134 return x; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
135 end; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
136 sorter = function(a, b) |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
137 local a_host, a_user, a_store = a.host or "", a.user or "", a.store or ""; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
138 local b_host, b_user, b_store = b.host or "", b.user or "", b.store or ""; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
139 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); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
140 end; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
141 }; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
142 -- merge rows to get stores |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
143 iter = mtools.merged(iter, function(a, b) |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
144 return (a.host == b.host and a.user == b.user and a.store == b.store); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
145 end); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
146 -- merge stores to get users |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
147 iter = mtools.merged(iter, function(a, b) |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
148 return (a[1].host == b[1].host and a[1].user == b[1].user); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
149 end); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
150 return function() |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
151 local x = iter(); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
152 return x and decode_user(x); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
153 end; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
154 end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
155 |
7881
4e3067272fae
tools/migration/migrator/*: Remove use of module()
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
156 local function writer(output, iter) |
4166
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
157 local dbh = assert(DBI.Connect( |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
158 assert(output.driver, "no output.driver specified"), |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
159 assert(output.database, "no output.database specified"), |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
160 output.username, output.password, |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
161 output.host, output.port |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
162 )); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
163 assert(dbh:ping()); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
164 create_table(dbh, output); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
165 local stmt = assert(dbh:prepare("SELECT * FROM prosody")); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
166 assert(stmt:execute()); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
167 local stmt = assert(dbh:prepare("DELETE FROM prosody")); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
168 assert(stmt:execute()); |
4247
6a372135b4c4
tools/migration/migrator/prosody_sql.lua: Fix compatibility with PostgreSQL (thanks Timo)
Matthew Wild <mwild1@gmail.com>
parents:
4234
diff
changeset
|
169 local insert_sql = "INSERT INTO `prosody` (`host`,`user`,`store`,`key`,`type`,`value`) VALUES (?,?,?,?,?,?)"; |
6a372135b4c4
tools/migration/migrator/prosody_sql.lua: Fix compatibility with PostgreSQL (thanks Timo)
Matthew Wild <mwild1@gmail.com>
parents:
4234
diff
changeset
|
170 if output.driver == "PostgreSQL" then |
6a372135b4c4
tools/migration/migrator/prosody_sql.lua: Fix compatibility with PostgreSQL (thanks Timo)
Matthew Wild <mwild1@gmail.com>
parents:
4234
diff
changeset
|
171 insert_sql = insert_sql:gsub("`", "\""); |
6a372135b4c4
tools/migration/migrator/prosody_sql.lua: Fix compatibility with PostgreSQL (thanks Timo)
Matthew Wild <mwild1@gmail.com>
parents:
4234
diff
changeset
|
172 end |
6a372135b4c4
tools/migration/migrator/prosody_sql.lua: Fix compatibility with PostgreSQL (thanks Timo)
Matthew Wild <mwild1@gmail.com>
parents:
4234
diff
changeset
|
173 local insert = assert(dbh:prepare(insert_sql)); |
4166
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
174 |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
175 return function(item) |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
176 if not item then assert(dbh:commit()) return dbh:close(); end -- end of input |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
177 local host = item.host or ""; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
178 local user = item.user or ""; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
179 for store, data in pairs(item.stores) do |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
180 -- TODO transactions |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
181 local extradata = {}; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
182 for key, value in pairs(data) do |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
183 if type(key) == "string" and key ~= "" then |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
184 local t, value = assert(serialize(value)); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
185 local ok, err = assert(insert:execute(host, user, store, key, t, value)); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
186 else |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
187 extradata[key] = value; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
188 end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
189 end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
190 if next(extradata) ~= nil then |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
191 local t, extradata = assert(serialize(extradata)); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
192 local ok, err = assert(insert:execute(host, user, store, "", t, extradata)); |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
193 end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
194 end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
195 end; |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
196 end |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
197 |
3ac90743039b
tools/migration/*.lua: Convert to unix line endings
Matthew Wild <mwild1@gmail.com>
parents:
4162
diff
changeset
|
198 |
7881
4e3067272fae
tools/migration/migrator/*: Remove use of module()
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
199 return { |
4e3067272fae
tools/migration/migrator/*: Remove use of module()
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
200 reader = reader; |
4e3067272fae
tools/migration/migrator/*: Remove use of module()
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
201 writer = writer; |
4e3067272fae
tools/migration/migrator/*: Remove use of module()
Kim Alvefur <zash@zash.se>
parents:
5776
diff
changeset
|
202 } |