Changeset 5853


Ignore:
Timestamp:
Nov 7, 2011, 2:50:41 PM (10 years ago)
Author:
Nicklas Nordborg
Message:

References #1630: Migrate from MySQL to PostgreSQL

Forgot to add the 'Migration.java' file in the last checkin...

Parts of the import functionality has been done. It should create a new empty database, and import the static part of the database. The dynamic part is not imported, and no foreign keys or indexes are created.

Location:
trunk
Files:
2 added
8 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/HibernateUtil.java

    r5689 r5853  
    592592    @return The schemaVersion that is currently installed
    593593  */
    594   static int createStaticTables(boolean update, ProgressReporter progress)
     594  static int createStaticTables(SchemaGenerator.Mode mode, ProgressReporter progress)
    595595    throws BaseException
    596596  {
     
    598598
    599599    int currentSchemaVersion = Install.NEW_SCHEMA_VERSION;
    600     if (update)
     600    if (mode == SchemaGenerator.Mode.UPDATE)
    601601    {
    602602      currentSchemaVersion = Application.getSchemaVersion();       
     
    627627    try
    628628    {
    629       SchemaGenerator schemaGenerator = new SchemaGenerator(cfg, dialect, dbEngine, update, progress);
     629      SchemaGenerator schemaGenerator = new SchemaGenerator(cfg, dialect, dbEngine, mode, progress);
    630630      session = HibernateUtil.newSession();
    631631      tx = HibernateUtil.newTransaction(session);
  • trunk/src/core/net/sf/basedb/core/Install.java

    r5833 r5853  
    6363import net.sf.basedb.core.data.MappingCoordinate;
    6464import net.sf.basedb.core.data.PluginTypeData;
     65import net.sf.basedb.core.hibernate.SchemaGenerator;
    6566
    6667import net.sf.basedb.core.SessionControl;
     
    119120  public static final int NEW_SCHEMA_VERSION = Integer.valueOf(100).intValue();
    120121 
    121   public static synchronized int createTables(boolean update, ProgressReporter progress,
     122  public static synchronized int createTables(SchemaGenerator.Mode mode, ProgressReporter progress,
    122123    String rootLogin, String rootPassword)
    123124    throws BaseException
     
    129130      Application.start(true, false, false);
    130131     
    131       if (update)
     132      if (mode == SchemaGenerator.Mode.UPDATE)
    132133      {
    133134        Update.verifyRootLoginAndPassword(rootLogin, rootPassword);
     
    135136     
    136137      if (progress != null) progress.display(0, "Building database...");
    137       schemaVersion = HibernateUtil.createStaticTables(update, progress);
     138      schemaVersion = HibernateUtil.createStaticTables(mode, progress);
    138139      message = "Database built successfully.";
    139140    }
     
    159160    will not be modified.
    160161   
    161     @param update TRUE if the run is an update, FALSE otherwise.
     162    @param mode The installation mode
    162163    @param progress An object implementing the {@link ProgressReporter}
    163164      interface
     
    166167    @throws BaseException This exception is thrown if there is an error
    167168  */
    168   public static synchronized void initDatabase(boolean update, ProgressReporter progress, String rootLogin, String rootPassword)
     169  public static synchronized void initDatabase(SchemaGenerator.Mode mode, ProgressReporter progress, String rootLogin, String rootPassword)
    169170    throws BaseException
    170171  {
     
    178179      Application.start(false, false, false);
    179180      session = HibernateUtil.newSession();
     181      boolean update = mode == SchemaGenerator.Mode.UPDATE;
    180182 
    181183      // SchemaVersion
  • trunk/src/core/net/sf/basedb/core/dbengine/AbstractDbEngine.java

    r5852 r5853  
    2323
    2424import java.util.regex.Pattern;
     25
     26import net.sf.basedb.core.hibernate.SchemaGenerator;
    2527
    2628import org.hibernate.dialect.Dialect;
     
    193195    statements that doesn't supply a default value for the new column
    194196    when updating.
    195     @since 2.16
     197    @since 3.1
    196198  */
    197199  @Override
    198200  public String inspectSchemaGenerationSQL(String sql, Dialect dialect,
    199       boolean update)
     201      SchemaGenerator.Mode mode)
    200202  {
    201203    String addColumn = dialect.getAddColumnString();
    202     if (update && sql.contains("alter table") && sql.contains(addColumn) &&
     204    if (mode == SchemaGenerator.Mode.UPDATE &&
     205        sql.contains("alter table") && sql.contains(addColumn) &&
    203206        sql.contains("not null") && !sql.contains("default"))
    204207    {
  • trunk/src/core/net/sf/basedb/core/dbengine/DbEngine.java

    r5852 r5853  
    363363    @param sql The original SQL statment as generated by Hibernate
    364364    @param dialect The Hibernate dialect currently in use
    365     @param update TRUE if we are updating an existing database,
    366       FALSE if we are creating a new one
     365    @param mode The installation mode
    367366    @return The SQL statement which may have been modified or null
    368367      to skip executing the statment
    369     @since 2.16
    370   */
    371   public String inspectSchemaGenerationSQL(String sql, Dialect dialect, boolean update);
     368    @since 3.1
     369  */
     370  public String inspectSchemaGenerationSQL(String sql, Dialect dialect, SchemaGenerator.Mode mode);
    372371 
    373372}
  • trunk/src/core/net/sf/basedb/core/dbengine/PostgresDbEngine.java

    r5852 r5853  
    2323
    2424import java.util.Set;
     25
     26import net.sf.basedb.core.hibernate.SchemaGenerator;
    2527
    2628import org.hibernate.dialect.Dialect;
     
    243245    with unique names. PostgreSQL users should always run the ./updateindexes.sh
    244246    tool which will create the missing indexes.
    245     @since 2.16
     247    @since 3.1
    246248  */
    247249  @Override
    248250  public String inspectSchemaGenerationSQL(String sql, Dialect dialect,
    249       boolean update)
    250   {
    251     sql = super.inspectSchemaGenerationSQL(sql, dialect, update);
     251      SchemaGenerator.Mode mode)
     252  {
     253    sql = super.inspectSchemaGenerationSQL(sql, dialect, mode);
    252254    if (sql != null && sql.startsWith("create index"))
    253255    {
    254256      sql = null;
    255257    }
     258   
     259    if (sql != null && mode == SchemaGenerator.Mode.MIGRATE)
     260    {
     261      if (sql.startsWith("alter table") && sql.contains("add constraint") && sql.contains("foreign key"))
     262      {
     263        // Skip foreign key creation
     264        sql = null;
     265      }
     266      else if (sql.contains("unique"))
     267      {
     268        // Do not add "unique" contstraints
     269        // Variant 1: ... , unique (column, column) ...
     270        sql = sql.replaceAll(", unique \\([^)]*\\)", "");
     271        // Variant 2: ... column ... unique, ....
     272        sql = sql.replaceAll("unique,", ",");
     273      }
     274     
     275    }
     276   
    256277    return sql;
    257278  }
  • trunk/src/core/net/sf/basedb/core/dbengine/TableInfo.java

    r5852 r5853  
    7979  {
    8080    List<Table> tables = new LinkedList<Table>();
    81     ResultSet result = metaData.getTables(catalog, schema, null, null);
     81    ResultSet result = metaData.getTables(catalog, schema, null, new String[] { "TABLE" });
    8282    while (result.next())
    8383    {
     
    8585      String tableCatalog = result.getString("TABLE_CAT");
    8686      String tableSchema = result.getString("TABLE_SCHEM");
     87     
     88      //System.out.println(tableName + ":" + result.getString("TABLE_TYPE"));
     89     
    8790      Table table = new Table();
    8891      table.setName(tableName);
  • trunk/src/core/net/sf/basedb/core/hibernate/SchemaGenerator.java

    r5427 r5853  
    5656  private final Dialect dialect;
    5757  private final DbEngine dbEngine;
    58   private final boolean update;
     58  private final Mode mode;
    5959  private final ProgressReporter progress;
    6060
     
    6565    @param dialect The dialect of the database we are connecting to
    6666    @param dbEngine The DbEngine for the database we are connecting to
    67     @param update TRUE if we are updating an existing database, FALSE if
    68       we are creating a new one
     67    @param mode The installation mode
    6968    @param progress An optional progress reporter
    7069  */
    71   public SchemaGenerator(Configuration cfg, Dialect dialect, DbEngine dbEngine, boolean update, ProgressReporter progress)
     70  public SchemaGenerator(Configuration cfg, Dialect dialect, DbEngine dbEngine, Mode mode, ProgressReporter progress)
    7271  {
    7372    this.cfg = cfg;
    7473    this.dialect = dialect;
    7574    this.dbEngine = dbEngine;
    76     this.update = update;
     75    this.mode = mode;
    7776    this.progress = progress;
    7877  }
     
    9493    {
    9594      String[] allSql = null;
    96       if (update)
     95      if (mode == Mode.UPDATE)
    9796      {
    9897        log.info("Fetching database metadata");
     
    109108      for (String sql : allSql)
    110109      {
    111         String actualSql = dbEngine.inspectSchemaGenerationSQL(sql, dialect, update);
     110        String actualSql = dbEngine.inspectSchemaGenerationSQL(sql, dialect, mode);
    112111        if (actualSql != null)
    113112        {
     
    118117          log.debug("Executing: " + actualSql);
    119118          stmt.executeUpdate(actualSql);
     119          //System.out.println(actualSql);
     120          if (progress != null)
     121          {
     122            if (numDone % interval == 0)
     123            {
     124              progress.display((100 * numDone) / numStatements, "--" + StringUtil.trimString(actualSql, 45));
     125            }
     126            else
     127            {
     128              progress.append(".");
     129            }
     130          }
    120131        }
    121132        else
    122133        {
    123134          log.debug("Ignoring: " + sql);
    124         }
    125         if (progress != null)
    126         {
    127           if ((numDone % interval == 0) && (actualSql != null))
    128           {
    129             progress.display((100 * numDone) / numStatements, "--" + StringUtil.trimString(actualSql, 45));
    130           }
    131           else
    132           {
    133             progress.append(".");
    134           }
    135135        }
    136136        numDone++;
     
    158158  }
    159159
     160  /**
     161    The installation mode.
     162    @since 3.1
     163  */
     164  public static enum Mode
     165  {
     166    /**
     167      Creating a fresh installation.
     168    */
     169    INSTALL,
     170    /**
     171      Updating an existing installation.
     172    */
     173    UPDATE,
     174    /**
     175      Creating a minimal database schema for migration. The
     176      schema should be created without foreign key and
     177      other constraints.
     178    */
     179    MIGRATE
     180  }
     181 
    160182}
  • trunk/src/install/net/sf/basedb/install/InitDB.java

    r5852 r5853  
    3636import net.sf.basedb.core.Update;
    3737import net.sf.basedb.core.Version;
     38import net.sf.basedb.core.hibernate.SchemaGenerator;
    3839import net.sf.basedb.util.ConsoleProgressReporter;
    3940import net.sf.basedb.util.ChainedProgressReporter;
     
    6768        String rootLogin = args.length == 3 ? args[1] : "root";
    6869       
    69         boolean update = "update".equals(cmd);
     70        SchemaGenerator.Mode mode = "update".equals(cmd) ?
     71            SchemaGenerator.Mode.UPDATE : SchemaGenerator.Mode.INSTALL;
    7072        ChainedProgressReporter progress = new ChainedProgressReporter(new ConsoleProgressReporter());
    7173        progress.setRange(0, 30);
    7274        showDbInfo(progress);
    73         int schemaVersion = Install.createTables(update, progress, rootLogin, rootPassword);
     75        int schemaVersion = Install.createTables(mode, progress, rootLogin, rootPassword);
    7476        progress.setRange(30, 50);
    75         if (update)
     77        if (mode == SchemaGenerator.Mode.UPDATE)
    7678        {
    7779          if (schemaVersion < 99)
     
    8587        }
    8688        progress.setRange(50, 75);
    87         Install.initDatabase(update, progress, rootLogin, rootPassword);
     89        Install.initDatabase(mode, progress, rootLogin, rootPassword);
    8890        progress.setRange(75, 90);
    89         if (update) Update.updateDatabase(progress, rootLogin, rootPassword);
     91        if (mode == SchemaGenerator.Mode.UPDATE)
     92        {
     93          Update.updateDatabase(progress, rootLogin, rootPassword);
     94        }
    9095        progress.setRange(95, 100);
    9196        progress.display(0, "Installing web application...");
     
    141146        if (export)
    142147        {
    143           Application.start(false);
    144148          Migration.exportAll(path, new ConsoleProgressReporter(false));
    145149        }
    146150        else
    147151        {
     152          ChainedProgressReporter progress = new ChainedProgressReporter(new ConsoleProgressReporter());
    148153          // Create empty database without indexes and foreign keys
     154          progress.setRange(0, 5);
     155          Install.createTables(SchemaGenerator.Mode.MIGRATE, progress, null, null);
     156          progress.append("\n");
     157
    149158          // Import all data
     159          progress = new ChainedProgressReporter(new ConsoleProgressReporter(false));
     160          progress.setRange(5, 90);
     161          Migration.importAll(path, progress);
     162         
    150163          // Add indexes, foreign keys, etc. to database
    151164        }
Note: See TracChangeset for help on using the changeset viewer.