Skip to content

Commit

Permalink
command
Browse files Browse the repository at this point in the history
* add console script

* update README
  • Loading branch information
eloyfelix authored Jan 31, 2020
1 parent df871e7 commit 7d3d284
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 6 deletions.
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,23 @@

Small SQLAlchemy based library that migrates Oracle DBs to MySQL, PostgreSQL and SQLite. Used in ChEMBL dumps generation process.

to use it:
to use it, as a Python library:

```python
from cbl_migrator import DbMigrator

origin = 'oracle://{user}:{pass}@{host}:{port}/{sid}?encoding=utf8'
dest = 'postgresql://{user}:{pass}@{host}:{port}/{dbname}?client_encoding=utf8'

migrator = DbMigrator(origin, dest, ['excluded_table1', 'excluded_table2'])
migrator = DbMigrator(origin, dest, ['excluded_table1', 'excluded_table2'], n_workers=4)
migrator.migrate()
```

directly from the command line:
```bash
cbl-migrator oracle://{user}:{pass}@{host}:{port}/{sid}?encoding=utf8 postgresql://{user}:{pass}@{host}:{port}/{dbname}?client_encoding=utf8 --n_workers 4
```

## What it does (in order of events)

- Copies tables from origin to dest using the closest data type for each field. No constraints except PKs are initially copied across.
Expand Down
Empty file added cbl_migrator/bin/__init__.py
Empty file.
57 changes: 57 additions & 0 deletions cbl_migrator/bin/run_migrator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import argparse
from cbl_migrator import DbMigrator
from multiprocessing import cpu_count
import sys


def run(origin, dest, n_workers, copy_schema, copy_data, copy_constraints, copy_indexes, chunk_size):
migrator = DbMigrator(origin, dest, n_workers=int(n_workers))
migrator.migrate(copy_schema=copy_schema, copy_data=copy_data,
copy_constraints=copy_constraints, copy_indexes=copy_indexes, chunk_size=int(chunk_size))
print('Migration finished, check cbl_migrator.log file for extra information')


def main(args=None):
if args is None:
args = sys.argv[1:]

parser = argparse.ArgumentParser(
description='Migrate an Oracle database to a PosgreSQL, MySQL or SQLite server')
parser.add_argument('origin',
help='Origin database connection string',
default=None)
parser.add_argument('dest',
help='Destination database connection string',
default=None)

parser.add_argument('--n_workers',
help='Number of workers migrating tables in parallel',
default=cpu_count())

parser.add_argument('--copy_schema',
help='Copy database schema',
default=True)

parser.add_argument('--copy_data',
help='Copy data',
default=True)

parser.add_argument('--copy_constraints',
help='Copy constraints',
default=True)

parser.add_argument('--copy_indexes',
help='Copy indexes',
default=True)

parser.add_argument('--chunk_size',
help='Number of rows copied at the same time',
default=1000)

args = parser.parse_args()
run(args.origin, args.dest, args.n_workers, args.copy_schema,
args.copy_data, args.copy_constraints, args.copy_indexes, args.chunk_size)


if __name__ == '__main__':
main()
6 changes: 3 additions & 3 deletions cbl_migrator/logs.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import logging

logger = logging.getLogger("db_migrator")
logger = logging.getLogger("cbl_migrator")
logger.setLevel(logging.DEBUG)
# create file handler which logs even debug messages
fh = logging.FileHandler("db_migrator.log")
fh = logging.FileHandler("cbl_migrator.log")
fh.setLevel(logging.DEBUG)
# create console handler with a higher log level
ch = logging.StreamHandler()
ch.setLevel(logging.ERROR)
ch.setLevel(logging.WARNING)
# create formatter and add it to the handlers
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
ch.setFormatter(formatter)
Expand Down
5 changes: 4 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
description='Migrates Oracle dbs to PostgreSQL, MySQL and Sqlite',
url='https://github.com/chembl/cbl_migrator',
license='MIT',
packages=['cbl_migrator'],
packages=['cbl_migrator', 'cbl_migrator.bin'],
long_description=open('README.md', encoding='utf-8').read(),
long_description_content_type='text/markdown',
install_requires=['SQLAlchemy>=1.3'],
tests_require=['exrex'],
entry_points = {
'console_scripts': ['cbl-migrator=cbl_migrator.bin.run_migrator:main'],
},
classifiers=['Development Status :: 2 - Pre-Alpha',
'Intended Audience :: Developers',
'License :: OSI Approved :: MIT License',
Expand Down

0 comments on commit 7d3d284

Please sign in to comment.