diff --git a/partitionmanager/cli.py b/partitionmanager/cli.py index 8a9707f..0bc627f 100644 --- a/partitionmanager/cli.py +++ b/partitionmanager/cli.py @@ -360,6 +360,8 @@ def do_partition(conf): except partitionmanager.types.DatabaseCommandException as e: log.warning("Failed to automatically handle %s: %s", table, e) metrics.add("alter_errors", table.name, 1) + except partitionmanager.types.TableEmptyException: + log.warning("Table %s appears to be empty. Skipping.", table) except (ValueError, Exception) as e: log.warning("Failed to handle %s: %s", table, e) metrics.add("alter_errors", table.name, 1) diff --git a/partitionmanager/database_helpers_test.py b/partitionmanager/database_helpers_test.py index 188752c..280ae17 100644 --- a/partitionmanager/database_helpers_test.py +++ b/partitionmanager/database_helpers_test.py @@ -9,6 +9,7 @@ SqlInput, SqlQuery, Table, + TableEmptyException, ) @@ -46,6 +47,16 @@ def test_position_of_table(self): pos = get_position_of_table(db, table, data) self.assertEqual(pos.as_list(), [90210]) + def test_empty_table(self): + db = MockDatabase() + db.add_response("SELECT id FROM `burgers` ORDER BY", []) + + table = Table("burgers") + data = {"range_cols": ["id"]} + + with self.assertRaises(TableEmptyException): + get_position_of_table(db, table, data) + def test_exact_timestamp_no_query(self): db = MockDatabase() db.add_response("SELECT id FROM `burgers` ORDER BY", [{"id": 42}]) diff --git a/partitionmanager/table_append_partition.py b/partitionmanager/table_append_partition.py index 49dffcb..7d2c7e8 100644 --- a/partitionmanager/table_append_partition.py +++ b/partitionmanager/table_append_partition.py @@ -61,7 +61,7 @@ def get_current_positions(database, table, columns): f"Expected one result from {table.name}" ) if not rows: - raise partitionmanager.types.TableInformationException( + raise partitionmanager.types.TableEmptyException( f"Table {table.name} appears to be empty. (No results)" ) positions[column] = rows[0][column] diff --git a/partitionmanager/types.py b/partitionmanager/types.py index db13101..dc4745c 100644 --- a/partitionmanager/types.py +++ b/partitionmanager/types.py @@ -611,6 +611,10 @@ class TableInformationException(Exception): """Raised when the table's status doesn't include the information we need.""" +class TableEmptyException(Exception): + """Raised when the table is empty.""" + + class NoEmptyPartitionsAvailableException(Exception): """Raised if no empty partitions are available to safely modify.""" diff --git a/pyproject.toml b/pyproject.toml index c6ea3b0..9c82cb7 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -116,4 +116,4 @@ max-complexity = 16 # default is 10 [tool.ruff.lint.pylint] max-args = 7 # default is 5 max-branches = 15 # default is 12 -max-statements = 52 # default is 50 +max-statements = 54 # default is 50