Skip to content

Commit

Permalink
Merge pull request #5 from adamalton/fix-migration-id-from-filename
Browse files Browse the repository at this point in the history
Fix migration id from filename, add GitHub tests workflow
  • Loading branch information
adamalton authored Apr 2, 2024
2 parents 93b70fc + f0f84bb commit e62a1cd
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 10 deletions.
20 changes: 20 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: Test
on: [push]
env:
DJANGO_SETTINGS_MODULE: "testing.test_settings"
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Set up Python
# This is the version of the action for setting up Python, not the Python version.
uses: actions/setup-python@v5
with:
# Semantic version range syntax or exact version of a Python version
python-version: '3.11'
- name: Install
run: pip install -e .
- name: Run tests
run: >-
django-admin test massmigration
20 changes: 15 additions & 5 deletions massmigration/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ def load(self):
if os.path.isdir(migrations_path):
items = os.listdir(migrations_path)
for item in sorted(items):
if is_valid_migration_id(item.rstrip(".py")):
migration = load_migration(app_config, item)
migration_id = migration_id_from_filename(item)
if migration_id:
migration = load_migration(app_config, migration_id)
self._all.append(migration)
self._by_key[migration.key] = migration
self._loaded = True
Expand All @@ -49,7 +50,16 @@ def load(self):
store = MigrationsStore()


def migration_id_from_filename(filename):
if filename.endswith(".py"):
migration_id = re.sub(r"\.py$", "", filename)
if is_valid_migration_id(migration_id):
return migration_id
return None


def is_valid_migration_name(name):
""" Is the given name (supplied without leading number) a valid name for a migration? """
return bool(re.match(r"^[a-z0-9_]+$", name))


Expand All @@ -61,9 +71,9 @@ def load_migration(app_config, filename):
""" Return in instance of the migration class from the given filename from the given app_config.
"""
module_str = app_config.name
migration_name = filename.rstrip(".py")
class_path_str = f"{module_str}.{MIGRATIONS_FOLDER}.{migration_name}.Migration"
migration_id = migration_id_from_filename(filename)
class_path_str = f"{module_str}.{MIGRATIONS_FOLDER}.{migration_id}.Migration"
cls = import_string(class_path_str)
app_label = app_config.label
instance = cls(app_label, migration_name)
instance = cls(app_label, migration_id)
return instance
39 changes: 39 additions & 0 deletions massmigration/tests/test_loader.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Third party
from django.test import TestCase

# Mass Migration
from massmigration.loader import (
is_valid_migration_id,
is_valid_migration_name,
migration_id_from_filename,
)

class LoaderTestCase(TestCase):
""" Tests for the 'loader.py' module. """

def test_is_valid_migration_id(self):
""" Test the `is_valid_migration_id` function. """
for migration_id, expect_valid in [
("0023_valid_name", True),
("0023_dots_not_allowed.html", False),
("no_leading_zeros", False),
]:
self.assertEqual(is_valid_migration_id(migration_id), expect_valid)

def test_is_valid_migration_name(self):
""" Test the `is_valid_migration_name` function. """
for name, expect_valid in [
("valid_name_here", True),
("hyphens-not-allowed", False),
("dots.not.allowed", False),
]:
self.assertEqual(is_valid_migration_name(name), expect_valid)

def test_migration_id_from_filename(self):
""" Test the `migration_id_from_filename` function. """
for filename, expected_id in [
("0001_my_migration.py", "0001_my_migration"),
("0001_my_migration.html", None),
("__init__.py", None),
]:
self.assertEqual(migration_id_from_filename(filename), expected_id)
17 changes: 12 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
URL = "https://github.com/adamalton/django-mass-migration"
AUTHOR = "Adam Alton"

# TODO: possibly make the dependency on djangae optional
EXTRAS = {}

# This gets replaced by the publish.yml workflow
VERSION = "{{VERSION_PLACEHOLDER}}"
# If it's not replaced (e.g. when installing for tests) then we still need a valid version number
if "PLACEHOLDER" in VERSION:
VERSION = "99.9.9"

setup(
name=NAME,
version="{{VERSION_PLACEHOLDER}}",
version=f"{VERSION}",
packages=PACKAGES,
author=AUTHOR,
description=DESCRIPTION,
Expand All @@ -39,6 +42,10 @@
"djangae>=1.1.0",
"django-gcloud-connectors>=0.3.6",
],
extras_require=EXTRAS,
# tests_require=EXTRAS["test"],
# TODO: make Djangae dependencies optional
# extras_require={
# "djangae": [

# ],
# },
)
Empty file added testing/__init__.py
Empty file.

0 comments on commit e62a1cd

Please sign in to comment.