Skip to content

Commit

Permalink
Merge pull request #83 from macbre/single_column
Browse files Browse the repository at this point in the history
Report tables with just a single column
  • Loading branch information
macbre authored Dec 30, 2017
2 parents 5abf38b + 565590d commit f12f22d
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 4 deletions.
15 changes: 13 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ Analyses your database queries and schema and suggests indices improvements. You

`index-digest` does the following:

* it checks the schema of all tables in a given database and suggests improvements (e.g. removal of redundant indices, adding a primary key to ease replication)
* it checks the schema of all tables in a given database and suggests improvements (e.g. removal of redundant indices, adding a primary key to ease replication, dropping tables with just a single column)
* if provided with SQL queries log (via `--sql-log` option) it:
* checks if all tables, columns and indices are used by these queries
* reports text columns with character set different than `utf`
* reports queries that do not use indices
* reports queries that use filesort, temporary file or full table scan
* reports queries that are not quite kosher (e.g. `LIKE "%foo%"`)
* reports queries that are not quite kosher (e.g. `LIKE "%foo%"`, `INSERT IGNORE`)

This tool **supports MySQL 5.5, 5.6, 5.7, 8.0 and MariaDB 10.0, 10.2** and runs under **Python 2.7, 3.4, 3.5 and 3.6**.

Expand Down Expand Up @@ -133,6 +133,16 @@ test_tables → table affected: 0075_some_guy_test_table
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
------------------------------------------------------------
single_column → table affected: 0074_bag_of_ints
✗ "0074_bag_of_ints" has just a single column
- schema: CREATE TABLE `0074_bag_of_ints` (
`id` int(9) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
------------------------------------------------------------
not_used_indices → table affected: 0002_not_used_indices
Expand Down Expand Up @@ -252,6 +262,7 @@ Outputs YML file with results and metadata.
* `non_utf_columns`: reports text columns that have characters encoding set to `latin1` (utf is the way to go)
* `missing_primary_index`: reports tables with no primary or unique key (see [MySQL bug #76252](https://bugs.mysql.com/bug.php?id=76252) and [Wikia/app#9863](https://github.com/Wikia/app/pull/9863))
* `test_tables`: reports tables that seem to be test leftovers (e.g. `some_guy_test_table`)
* `single_column`: reports tables with just a single column

### Additional checks performed on SQL log

Expand Down
4 changes: 3 additions & 1 deletion indexdigest/cli/script.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@
check_selects_with_like, \
check_missing_primary_index, \
check_test_tables, \
check_insert_ignore_queries
check_insert_ignore_queries, \
check_single_column


def main():
Expand Down Expand Up @@ -80,6 +81,7 @@ def main():
check_latin_columns(database),
check_missing_primary_index(database),
check_test_tables(database),
check_single_column(database),
)

# checks that use SQL log
Expand Down
1 change: 1 addition & 0 deletions indexdigest/linters/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
from .linter_0032_utf_latin_columns import check_latin_columns
from .linter_0034_missing_primary_index import check_missing_primary_index
from .linter_0070_insert_ignore import check_insert_ignore_queries
from .linter_0074_single_column import check_single_column
from .linter_0075_test_tables import check_test_tables
22 changes: 22 additions & 0 deletions indexdigest/linters/linter_0074_single_column.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"""
This linter reports tables with just a single column
"""
from indexdigest.utils import LinterEntry


def check_single_column(database):
"""
:type database indexdigest.database.Database
:rtype: list[LinterEntry]
"""
tables = [
table
for table in database.get_tables()
if len(database.get_table_columns(table)) == 1
]

for table in tables:
yield LinterEntry(linter_type='single_column', table_name=table,
message='"{}" has just a single column'.
format(table),
context={'schema': database.get_table_schema(table)})
22 changes: 22 additions & 0 deletions indexdigest/test/linters/test_0074_single_column.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from __future__ import print_function

from unittest import TestCase

from indexdigest.linters import check_single_column
from indexdigest.test import DatabaseTestMixin


class TestSingleColumn(TestCase, DatabaseTestMixin):

def test_check_single_column(self):
reports = list(check_single_column(self.connection))

print(list(map(str, reports)))

self.assertEqual(len(reports), 1)

self.assertEqual(str(reports[0]),
'0074_bag_of_ints: "0074_bag_of_ints" has just a single column')
self.assertTrue('CREATE TABLE `0074_bag_of_ints` (' in reports[0].context['schema'])

# assert False
3 changes: 2 additions & 1 deletion sql/0006-not-used-columns-and-tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ INSERT INTO 0006_not_used_columns VALUES
DROP TABLE IF EXISTS `0006_not_used_tables`;
CREATE TABLE `0006_not_used_tables` (
`id` int(9) NOT NULL AUTO_INCREMENT,
`foo` varchar(16) NOT NULL DEFAULT '',
PRIMARY KEY (`id`)
);

INSERT INTO 0006_not_used_tables VALUES(1);
INSERT INTO 0006_not_used_tables VALUES(1, 'foo');
8 changes: 8 additions & 0 deletions sql/0074-single-column.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Report tables with just a single column
--
-- https://github.com/macbre/index-digest/issues/74
DROP TABLE IF EXISTS `0074_bag_of_ints`;
CREATE TABLE `0074_bag_of_ints` (
`id` int(9) NOT NULL AUTO_INCREMENT,
PRIMARY KEY (`id`)
) CHARSET=utf8;

0 comments on commit f12f22d

Please sign in to comment.