Package net.sf.basedb.core
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. TheexportAll(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. TheimportAll(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) $
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) class
Migration.ProgressInputStream
-
Field Summary
Fields Modifier and Type Field Description 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
Constructors Modifier Constructor Description private
Migration(boolean export, File directory, Session session, ProgressReporter progress)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description 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.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.
-
-
-
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 directoryprogress
- 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 filesprogress
- A progress reporter- Throws:
SQLException
IOException
-
execute
public void execute(Connection connection) throws SQLException
- Specified by:
execute
in interfaceWork
- 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 currentDbEngine.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
-
-