Skip to content

Commit

Permalink
Merge pull request #1 from Naturhistoriska/develop
Browse files Browse the repository at this point in the history
Add command-line option "--table-type"
  • Loading branch information
jmenglund committed Sep 2, 2019
2 parents 358542d + f952e39 commit fd139ee
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
20 changes: 20 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Change Log
==========

All notable changes to this project will be documented in this file.
This project adheres to `Semantic Versioning <http://semver.org/>`_.


v0.2.0 - 2019-09-02
-------------------

* Added option `--table-type` to select a specific table type to export.
Available options are: "BASE TABLE", "VIEW" and "SYSTEM VIEW".

`View commits <https://github.com/naturhistoriska/extract_mysql_tables.py/compare/v0.1.0...v0.2.0>`_


v0.1.0 - 2019-03-11
-------------------

Initial release.
12 changes: 8 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ Usage
-----

.. code-block::
./extract_mysql_tables.py --help
usage: extract_mysql_tables.py [-h] [-V] [-u USER] [-p PASSWORD] [--host HOST]
[-o DIR]
[--table-type {1,2,3}] [-o DIR]
database [table-file]
Command-line utility for exporting tables from a MySQL database to files in
Expand All @@ -63,6 +63,10 @@ Usage
-p PASSWORD, --password PASSWORD
MySQL password
--host HOST database host (default: "localhost")
--table-type {1,2,3} Table type to include in export: 1=BASE TABLE; 2=VIEW;
3=SYSTEM VIEW (i.e. INFORMATION_SCHEMA table). The
table type will be ignored if there is a file provided
with table names.
-o DIR, --output-dir DIR
path to the output directory (default: current
directory)
Expand All @@ -88,7 +92,7 @@ License
`MIT license <https://opensource.org/licenses/MIT>`_.


Author
------
Author and maintainer
---------------------

Markus Englund, [email protected]
34 changes: 28 additions & 6 deletions extract_mysql_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

__author__ = 'Markus Englund'
__license__ = 'MIT'
__version__ = '0.1.0'
__version__ = '0.2.0'


def main(args=None):
Expand All @@ -37,7 +37,7 @@ def main(args=None):

try:
if parser.tablefile_filepath is None:
table_list = list_all_tables(mysql_connection)
table_list = list_all_tables(mysql_connection, parser.table_type)
else:
table_list = read_file_into_list(parser.tablefile_filepath)

Expand Down Expand Up @@ -67,6 +67,12 @@ def parse_args(args):
'--host', type=str, action='store', default='localhost',
dest='host', help='database host (default: "localhost")')
parser.add_argument('database', action='store', help='database name')
parser.add_argument(
'--table-type', type=int, action=StoreTableTypeName,
choices=range(1, 4), default=None, help=(
'Table type to include in export: 1=BASE TABLE; 2=VIEW; '
'3=SYSTEM VIEW (i.e. INFORMATION_SCHEMA table). The table type '
'will be ignored if there is a file provided with table names.'))
parser.add_argument(
'-o', '--output-dir', dest='output_dirpath', type=is_directory,
action=StoreExpandedPath, default=os.getcwd(), metavar='DIR',
Expand All @@ -88,8 +94,18 @@ def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, filepath)


class StoreTableTypeName(argparse.Action):
"""Store the table type name based on a given integer."""

def __call__(self, parser, namespace, values, option_string=None):
if values:
d = dict(zip(range(1, 4), ['BASE TABLE', 'VIEW', 'SYSTEM VIEW']))
table_type = d[values]
setattr(namespace, self.dest, table_type)


def table_to_tsv(conn, table_name, output_filepath):
"""Export table to a TSV file named after the table."""
"""Export table to a TSV file."""
cursor = conn.cursor()
cursor.execute('select * from ' + table_name + ';')
with open(output_filepath, 'w', newline='') as csv_file:
Expand All @@ -116,11 +132,17 @@ def is_file(filename):
return filename


def list_all_tables(conn):
def list_all_tables(conn, table_type=None):
"""Return a list with names of all tables in the database."""
if table_type is not None:
sql_query = (
"show full tables where TABLE_TYPE = '{}';"
.format(table_type))
else:
sql_query = 'show full tables;'
cursor = conn.cursor()
cursor.execute('show tables;')
return [name for (name,) in cursor]
cursor.execute(sql_query)
return [name for (name, _) in cursor]


def read_file_into_list(filepath):
Expand Down

0 comments on commit fd139ee

Please sign in to comment.