diff options
author | Frederick Muriuki Muriithi | 2022-11-09 11:55:12 +0300 |
---|---|---|
committer | Frederick Muriuki Muriithi | 2022-11-09 11:57:49 +0300 |
commit | d80bff6898019ff8793044a3c9e1fb824b763800 (patch) | |
tree | f6d11da711ac8750903987f6fb1ce5bce2c0bb8d | |
parent | b8d33374ba4686fcadeed9f6c60f937f2eb19a89 (diff) | |
download | genenetwork3-d80bff6898019ff8793044a3c9e1fb824b763800.tar.gz |
Migrations: Add `resource_meta` field to `resource_categories` table
2 files changed, 94 insertions, 0 deletions
diff --git a/migrations/auth/20221109_01_HbD5F-add-resource-meta-field-to-resource-categories-field.py b/migrations/auth/20221109_01_HbD5F-add-resource-meta-field-to-resource-categories-field.py new file mode 100644 index 0000000..6c829b1 --- /dev/null +++ b/migrations/auth/20221109_01_HbD5F-add-resource-meta-field-to-resource-categories-field.py @@ -0,0 +1,17 @@ +""" +Add 'resource_meta' field to 'resource_categories' field. +""" + +from yoyo import step + +__depends__ = {'20221108_04_CKcSL-init-data-in-resource-categories-table'} + +steps = [ + step( + """ + ALTER TABLE resource_categories + ADD COLUMN + resource_meta TEXT NOT NULL DEFAULT '[]' + """, + "ALTER TABLE resource_categories DROP COLUMN resource_meta") +] diff --git a/tests/unit/auth/test_migration_add_resource_meta_field_to_resources_categories_table.py b/tests/unit/auth/test_migration_add_resource_meta_field_to_resources_categories_table.py new file mode 100644 index 0000000..43ee88f --- /dev/null +++ b/tests/unit/auth/test_migration_add_resource_meta_field_to_resources_categories_table.py @@ -0,0 +1,77 @@ +"""Test migration adding the `resource_meta` field to `resource_categories` table.""" +from contextlib import closing + +import pytest +import sqlite3 + +from gn3.migrations import get_migration, apply_migrations, rollback_migrations +from tests.unit.auth.conftest import ( + migrations_up_to, apply_single_migration, rollback_single_migration) + +migration_path = "migrations/auth/20221109_01_HbD5F-add-resource-meta-field-to-resource-categories-field.py" + +def __has_resource_meta(value: str): + return value.find("resource_meta TEXT") >= 0 + +@pytest.mark.unit_test +def test_apply_add_resource_meta(auth_migrations_dir, auth_testdb_path, backend): + older_migrations = migrations_up_to(migration_path, auth_migrations_dir) + the_migration = get_migration(migration_path) + apply_migrations(backend, older_migrations) + with closing(sqlite3.connect(auth_testdb_path)) as conn, closing(conn.cursor()) as cursor: + cursor.execute( + "SELECT sql FROM sqlite_schema WHERE name=?", + ("resource_categories",)) + results_before_migration = cursor.fetchone() + apply_single_migration(backend, the_migration) + cursor.execute( + "SELECT sql FROM sqlite_schema WHERE name=?", + ("resource_categories",)) + results_after_migration = cursor.fetchone() + + rollback_migrations(backend, older_migrations + [the_migration]) + + assert not any( + [__has_resource_meta(line.strip()) + for line in results_before_migration[0].split("\n")] + ), "`resource_meta` field exists in table before migration." + assert any( + [__has_resource_meta(line.strip()) + for line in results_after_migration[0].split("\n")] + ), "Couldn't find `resource_meta` field in table" + +@pytest.mark.unit_test +def test_rollback_add_resource_meta(auth_migrations_dir, auth_testdb_path, backend): + older_migrations = migrations_up_to(migration_path, auth_migrations_dir) + the_migration = get_migration(migration_path) + apply_migrations(backend, older_migrations) + with closing(sqlite3.connect(auth_testdb_path)) as conn, closing(conn.cursor()) as cursor: + cursor.execute( + "SELECT sql FROM sqlite_schema WHERE name=?", + ("resource_categories",)) + results_before_migration = cursor.fetchone() + apply_single_migration(backend, the_migration) + cursor.execute( + "SELECT sql FROM sqlite_schema WHERE name=?", + ("resource_categories",)) + results_after_migration = cursor.fetchone() + rollback_single_migration(backend, the_migration) + cursor.execute( + "SELECT sql FROM sqlite_schema WHERE name=?", + ("resource_categories",)) + results_after_rollback = cursor.fetchone() + + rollback_migrations(backend, older_migrations) + + assert not any ([ + __has_resource_meta(line.strip()) + for line in results_before_migration[0].split("\n")] + ), "`resource_meta` field exists in table before migration." + assert any([ + __has_resource_meta(line.strip()) + for line in results_after_migration[0].split("\n")] + ), "Couldn't find `resource_meta` field in table" + assert not any([ + __has_resource_meta(line.strip()) + for line in results_after_rollback[0].split("\n")] + ), "`resource_meta` field exists in table after rollback." |