3.2.1: 2012-12-13

net.sf.basedb.core
Class Migration

java.lang.Object
  extended by net.sf.basedb.core.Migration
All Implemented Interfaces:
JdbcWork<Object>, Work

public final class Migration
extends Object
implements JdbcWork<Object>

Class for migrating a database to PostgreSQL. The exportAll(String, ProgressReporter) method should work on both MySQL and PostgreSQL (but is only tested regularly with MySQL). The export generates data that is suitable for importing into PostgreSQL using the COPY command. The importAll(String, ProgressReporter) only works on a PostgreSQL database.

Since:
3.1
Author:
Nicklas
Last modified
$Date: 2011-12-02 15:28:46 +0100 (Fri, 02 Dec 2011) $

Nested Class Summary
(package private)  class Migration.ProgressInputStream
           
 
Field Summary
private  boolean analyzeAfterImport
           
private  File directory
           
private  boolean dropConstraintsBeforeImport
           
private  boolean dropPrimaryKeyBeforeImport
           
private  boolean export
           
private  boolean exportCompressed
           
private  int fetchSize
           
private  DecimalFormat numRowsFormat
           
private  ProgressReporter progress
           
private  Session session
           
 
Constructor Summary
private Migration(boolean export, File directory, Session session, ProgressReporter progress)
           
 
Method Summary
private  void doExport(Connection connection)
          Perform the export.
private  void doImport(Connection connection)
          Perform the import.
 void execute(Connection connection)
           
static void exportAll(String path, ProgressReporter progress)
          Export the current BASE database to data files in the directory on the given path.
private  File getColumnsFile(TableInfo table)
          Get the columns file for the given table.
private  File getDataFile(TableInfo table)
          Get the data file for the given table.
private  File getDataFile(TableInfo table, boolean compress)
          Get the data file for the given table.
private  InputStream getInputStream(File file)
          Get a stream for reading from the given file.
private  OutputStream getOutputStream(File file)
          Get a stream for writing to the given file.
 Object getResult()
          Get the result of the work.
static void importAll(String path, ProgressReporter progress)
          Import data to a BASE database.
private  Integer importFromFile(TableInfo table, File dataFile, File columnsFile, DbEngine engine, Statement st, ProgressReporter progress)
          Import data to the given table.
private  String numRows(long numRows)
           
 void setAnalyzeAfterImport(boolean analyze)
          Should we execute an analyis command after the import has been completed.
 void setDropConstraintsBeforeImport(boolean drop)
          Should we drop the unique constraints and indexes before importing data?
 void setDropPrimaryKeyBeforeImport(boolean drop)
          Should we drop the primary key before importing data?
 void setExportCompressed(boolean compress)
          Should the exporter compress the data files or leave them as is?
 void setFetchSize(int fetchSize)
          Set the JDBC fetch size to use when fetching rows from the database.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

export

private final boolean export

directory

private final File directory

session

private final Session session

progress

private final ProgressReporter progress

numRowsFormat

private final DecimalFormat numRowsFormat

exportCompressed

private boolean exportCompressed

fetchSize

private int fetchSize

dropPrimaryKeyBeforeImport

private boolean dropPrimaryKeyBeforeImport

dropConstraintsBeforeImport

private boolean dropConstraintsBeforeImport

analyzeAfterImport

private boolean analyzeAfterImport
Constructor Detail

Migration

private Migration(boolean export,
                  File directory,
                  Session session,
                  ProgressReporter progress)
Method Detail

exportAll

public static void exportAll(String path,
                             ProgressReporter progress)
                      throws SQLException,
                             IOException
Export the current BASE database to data files in the directory on the given path.

Parameters:
path - A path to an existing directory
progress - A progress reporter
Throws:
SQLException
IOException

importAll

public static void importAll(String path,
                             ProgressReporter progress)
                      throws SQLException,
                             IOException
Import data to a BASE database. The database must exists, but not contain any tables.

Parameters:
path - The path to the directory with data files
progress - A progress reporter
Throws:
SQLException
IOException

getResult

public Object getResult()
Description copied from interface: JdbcWork
Get the result of the work.

Specified by:
getResult in interface JdbcWork<Object>

execute

public void execute(Connection connection)
             throws SQLException
Specified by:
execute in interface Work
Throws:
SQLException

setDropPrimaryKeyBeforeImport

public void setDropPrimaryKeyBeforeImport(boolean drop)
Should we drop the primary key before importing data? This can speed up the import. The primary key will be re-created after the import is complete.

Parameters:
drop - TRUE to drop (default), FALSE to leave it

setDropConstraintsBeforeImport

public void setDropConstraintsBeforeImport(boolean drop)
Should we drop the unique constraints and indexes before importing data? This can speed up the import. The constraints will be re-created after the import is complete.

Parameters:
drop - TRUE to drop (default), FALSE to leave it

setAnalyzeAfterImport

public void setAnalyzeAfterImport(boolean analyze)
Should we execute an analyis command after the import has been completed. This may result in improved performance when re-creating indexes and foreign keys. The default is whatever the current DbEngine.analyzeAfterBatchOperation() reports.

Parameters:
analyze - TRUE to analyze, FALSE to not

setExportCompressed

public void setExportCompressed(boolean compress)
Should the exporter compress the data files or leave them as is? Compressing can considerably reduce the disk space needed for migration. The import will automatically detect if the files are compressed or not.

Parameters:
compress - TRUE to compress the files, FALSE to not compress (default)

setFetchSize

public void setFetchSize(int fetchSize)
Set the JDBC fetch size to use when fetching rows from the database. A higher value may improve performance but uses more memory. The default value is 20000.

Parameters:
fetchSize - The fetch size to use
See Also:
Statement.setFetchSize(int)

doExport

private void doExport(Connection connection)
               throws SQLException,
                      IOException
Perform the export. In principle we support all databases that BASE supports, but it may depend on the actual underlying SQL column types. The export generates files that can be imported into PostgreSQL only.

Throws:
SQLException
IOException

doImport

private void doImport(Connection connection)
               throws SQLException,
                      IOException
Perform the import. Importing is supported on PostgreSQL only.

Throws:
SQLException
IOException

numRows

private String numRows(long numRows)

getDataFile

private File getDataFile(TableInfo table)
Get the data file for the given table. Automatically detect if a compressed file is present or not.


getDataFile

private File getDataFile(TableInfo table,
                         boolean compress)
Get the data file for the given table.


getColumnsFile

private File getColumnsFile(TableInfo table)
Get the columns file for the given table.


getOutputStream

private OutputStream getOutputStream(File file)
                              throws IOException
Get a stream for writing to the given file. If the file name ends with .gz it is automatically wrapped with a GZIPOutputStream.

Throws:
IOException

getInputStream

private InputStream getInputStream(File file)
                            throws IOException
Get a stream for reading from the given file. If the file name ends with .gz it is automatically wrapped with a GZIPInputStream.

Throws:
IOException

importFromFile

private Integer importFromFile(TableInfo table,
                               File dataFile,
                               File columnsFile,
                               DbEngine engine,
                               Statement st,
                               ProgressReporter progress)
                        throws SQLException,
                               IOException
Import data to the given table.

Returns:
The max id if 'id' column exists, null otherwise
Throws:
SQLException
IOException

3.2.1: 2012-12-13