Changeset 5735


Ignore:
Timestamp:
Sep 13, 2011, 3:51:08 PM (10 years ago)
Author:
Nicklas Nordborg
Message:

References #1589: Delete deprecated classes and methods

Removed all special fixes in OneTimeFix.java.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/install/net/sf/basedb/install/OneTimeFix.java

    r4889 r5735  
    2323package net.sf.basedb.install;
    2424
    25 import java.io.FileWriter;
    26 import java.io.PrintWriter;
    2725import java.sql.Connection;
    2826import java.sql.DatabaseMetaData;
    2927import java.sql.Driver;
    3028import java.sql.DriverManager;
    31 import java.sql.ResultSet;
    3229import java.sql.SQLException;
    33 import java.sql.Statement;
    34 import java.util.LinkedList;
    35 import java.util.List;
    3630import java.util.Properties;
    3731
    38 import org.hibernate.mapping.Table;
    3932
    4033import net.sf.basedb.core.Application;
    41 import net.sf.basedb.core.BioAssaySet;
    4234import net.sf.basedb.core.Config;
    43 import net.sf.basedb.core.DbControl;
    44 import net.sf.basedb.core.Experiment;
    45 import net.sf.basedb.core.Include;
    46 import net.sf.basedb.core.ItemQuery;
    47 import net.sf.basedb.core.Job;
    48 import net.sf.basedb.core.PluginDefinition;
    4935import net.sf.basedb.core.ProgressReporter;
    50 import net.sf.basedb.core.SessionControl;
    51 import net.sf.basedb.core.StringUtil;
    52 import net.sf.basedb.core.User;
    53 import net.sf.basedb.core.dbengine.TableInfo;
    54 import net.sf.basedb.core.dbengine.TableInfo.ColumnInfo;
    55 import net.sf.basedb.core.query.Hql;
    56 import net.sf.basedb.core.query.Orders;
    5736
    5837/**
     
    7756    {
    7857      String cmd = args[0];   
    79       if ("lowess_warn".equals(cmd))
     58      if ("foo".equals(cmd))
    8059      {
    81         String login = getOption(args, "-u", "root");
    82         String pwd = getOption(args, "-p", "root");
    83         boolean flag = getOption(args, "-f", null) != null;
    84        
    85         String msg =
    86           "This script looks for analysed data that has been normalized with\n" +
    87           "the LOWESS plug-in. This plug-in has had a serious bug that caused\n" +
    88           "incorrect normalization values to be calculated. The bug has been\n" +
    89           "fixed in BASE 2.7.2, but all data that has been normalized with\n" +
    90           "the LOWESS plug-in before this fix must be redone, including any\n" +
    91           "analysis steps following the normalization. For more information\n" +
    92           "about the bug see: http://base.thep.lu.se/ticket/1077\n\n" +
    93           "This script will find and display all experiments and bioassay sets\n" +
    94           "that has used the LOWESS normalization plug-in. It is not possible to\n" +
    95           "detect if the normalization was done before or after the fix was\n" +
    96           "applied. Use the '-f' option when executing this script to tag all\n" +
    97           "affected experiments and bioassay sets with 'FIX LOWESS' in their names.\n\n" +
    98           "Press ENTER to continue or CTRL+C to abort";
    99         System.out.println(msg);
    100         waitForEnter();
    101         showDbInfo(null);
    102         System.out.println("Starting BASE. Please wait...");
    103         Application.start(false);
    104         SessionControl sc = Application.newSessionControl(null, null, null);
    105         sc.login(login, pwd, null, false);
    106         showLowessBioassaySets(sc, flag);
    107         sc.logout();
    108         sc.close();
    109         Application.stop();
    110       }
    111       else if ("utf8".equals(cmd))
    112       {
    113         boolean execute = getOption(args, "-x", null) != null;
    114         boolean force = getOption(args, "-f", null) != null;
    115         String output = getOption(args, "-o", "convert-to-utf8.sql");
    116 
    117         showDbInfo(null);
    118         if (!isMySQL())
    119         {
    120           System.out.println("This script only works with MySQL.");
    121           return;
    122         }
    123        
    124         String msg =
    125           "This script is used for converting the character set used\n" +
    126           "by the BASE databases to UTF-8. Only run this script if you\n" +
    127           "are sure that conversion is needed. The script will first try\n" +
    128           "to find out if your database needs conversion or not.\n";
    129         System.out.println(msg);
    130 
    131         String dbCreateSql = getCreateSql();
    132         boolean isUtf8 = dbCreateSql.contains("utf8");
    133         msg = "======================================================\n" +
    134           dbCreateSql +
    135           "\n======================================================\n\n";
    136         if (!isUtf8)
    137         {
    138           msg +=
    139             "YOUR DATABASE NEEDS TO BE CONVERTED SINCE IT USES\n" +
    140             "A DIFFERENT CHARACTER SET THAN UTF-8.\n";
    141         }
    142         else
    143         {
    144           msg +=
    145             "YOUR DATABASE IS ALREADY USING UTF-8 AND DOESN'T HAVE\n" +
    146             "TO BE CONVERTED.\n";
    147         }
    148         if (execute)
    149         {
    150           msg += "Press\n   ENTER to convert the database";
    151           if (isUtf8) msg += " (not recommended)";
    152         }
    153         else
    154         {
    155           msg += "Press\n   ENTER to save SQL script to '" +
    156             output + "'";
    157         }
    158         msg += "\nor\n   CTRL-C to abort.";
    159        
    160         System.out.println(msg);
    161         waitForEnter();
    162        
    163         if (isUtf8 && execute)
    164         {
    165           System.out.println(
    166             "======================================================\n" +
    167             "YOUR DATABASE IS ALREADY USING UTF-8.\n" +
    168             "ARE YOU SURE YOU WANT TO RUN THIS SCRIPT?\n" +
    169             "======================================================\n" +
    170             "Press ENTER to continue or CTRL-C to abort.");
    171           waitForEnter();
    172         }
    173        
    174         System.out.println("Starting UTF-8 conversion/script generation. Please wait...");
    175         PrintWriter out = new PrintWriter(new FileWriter(output));
    176         convertToUtf8(execute, force, out);
    177         out.flush();
    178         out.close();
     60        showUsage();
    17961      }
    18062      else
     
    19678    System.out.println("    -u <login>    : Login name for the root user account; (root)");
    19779    System.out.println("    -p <password> : Password for the root user account; (root)");
    198     System.out.println("cmd=lowess_warn: ");
    199     System.out.println("    -f            : Tag names of experiments with 'FIX LOWESS'");
    20080  }
    20181
     
    259139  }
    260140 
    261   private static void showLowessBioassaySets(SessionControl sc, boolean flag)
    262   {
    263     DbControl dc = sc.newDbControl();
    264     int totalBas = 0;
    265     int totalExp = 0;
    266     try
    267     {
    268       ItemQuery<Experiment> expQuery = Experiment.getQuery();
    269       expQuery.include(Include.ALL);
    270       expQuery.order(Orders.asc(Hql.property("name")));
    271      
    272       for (Experiment exp : expQuery.list(dc))
    273       {
    274         User owner = exp.getOwner();
    275         ItemQuery<BioAssaySet> basQuery = exp.getBioAssaySets();
    276         basQuery.order(Orders.asc(Hql.property("name")));
    277        
    278         boolean hasPrintedExp = false;
    279        
    280         for (BioAssaySet bas : basQuery.list(dc))
    281         {
    282           Job job = bas.getTransformation().getJob();
    283           if (job != null)
    284           {
    285             PluginDefinition plugin = job.getPluginDefinition();
    286             if (plugin != null)
    287             {
    288               if (plugin.getClassName().equals("net.sf.basedb.plugins.LowessNormalization"))
    289               {
    290                 if (!hasPrintedExp)
    291                 {
    292                   String msg = "\nExperiment: " + exp.getName() +
    293                     "; owner: " + owner.getName() +
    294                     " \n--------------------------------";
    295                   System.out.println(msg);
    296                   hasPrintedExp = true;
    297                   totalExp++;
    298                   if (flag && !exp.getName().startsWith("FIX LOWESS"))
    299                   {
    300                     String newName = "FIX LOWESS: " + exp.getName();
    301                     exp.setName(StringUtil.trimString(newName, Experiment.MAX_NAME_LENGTH));
    302                   }
    303                 }
    304                 System.out.println("  " + bas.getName() + "; id: " + bas.getId());
    305                 if (flag && !bas.getName().startsWith("FIX LOWESS"))
    306                 {
    307                   String newName = "FIX LOWESS: " + bas.getName();
    308                   bas.setName(StringUtil.trimString(newName, BioAssaySet.MAX_NAME_LENGTH));
    309                 }
    310                 totalBas++;
    311               }
    312             }
    313           }
    314         }
    315       }
    316       dc.commit();
    317     }
    318     finally
    319     {
    320       if (dc != null) dc.close();
    321     }
    322     System.out.println("--------------------------");
    323     if (totalBas == 0)
    324     {
    325       System.out.println("Could not find any experiments that has used the LOWESS plug-in.");
    326     }
    327     else
    328     {
    329       System.out.println("Found " +totalBas + " LOWESS calculations in " + totalExp + " experiments");
    330     }
    331   }
    332141 
    333142  /**
     
    365174  }
    366175 
    367   /**
    368     Get the SQL statement that was used to create the
    369     current BASE database.
    370     @since 2.9
    371   */
    372   private static String getCreateSql()
    373     throws Exception
    374   {
    375     Connection c = null;
    376     Statement s = null;
    377     ResultSet rs = null;
    378     String createSql = null;
    379     try
    380     {
    381       c = getConnection();
    382       DatabaseMetaData metaData = c.getMetaData();
    383       String dbName = metaData.getDatabaseProductName();
    384       String sql = "SHOW CREATE DATABASE `" + c.getCatalog() + "`";
    385       s = c.createStatement();
    386       rs = s.executeQuery(sql);
    387       rs.next();
    388       createSql = "Q: " + sql + "\nA: " + rs.getString("Create Database");
    389     }
    390     finally
    391     {
    392       if (rs != null) rs.close();
    393       if (s!= null) s.close();
    394       if (c != null) c.close();
    395     }
    396     return createSql;
    397   }
    398  
    399   /**
    400     Get the SQL statement that was used to create a given table.
    401     @since 2.9
    402   */
    403   private static String getCreateTableSql(Statement s, String catalog, String table)
    404     throws Exception
    405   {
    406     ResultSet rs = null;
    407     String sql = null;
    408     try
    409     {
    410       rs = s.executeQuery("SHOW CREATE TABLE `" + catalog + "`.`" + table + "`");
    411       rs.next();
    412       sql = rs.getString("Create Table");
    413     }
    414     finally
    415     {
    416       if (rs != null) rs.close();
    417     }
    418     return sql;
    419   }
    420  
    421   /**
    422     Convert a BASE database to use UTF-8 for all tables. This only works
    423     with MySQL.
    424     @param execute FALSE to only generate the SQL statements, TRUE to
    425       actually execute them on the database server
    426     @param force TRUE to force conversion of tables that seems to
    427       not need conversion
    428     @param out The generated SQL statements are written to this
    429       writer
    430     @since 2.9
    431   */
    432   private static void convertToUtf8(boolean execute, boolean force, PrintWriter out)
    433     throws Exception
    434   {
    435     String dynamicDb = Config.getString("db.dynamic.catalog");
    436     List<String> sqlList = new LinkedList<String>();
    437     Connection c = null;
    438     Statement s = null;
    439     try
    440     {
    441       c = getConnection();
    442       String mainDb = c.getCatalog();
    443       DatabaseMetaData metaData = c.getMetaData();
    444      
    445       generateConvertSql(metaData, mainDb, force, true, sqlList);
    446       generateConvertSql(metaData, dynamicDb, force, false, sqlList);
    447      
    448       if (execute) s = c.createStatement();
    449       for (String sql : sqlList)
    450       {
    451         if (out != null) out.println(sql + ";");
    452         if (execute)
    453         {
    454           System.out.println(sql);
    455           long t1 = System.currentTimeMillis();
    456           int count = s.executeUpdate(sql);
    457           long t2 = System.currentTimeMillis();
    458           long t = t2 - t1;
    459           String time = t < 10000 ? t + " milliseconds" : (int)(t/1000) + " seconds";
    460           System.out.println(count + " rows affected (" + time + ")\n");
    461         }
    462       }
    463     }
    464     finally
    465     {
    466       if (s!= null) s.close();
    467       if (c != null) c.close();
    468     }
    469   }
    470  
    471   /**
    472     Generates SQL conversion script for all tables in a given
    473     database (catalog). The is method will generate SQL like this:
    474     <pre class="code">
    475 ALTER TABLE &lt;table-name&gt;
    476 CONVERT TO CHARACTER SET utf8,
    477 DEFAULT CHARACTER SET utf8;
    478 </pre>
    479 
    480     <ul>
    481     <li>If the table is already using UTF-8 it is skipped,
    482       unless forceConvert=TRUE
    483     <li>If the table doesn't have string columns, the following can happen:
    484       <ol>
    485       <li>forceConvert=TRUE: The table is converted
    486       <li>forceConvert=FALSE and changeDefaultForAllTables=TRUE:
    487         Only the default character set is changed. The <code>CONVERT TO...</code>
    488         part of the query is skipped.
    489       <li>forceConvert=FALSE and changeDefaultForAllTables=FALSE</code>:
    490         Nothing is done to the table.
    491       </ol>
    492     </ul>
    493 
    494     @param metaData The metadata object for the database
    495     @param catalog The name of the database to convert
    496     @param forceConvert TRUE to generate SQL for converting tables
    497       that seems to not need conversion
    498     @param changeDefaultForAllTables TRUE to also change the default
    499       character set for tables that has no string columns
    500     @param sqlList
    501     @throws Exception
    502     @since 2.9
    503    */
    504   private static void generateConvertSql(DatabaseMetaData metaData, String catalog,
    505       boolean forceConvert, boolean changeDefaultForAllTables, List<String> sqlList)
    506     throws Exception
    507   {
    508     ResultSet rs = null;
    509     Statement s = null;
    510     try
    511     {
    512       s = metaData.getConnection().createStatement();
    513       // Load info about all tables in the given DB.
    514       rs = metaData.getTables(catalog, null, null, new String[] { "TABLE" });
    515       while (rs.next())
    516       {
    517         Table tbl = new Table();
    518         tbl.setCatalog(rs.getString("TABLE_CAT"));
    519         tbl.setName(rs.getString("TABLE_NAME"));
    520         TableInfo info = new TableInfo(tbl, metaData);
    521        
    522         // Check if the table has at least one string-type column
    523         boolean hasStringColumn = false;
    524         for (ColumnInfo col : info.getColumns())
    525         {
    526           if (col.isStringType())
    527           {
    528             hasStringColumn = true;
    529             break;
    530           }
    531         }
    532        
    533         // Check if the table already uses UTF-8
    534         String createSql = getCreateTableSql(s, tbl.getCatalog(), tbl.getName());
    535         boolean isUtf8 = createSql.contains("utf8");
    536        
    537         // Generate conversion script
    538         String msg = "Table '" + tbl.getCatalog() + "." + tbl.getName() + ": ";
    539         if (!isUtf8 || forceConvert)
    540         {
    541           String sql = "ALTER TABLE `" + tbl.getCatalog() + "`.`" + tbl.getName() + "` " +
    542             "DEFAULT CHARACTER SET utf8";
    543           if (hasStringColumn || forceConvert)
    544           {
    545             sql += ", CONVERT TO CHARACTER SET utf8";
    546             msg += "Need conversion to UTF-8.";
    547           }
    548           else if (changeDefaultForAllTables)
    549           {
    550             msg += "Has no string-type columns. Change default character set to UTF-8.";
    551           }
    552           else
    553           {
    554             sql = null;
    555             msg += "Has no string-type columns. No change needed.";
    556           }
    557           if (sql != null) sqlList.add(sql);
    558         }
    559         else
    560         {
    561           msg += "Uses UTF-8 already. No conversion needed.";
    562         }
    563         System.out.println(msg);
    564       }
    565       rs.close();
    566     }
    567     finally
    568     {
    569       if (rs != null) rs.close();
    570     }
    571     sqlList.add("ALTER DATABASE `" + catalog + "` DEFAULT CHARACTER SET utf8");
    572   }
    573176
    574177  public static void waitForEnter()
Note: See TracChangeset for help on using the changeset viewer.