Class Migration

java.lang.Object
net.sf.basedb.core.Migration
All Implemented Interfaces:
Work

public final class Migration
extends Object
implements Work
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: 2021-01-20 13:18:30 +0100 (Wed, 20 Jan 2021) $
  • Field Details

    • 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 Details

  • Method Details

    • 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
    • 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