Skip to content

Commit 7d3d284

Browse files
authored
command
* add console script * update README
1 parent df871e7 commit 7d3d284

File tree

5 files changed

+71
-6
lines changed

5 files changed

+71
-6
lines changed

README.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22

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

5-
to use it:
5+
to use it, as a Python library:
66

77
```python
88
from cbl_migrator import DbMigrator
99

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

13-
migrator = DbMigrator(origin, dest, ['excluded_table1', 'excluded_table2'])
13+
migrator = DbMigrator(origin, dest, ['excluded_table1', 'excluded_table2'], n_workers=4)
1414
migrator.migrate()
1515
```
1616

17+
directly from the command line:
18+
```bash
19+
cbl-migrator oracle://{user}:{pass}@{host}:{port}/{sid}?encoding=utf8 postgresql://{user}:{pass}@{host}:{port}/{dbname}?client_encoding=utf8 --n_workers 4
20+
```
21+
1722
## What it does (in order of events)
1823

1924
- Copies tables from origin to dest using the closest data type for each field. No constraints except PKs are initially copied across.

cbl_migrator/bin/__init__.py

Whitespace-only changes.

cbl_migrator/bin/run_migrator.py

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import argparse
2+
from cbl_migrator import DbMigrator
3+
from multiprocessing import cpu_count
4+
import sys
5+
6+
7+
def run(origin, dest, n_workers, copy_schema, copy_data, copy_constraints, copy_indexes, chunk_size):
8+
migrator = DbMigrator(origin, dest, n_workers=int(n_workers))
9+
migrator.migrate(copy_schema=copy_schema, copy_data=copy_data,
10+
copy_constraints=copy_constraints, copy_indexes=copy_indexes, chunk_size=int(chunk_size))
11+
print('Migration finished, check cbl_migrator.log file for extra information')
12+
13+
14+
def main(args=None):
15+
if args is None:
16+
args = sys.argv[1:]
17+
18+
parser = argparse.ArgumentParser(
19+
description='Migrate an Oracle database to a PosgreSQL, MySQL or SQLite server')
20+
parser.add_argument('origin',
21+
help='Origin database connection string',
22+
default=None)
23+
parser.add_argument('dest',
24+
help='Destination database connection string',
25+
default=None)
26+
27+
parser.add_argument('--n_workers',
28+
help='Number of workers migrating tables in parallel',
29+
default=cpu_count())
30+
31+
parser.add_argument('--copy_schema',
32+
help='Copy database schema',
33+
default=True)
34+
35+
parser.add_argument('--copy_data',
36+
help='Copy data',
37+
default=True)
38+
39+
parser.add_argument('--copy_constraints',
40+
help='Copy constraints',
41+
default=True)
42+
43+
parser.add_argument('--copy_indexes',
44+
help='Copy indexes',
45+
default=True)
46+
47+
parser.add_argument('--chunk_size',
48+
help='Number of rows copied at the same time',
49+
default=1000)
50+
51+
args = parser.parse_args()
52+
run(args.origin, args.dest, args.n_workers, args.copy_schema,
53+
args.copy_data, args.copy_constraints, args.copy_indexes, args.chunk_size)
54+
55+
56+
if __name__ == '__main__':
57+
main()

cbl_migrator/logs.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import logging
22

3-
logger = logging.getLogger("db_migrator")
3+
logger = logging.getLogger("cbl_migrator")
44
logger.setLevel(logging.DEBUG)
55
# create file handler which logs even debug messages
6-
fh = logging.FileHandler("db_migrator.log")
6+
fh = logging.FileHandler("cbl_migrator.log")
77
fh.setLevel(logging.DEBUG)
88
# create console handler with a higher log level
99
ch = logging.StreamHandler()
10-
ch.setLevel(logging.ERROR)
10+
ch.setLevel(logging.WARNING)
1111
# create formatter and add it to the handlers
1212
formatter = logging.Formatter("%(asctime)s - %(levelname)s - %(message)s")
1313
ch.setFormatter(formatter)

setup.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,14 @@
99
description='Migrates Oracle dbs to PostgreSQL, MySQL and Sqlite',
1010
url='https://github.com/chembl/cbl_migrator',
1111
license='MIT',
12-
packages=['cbl_migrator'],
12+
packages=['cbl_migrator', 'cbl_migrator.bin'],
1313
long_description=open('README.md', encoding='utf-8').read(),
1414
long_description_content_type='text/markdown',
1515
install_requires=['SQLAlchemy>=1.3'],
1616
tests_require=['exrex'],
17+
entry_points = {
18+
'console_scripts': ['cbl-migrator=cbl_migrator.bin.run_migrator:main'],
19+
},
1720
classifiers=['Development Status :: 2 - Pre-Alpha',
1821
'Intended Audience :: Developers',
1922
'License :: OSI Approved :: MIT License',

0 commit comments

Comments
 (0)