Package net.sf.basedb.core
Class Migration
java.lang.Object
net.sf.basedb.core.Migration
- All Implemented Interfaces:
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) $
-
Nested Class Summary
-
Field Summary
Modifier and TypeFieldDescriptionprivate boolean
private final File
private boolean
private boolean
private final boolean
private boolean
private int
private final DecimalFormat
private final ProgressReporter
private final Session
-
Constructor Summary
ModifierConstructorDescriptionprivate
Migration
(boolean export, File directory, Session session, ProgressReporter progress) -
Method Summary
Modifier and TypeMethodDescriptionprivate 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 Details
-
export
private final boolean export -
directory
-
session
-
progress
-
numRowsFormat
-
exportCompressed
private boolean exportCompressed -
fetchSize
private int fetchSize -
dropPrimaryKeyBeforeImport
private boolean dropPrimaryKeyBeforeImport -
dropConstraintsBeforeImport
private boolean dropConstraintsBeforeImport -
analyzeAfterImport
private boolean analyzeAfterImport
-
-
Constructor Details
-
Migration
-
-
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 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
- 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:
-
doExport
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
Perform the import. Importing is supported on PostgreSQL only.- Throws:
SQLException
IOException
-
numRows
-
getDataFile
Get the data file for the given table. Automatically detect if a compressed file is present or not. -
getDataFile
Get the data file for the given table. -
getColumnsFile
Get the columns file for the given table. -
getOutputStream
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
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
-