Исходный код вики Database Sanity Check
Редактировал(а) Dmitriy Rybakov 05.08.2025, 13:07
Последние авторы
| author | version | line-number | content |
|---|---|---|---|
| 1 | This script is a sanity check of the database. There should be no results to the queries run if the database is in good state. | ||
| 2 | The data listed are data that should be deleted or fixed. | ||
| 3 | |||
| 4 | {{groovy}} | ||
| 5 | |||
| 6 | def runQuery(connection, sql) { | ||
| 7 | def result = "" | ||
| 8 | def stmt = connection.createStatement(); | ||
| 9 | try { | ||
| 10 | def resultset = stmt.executeQuery(sql); | ||
| 11 | def mdata = resultset.getMetaData(); | ||
| 12 | def nbcols = mdata.getColumnCount(); | ||
| 13 | result += "|=" | ||
| 14 | for (def i=1;i<=nbcols;i++) { | ||
| 15 | result += mdata.getColumnName(i); | ||
| 16 | if (i!=nbcols) | ||
| 17 | result += "|="; | ||
| 18 | } | ||
| 19 | result += "\n"; | ||
| 20 | |||
| 21 | while (true) { | ||
| 22 | resultset.next(); | ||
| 23 | if (resultset.getRow()==0) | ||
| 24 | return ""; | ||
| 25 | |||
| 26 | result += "|" | ||
| 27 | for (i=1;i<=nbcols;i++) { | ||
| 28 | def value = resultset.getString(i); | ||
| 29 | if ((value==null)||(value=="")) | ||
| 30 | value = " " | ||
| 31 | result += value | ||
| 32 | if (i!=nbcols) | ||
| 33 | result += "|"; | ||
| 34 | } | ||
| 35 | result+= "\n"; | ||
| 36 | if (resultset.isLast()) | ||
| 37 | break; | ||
| 38 | } | ||
| 39 | stmt.close(); | ||
| 40 | } catch (Exception e2) { | ||
| 41 | return "Error running sql: {{{${sql}}}} - " + e2.getMessage(); | ||
| 42 | } finally { | ||
| 43 | stmt.close(); | ||
| 44 | } | ||
| 45 | return result; | ||
| 46 | } | ||
| 47 | |||
| 48 | def nbqueries = 0; | ||
| 49 | def message = "" | ||
| 50 | def context2 = xcontext.getContext(); | ||
| 51 | def store = xwiki.getXWiki().getStore().getStore(); | ||
| 52 | store.beginTransaction(context2); | ||
| 53 | def session = store.getSession(context2); | ||
| 54 | def connection = session.connection(); | ||
| 55 | |||
| 56 | def script = xwiki.getDocument("Admin.SanityCheckSQL").getContent().split("\n") | ||
| 57 | for (sql in script) { | ||
| 58 | if (sql!="") { | ||
| 59 | if (sql.startsWith("#")) | ||
| 60 | message += sql.substring(1) | ||
| 61 | else { | ||
| 62 | nbqueries++; | ||
| 63 | def res = runQuery(connection, sql); | ||
| 64 | if (res!=""||request.showall=="1") { | ||
| 65 | println "=== ${message} ===" | ||
| 66 | println res; | ||
| 67 | } | ||
| 68 | message = "" | ||
| 69 | } | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | println "${nbqueries} sanity check queries have been run" | ||
| 74 | {{/groovy}} |