Changeset 2142


Ignore:
Timestamp:
Apr 3, 2006, 1:14:21 PM (18 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #184: Running SQL/HQL after an update to the database.

Location:
trunk
Files:
2 added
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/common-queries.xml

    r2129 r2142  
    16221622    </description>
    16231623  </query>
     1624 
     1625  <query id="GET_SCHEMA_VERSION" type="HQL">
     1626    <sql>
     1627      SELECT sv
     1628      FROM SchemaVersionData sv
     1629    </sql>
     1630    <description>
     1631      A Hibernate query that loads the schema version (there should only be one record).
     1632    </description>
     1633  </query>
     1634 
     1635  <query id="SET_SCHEMA_VERSION" type="HQL">
     1636    <sql>
     1637      UPDATE SchemaVersionData sv
     1638      SET sv.schemaVersion = :schemaVersion,
     1639      sv.build = :build
     1640    </sql>
     1641    <description>
     1642      A Hibernate query that updates the schema version and build number.
     1643    </description>
     1644  </query>
     1645 
     1646  <query id="SET_PROJECT_ID_FOR_JOBS" type="HQL">
     1647    <sql>
     1648      UPDATE JobData job
     1649      SET job.activeProjectId = 0
     1650      WHERE job.activeProjectId IS NULL
     1651    </sql>
     1652    <description>
     1653      A Hibernate query that sets the active project ID for all
     1654      jobs to 0 if they have a null value.
     1655    </description>
     1656  </query>
    16241657
    16251658</predefined-queries>
  • trunk/src/core/net/sf/basedb/core/Application.java

    r2038 r2142  
    2626
    2727import net.sf.basedb.core.data.ClientData;
     28import net.sf.basedb.core.data.SchemaVersionData;
    2829import net.sf.basedb.core.authentication.Authenticator;
    2930
     
    185186    return BUILD;
    186187  }
     188 
     189  /**
     190    Get the current database schema version number.
     191    @return The schema version number, or -1 if it could not be loaded
     192  */
     193  public static int getSchemaVersion()
     194  {
     195    org.hibernate.Session session = null;
     196    org.hibernate.Transaction tx = null;
     197    int schemaVersion = -1;
     198    try
     199    {
     200      session = HibernateUtil.newSession();
     201      tx = HibernateUtil.newTransaction(session);
     202      org.hibernate.Query query = HibernateUtil.getPredefinedQuery(session, "GET_SCHEMA_VERSION");
     203      schemaVersion = HibernateUtil.loadData(SchemaVersionData.class, query).getSchemaVersion();
     204      HibernateUtil.commit(tx);
     205    }
     206    catch (BaseException ex)
     207    {
     208      if (tx != null) HibernateUtil.rollback(tx);
     209      log.error("getSchemaVersion", ex);
     210    }
     211    finally
     212    {
     213      if (session != null) HibernateUtil.close(session);
     214    }
     215    return schemaVersion;
     216  }
     217 
    187218 
    188219  /**
  • trunk/src/core/net/sf/basedb/core/HibernateUtil.java

    r2051 r2142  
    439439      session = HibernateUtil.newSession();
    440440      DatabaseMetaData metaData = session.connection().getMetaData();
     441     
    441442      Iterator<PersistentClass> classes = cfg.getClassMappings();
    442443      while (classes.hasNext() && isEmpty)
     
    10871088    }
    10881089  }
    1089  
     1090  /**
     1091    Exceute an update or delete query.
     1092   
     1093    @param query A <code>Query</code> object which is executed using the
     1094      {@link Query#executeUpdate()} method
     1095    @return The number of affected items
     1096  */
     1097  static int executeUpdate(Query query)
     1098    throws BaseException
     1099  {
     1100    assert query != null : "query == null";
     1101    try
     1102    {
     1103      return query.executeUpdate();
     1104    }
     1105    catch(HibernateException ex)
     1106    {
     1107      throw new BaseException(ex);
     1108    }
     1109  }
     1110
    10901111  /**
    10911112    Get the value of a property on a data object using Hibernate
  • trunk/src/core/net/sf/basedb/core/Install.java

    r2106 r2142  
    3636import net.sf.basedb.core.data.QuotaIndex;
    3737import net.sf.basedb.core.data.RoleData;
     38import net.sf.basedb.core.data.SchemaVersionData;
    3839import net.sf.basedb.core.data.SoftwareTypeData;
    3940import net.sf.basedb.core.data.SoftwareData;
     
    8687  private static SessionControl sessionControl = null;
    8788
     89  /**
     90    The schema version to give to new installations. Already
     91    existing installations will be updated to this version number
     92    by the {@link Update#updateDatabase(ProgressReporter, String, String)}
     93    method.
     94  */
     95  private static final int NEW_SCHEMA_VERSION = 2;
     96
    8897  public static synchronized void createTables(boolean update, final ProgressReporter progress)
    8998    throws BaseException
     
    139148    @throws BaseException This exception is thrown if there is an error
    140149  */
    141   public static synchronized void initDatabase(ProgressReporter progress, String rootPassword)
    142     throws BaseException
    143   {
    144     Application.start(false);
    145     session = HibernateUtil.newSession();
    146 
     150  public static synchronized void initDatabase(boolean update, ProgressReporter progress, String rootLogin, String rootPassword)
     151    throws BaseException
     152  {
    147153    final int total_progress_steps = 22;
    148154    final float progress_factor = 100 / total_progress_steps;
     
    150156    try
    151157    {
     158      Application.start(false);
     159      session = HibernateUtil.newSession();
     160
     161      // SchemaVersion
     162      createSchemaVersion(update ? 1 : NEW_SCHEMA_VERSION);
    152163
    153164      // QuotaTypes
     
    177188      // Users
    178189      if (progress != null) progress.display((int)(4*progress_factor), "--Creating users...");
    179       UserData rootUser = createUser(User.ROOT, "root", rootPassword, "Root", "This is the root user account of BASE. It has full permission to everything.", roleAdmin, quotaUnlimit);
     190      UserData rootUser = createUser(User.ROOT, rootLogin, rootPassword, "Root", "This is the root user account of BASE. It has full permission to everything.", roleAdmin, quotaUnlimit);
    180191      // createUser(0, "demo", "demo", "Demo", "This account can be used for demonstration purposes.", Role.GUEST);
    181192      // createUser(0, "power", "power", "Power user", "This account has power user privileges.", Role.POWER_USER);
     
    183194      // Now that we have a root user let's create a session
    184195      sessionControl = Application.newSessionControl( null, null, null );
    185       sessionControl.login("root", rootPassword, "InitDBSessionId", false);
     196      sessionControl.login(rootLogin, rootPassword, "InitDBSessionId", false);
    186197
    187198      if (progress != null) progress.display((int)(5*progress_factor), "--Creating groups...");
     
    577588  }
    578589 
     590  /**
     591    Create the schema version.
     592  */
     593  private static SchemaVersionData createSchemaVersion(int sv)
     594    throws BaseException
     595  {
     596    org.hibernate.Transaction tx = null;
     597    SchemaVersionData schemaVersion = null;
     598    try
     599    {
     600      tx = HibernateUtil.newTransaction(session);
     601
     602      org.hibernate.Query query = HibernateUtil.getPredefinedQuery(session, "GET_SCHEMA_VERSION");
     603      schemaVersion = HibernateUtil.loadData(SchemaVersionData.class, query);
     604      if (schemaVersion != null)
     605      {
     606        log.info("createSchemaVersion: EXISTS");
     607        HibernateUtil.commit(tx);
     608      }
     609      else
     610      {
     611        schemaVersion = new SchemaVersionData();
     612        schemaVersion.setBuild(Application.getBuild());
     613        schemaVersion.setSchemaVersion(sv);
     614        HibernateUtil.saveData(session, schemaVersion);
     615        HibernateUtil.commit(tx);
     616        log.info("createSchemaVersion: OK");
     617      }
     618    }
     619    catch (BaseException ex)
     620    {
     621      if(tx != null) HibernateUtil.rollback(tx);
     622      log.error("createSchemaVersion: FAILED", ex);
     623      throw ex;
     624    }
     625    return schemaVersion;
     626  }
     627
    579628  /**
    580629    Create a {@link Directory}.
  • trunk/src/install/net/sf/basedb/install/InitDB.java

    r2021 r2142  
    2626
    2727import net.sf.basedb.core.Install;
    28 import net.sf.basedb.core.ProgressReporter;
     28import net.sf.basedb.core.Update;
    2929import net.sf.basedb.util.ConsoleProgressReporter;
    3030import net.sf.basedb.util.ChainedProgressReporter;
     
    5050    }
    5151    String rootPassword = args.length == 2 ? args[1] : args[0];
     52    String rootLogin = "root";
    5253    boolean update = args.length == 2 && "update".equals(args[0]);
    5354    try
     
    5657      progress.setRange(0, 30);
    5758      Install.createTables(update, progress);
    58       progress.setRange(35, 90);
    59       Install.initDatabase(progress, rootPassword);
     59      progress.setRange(35, 70);
     60      Install.initDatabase(update, progress, rootLogin, rootPassword);
     61      progress.setRange(75, 90);
     62      Update.updateDatabase(progress, rootLogin, rootPassword);
    6063      progress.setRange(95, 100);
    6164      progress.display(0, "Installing web application...");
     
    6366      progress.display(100, "Web application installed successfully.\n");
    6467    }
    65     catch (Exception ex)
     68    catch (Throwable t)
    6669    {
    6770      System.out.println("\n");
    68       ex.printStackTrace();
     71      t.printStackTrace();
    6972    }
    7073  }
  • trunk/src/test/TestInstall.java

    r1913 r2142  
    3737      ProgressReporter progress = new ConsoleProgressReporter();
    3838      Install.createTables(false, progress);
    39       Install.initDatabase(progress, "root");
     39      Install.initDatabase(false, progress, "root", "root");
    4040      progress.display(100, "Database installed successfully.\n");
    4141    }
  • trunk/src/test/TestUpdate.java

    r2021 r2142  
    3737      ProgressReporter progress = new ConsoleProgressReporter();
    3838      Install.createTables(true, progress);
    39       Install.initDatabase(progress, "root");
     39      Install.initDatabase(true, progress, "root", "root");
     40      Update.updateDatabase(progress, "root", "root");
    4041      progress.display(100, "Database updated successfully.\n");
    4142    }
  • trunk/www/exception/exception.jsp

    r2118 r2142  
    116116    <tr>
    117117      <td class="prompt">Base version</td>
    118       <td><%=Application.getMajorVersion() + "." +Application.getMinorVersion() + " (build #" + Application.getBuild()+")"%></td>
     118      <td><%=Application.getMajorVersion() + "." +Application.getMinorVersion() + " (build #" + Application.getBuild()+"; schema #" + Application.getSchemaVersion()+")"%></td>
    119119    </tr>
    120120    <tr>
  • trunk/www/info/about.jsp

    r2091 r2142  
    6060    <b>Administrator:</b> <%=Values.getString(serverAdminName, "<i>- unknown -</i>")%><br>
    6161    <b>Email:</b> <%=serverAdminEmail == null ? "<i>- unknown -</i>": "<a href=\"mailto:"+serverAdminEmail+"\">"+serverAdminEmail+"</a>"%><br>
    62     <b>Version:</b> <%=Application.getMajorVersion() + "." +Application.getMinorVersion() + " (build #" + Application.getBuild()+")"%><br>
     62    <b>Version:</b> <%=Application.getMajorVersion() + "." +Application.getMinorVersion() + " (build #" + Application.getBuild()+"; schema #" + Application.getSchemaVersion()+")"%><br>
    6363    <br>
    6464    <font size="-1">
     
    7575  <tr>
    7676    <td class="prompt">Base version</td>
    77     <td><%=Application.getMajorVersion() + "." +Application.getMinorVersion() + " (build #" + Application.getBuild()+")"%></td>
     77    <td><%=Application.getMajorVersion() + "." +Application.getMinorVersion() + " (build #" + Application.getBuild()+"; schema #" + Application.getSchemaVersion()+")"%></td>
    7878  </tr>
    7979  <tr>
Note: See TracChangeset for help on using the changeset viewer.