diff --git a/CHANGELOG.rst b/CHANGELOG.rst
new file mode 100644
index 0000000..f10c817
--- /dev/null
+++ b/CHANGELOG.rst
@@ -0,0 +1,20 @@
+Change Log
+==========
+
+All notable changes to this project will be documented in this file.
+This project adheres to `Semantic Versioning `_.
+
+
+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 `_
+
+
+v0.1.0 - 2019-03-11
+-------------------
+
+Initial release.
diff --git a/README.rst b/README.rst
index 03d0555..4a28bfa 100644
--- a/README.rst
+++ b/README.rst
@@ -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
@@ -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)
@@ -88,7 +92,7 @@ License
`MIT license `_.
-Author
-------
+Author and maintainer
+---------------------
Markus Englund, markus.englund@nrm.se
diff --git a/extract_mysql_tables.py b/extract_mysql_tables.py
index 95bbf26..9f92055 100755
--- a/extract_mysql_tables.py
+++ b/extract_mysql_tables.py
@@ -18,7 +18,7 @@
__author__ = 'Markus Englund'
__license__ = 'MIT'
-__version__ = '0.1.0'
+__version__ = '0.2.0'
def main(args=None):
@@ -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)
@@ -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',
@@ -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:
@@ -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):