Skip to content

Commit

Permalink
History tables: no history tables for table names longer than 63 chars
Browse files Browse the repository at this point in the history
This makes sure errors due to Postgres clipping identifier names after
63 characters are found early. This shouldn't matter for us in practice,
but ideally we wouldn't include the target table name in generated
names (e.g. the update function, the history table and the time table).
  • Loading branch information
tomka committed Aug 3, 2016
1 parent d609ef6 commit f221e3a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,14 @@
-- History tables will be named like the live table plus a '_history' suffix
history_table_name = history_table_name(live_table_name);
-- Make sure the history table name is not longer than 63 characters, a
-- limit that Postgres defaults to and which causes identifier names to
-- become shortened silently.
IF (LENGTH(get_history_update_fn_name_regular(live_table_name)) > 63) THEN
RAISE EXCEPTION 'Can''t create history table with name longer than '
'63 characters: %', history_table_name;
END IF;
-- Don't do anything if there is already a history table registered with this name.
IF EXISTS(SELECT 1 FROM catmaid_history_table cht
WHERE cht.history_table_name = outerblock.history_table_name) THEN
Expand Down
18 changes: 17 additions & 1 deletion django/applications/catmaid/tests/test_history_tables.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import time

from django.db import connection, transaction
from django.db import connection, transaction, InternalError
from django.test import TestCase, TransactionTestCase
from guardian.shortcuts import assign_perm
from catmaid import history
Expand Down Expand Up @@ -28,6 +28,22 @@ def run(self, *args):
history.enable_history_tracking()
return super(HistoryTableTests, self).run(*args)

def test_name_length_limits(self):
""" Make sure an exception is raised if the history table name for a live
table exceeds identifier limits imposed by Postgres.
"""
cursor = connection.cursor()
with self.assertRaises(InternalError):
cursor.execute("""
CREATE TABLE
a_very_very_long_table_name_which_is_pretty_close_to_63_chars (
test text
);
SELECT create_history_table(
'a_very_very_long_table_name_which_is_pretty_close_to_63_chars'::regclass)
""")


def test_history_table_existence(self):
"""Test if all catmaid tables have a history table"""
expected_tables_with_history = (
Expand Down

0 comments on commit f221e3a

Please sign in to comment.