|
| 1 | +""" |
| 2 | +Dropping a table must not drop columns of other tables. |
| 3 | +
|
| 4 | +On PostgreSQL, an enum attribute is declared with an enum type named by its |
| 5 | +values, so every column in the schema with the same values shares one type. |
| 6 | +Dropping a table drops its enum types only when no other column uses them. |
| 7 | +""" |
| 8 | + |
| 9 | +import time |
| 10 | + |
| 11 | +import pytest |
| 12 | + |
| 13 | +import datajoint as dj |
| 14 | + |
| 15 | + |
| 16 | +@pytest.fixture(scope="function") |
| 17 | +def schema_by_backend(connection_by_backend, db_creds_by_backend): |
| 18 | + backend = db_creds_by_backend["backend"] |
| 19 | + schema_name = f"djtest_shared_enum_{backend}_{str(int(time.time() * 1000))[-8:]}" |
| 20 | + schema = dj.Schema(schema_name, connection=connection_by_backend) |
| 21 | + yield schema |
| 22 | + if connection_by_backend.is_connected: |
| 23 | + connection_by_backend.query( |
| 24 | + f"DROP DATABASE IF EXISTS {connection_by_backend.adapter.quote_identifier(schema_name)}" |
| 25 | + if backend == "mysql" |
| 26 | + else f"DROP SCHEMA IF EXISTS {connection_by_backend.adapter.quote_identifier(schema_name)} CASCADE" |
| 27 | + ) |
| 28 | + |
| 29 | + |
| 30 | +def _columns(schema, table_name): |
| 31 | + rows = schema.connection.query( |
| 32 | + "SELECT column_name FROM information_schema.columns " |
| 33 | + "WHERE table_schema = %s AND table_name = %s ORDER BY ordinal_position", |
| 34 | + args=(schema.database, table_name), |
| 35 | + ).fetchall() |
| 36 | + return [row[0] for row in rows] |
| 37 | + |
| 38 | + |
| 39 | +def _enum_types(schema): |
| 40 | + if schema.connection.adapter.backend != "postgresql": |
| 41 | + return [] |
| 42 | + rows = schema.connection.query( |
| 43 | + "SELECT t.typname FROM pg_catalog.pg_type t " |
| 44 | + "JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace " |
| 45 | + "WHERE t.typtype = 'e' AND n.nspname = %s", |
| 46 | + args=(schema.database,), |
| 47 | + ).fetchall() |
| 48 | + return [row[0] for row in rows] |
| 49 | + |
| 50 | + |
| 51 | +def test_drop_keeps_a_shared_enum_column(schema_by_backend): |
| 52 | + """Two tables declare the same enum. Dropping one keeps the other's column |
| 53 | + and its values; dropping the second removes the type.""" |
| 54 | + |
| 55 | + @schema_by_backend |
| 56 | + class Animal(dj.Manual): |
| 57 | + definition = """ |
| 58 | + animal_id : int32 |
| 59 | + --- |
| 60 | + species : enum('mouse', 'rat') |
| 61 | + """ |
| 62 | + |
| 63 | + @schema_by_backend |
| 64 | + class Cage(dj.Manual): |
| 65 | + definition = """ |
| 66 | + cage_id : int32 |
| 67 | + --- |
| 68 | + species : enum('mouse', 'rat') |
| 69 | + """ |
| 70 | + |
| 71 | + Animal.insert([(1, "mouse"), (2, "rat")]) |
| 72 | + Cage.insert([(1, "rat")]) |
| 73 | + types_before = _enum_types(schema_by_backend) |
| 74 | + |
| 75 | + Cage().drop_quick() |
| 76 | + assert _columns(schema_by_backend, "animal") == ["animal_id", "species"] |
| 77 | + assert Animal.to_dicts(order_by="animal_id") == [ |
| 78 | + {"animal_id": 1, "species": "mouse"}, |
| 79 | + {"animal_id": 2, "species": "rat"}, |
| 80 | + ] |
| 81 | + assert _enum_types(schema_by_backend) == types_before |
| 82 | + |
| 83 | + Animal().drop_quick() |
| 84 | + assert _enum_types(schema_by_backend) == [] |
| 85 | + |
| 86 | + |
| 87 | +def test_dropping_a_jobs_table_keeps_other_jobs_tables_whole(schema_by_backend): |
| 88 | + """Every jobs table's status column has the same enum. Dropping one jobs |
| 89 | + table leaves the others' status columns, and their jobs, in place.""" |
| 90 | + |
| 91 | + @schema_by_backend |
| 92 | + class Item(dj.Manual): |
| 93 | + definition = """ |
| 94 | + item : int32 |
| 95 | + """ |
| 96 | + |
| 97 | + @schema_by_backend |
| 98 | + class First(dj.Computed): |
| 99 | + definition = """ |
| 100 | + -> Item |
| 101 | + """ |
| 102 | + |
| 103 | + def make(self, key): |
| 104 | + self.insert1(key) |
| 105 | + |
| 106 | + @schema_by_backend |
| 107 | + class Second(dj.Computed): |
| 108 | + definition = """ |
| 109 | + -> Item |
| 110 | + """ |
| 111 | + |
| 112 | + def make(self, key): |
| 113 | + raise RuntimeError("not ready") |
| 114 | + |
| 115 | + Item.insert([(1,), (2,)]) |
| 116 | + First.populate(reserve_jobs=True) |
| 117 | + Second.populate(reserve_jobs=True, suppress_errors=True) |
| 118 | + |
| 119 | + First.jobs.drop_quick() |
| 120 | + assert "status" in _columns(schema_by_backend, "~~second") |
| 121 | + assert sorted(Second.jobs.to_arrays("status").tolist()) == ["error", "error"] |
0 commit comments