3
0
Fork 0

feat: add support for Create Alembic Migration

just a wrapper around `alembic revision` but it seems useful...
This commit is contained in:
Lance Edgar 2025-12-24 00:15:37 -06:00
parent 58bf8b4bbb
commit b1ebe1095e
6 changed files with 497 additions and 30 deletions

View file

@ -1,5 +1,6 @@
# -*- coding: utf-8; -*-
import os
from unittest.mock import patch
import sqlalchemy as sa
@ -12,9 +13,11 @@ from wuttjamaican.db.conf import (
make_alembic_config,
)
import colander
from pyramid.httpexceptions import HTTPNotFound, HTTPFound
from wuttaweb.views import alembic as mod
from wuttaweb.forms import Form
from wuttaweb.testing import WebTestCase
from wuttaweb.forms.widgets import AlembicRevisionWidget
@ -133,6 +136,166 @@ class TestAlembicMigrationView(WebTestCase):
form = view.make_model_form(rev)
self.assertIsInstance(form.widgets["down_revision"], AlembicRevisionWidget)
def test_make_create_form(self):
self.pyramid_config.add_route("alembic.migrations", "/alembic/migrations/")
view = self.make_view()
# sanity / coverage
form = view.make_create_form()
self.assertIsInstance(form, Form)
self.assertIn("branching_option", form)
def test_validate_revise_branch(self):
self.pyramid_config.add_route("alembic.migrations", "/alembic/migrations/")
view = self.make_view()
form = view.make_create_form()
schema = form.get_schema()
# good example
self.assertIsNone(
view.validate_revise_branch(
schema,
{
"branching_option": "revise",
"revise_branch": "wutta",
},
)
)
# branch is required
self.assertRaises(
colander.Invalid,
view.validate_revise_branch,
schema,
{
"branching_option": "revise",
"revise_branch": None,
},
)
def test_validate_new_branch(self):
self.pyramid_config.add_route("alembic.migrations", "/alembic/migrations/")
view = self.make_view()
form = view.make_create_form()
schema = form.get_schema()
# good example
self.assertIsNone(
view.validate_revise_branch(
schema,
{
"branching_option": "new",
"new_branch": "poser",
"version_location": "wuttjamaican.db:alembic/versions",
},
)
)
# name is required
self.assertRaises(
colander.Invalid,
view.validate_new_branch,
schema,
{
"branching_option": "new",
"new_branch": None,
"version_location": "wuttjamaican.db:alembic/versions",
},
)
# version_location is required
self.assertRaises(
colander.Invalid,
view.validate_new_branch,
schema,
{
"branching_option": "new",
"new_branch": "poser",
"version_location": None,
},
)
def test_save_create_form(self):
self.pyramid_config.add_route("alembic.migrations", "/alembic/migrations/")
self.pyramid_config.add_route("alembic.dashboard", "/alembic/dashboard")
view = self.make_view()
form = view.make_create_form()
# revise branch
form.validated = {
"description": "test revision",
"autogenerate": False,
"branching_option": "revise",
"revise_branch": "wutta",
}
revision = view.save_create_form(form)
# file was saved in wutta dir
self.assertTrue(
revision.path.startswith(
self.app.resource_path("wuttjamaican.db:alembic/versions")
)
)
# get rid of that file!
os.remove(revision.path)
# new branch
form.validated = {
"description": "test revision",
"autogenerate": False,
"branching_option": "new",
"new_branch": "wuttatest",
"version_location": "wuttjamaican.db:alembic/versions",
}
revision = view.save_create_form(form)
# file was saved in wutta dir
self.assertTrue(
revision.path.startswith(
self.app.resource_path("wuttjamaican.db:alembic/versions")
)
)
# get rid of that file!
os.remove(revision.path)
def test_save_delete_form(self):
self.pyramid_config.add_route(
"alembic.migrations.view", "/alembic/migrations/{revision}"
)
view = self.make_view()
alembic = make_alembic_config(self.config)
# write new empty migration script
revision = alembic_command.revision(
alembic,
head="base",
branch_label="wuttatest",
version_path=self.app.resource_path("wuttjamaican.db:alembic/versions"),
message="test revision",
)
# script exists
self.assertTrue(os.path.exists(revision.path))
with patch.object(
self.request, "matchdict", new={"revision": revision.revision}
):
form = view.make_delete_form(revision)
view.save_delete_form(form)
# script gone
self.assertFalse(os.path.exists(revision.path))
def test_configure(self):
self.pyramid_config.add_route("home", "/")
self.pyramid_config.add_route("login", "/auth/login")
self.pyramid_config.add_route("alembic.migrations", "/alembic/migrations")
view = self.make_view()
# sanity/coverage
view.configure()
class TestAlembicDashboardView(WebTestCase):
@ -294,4 +457,4 @@ version_locations = wuttjamaican.db:alembic/versions
self.assertFalse(self.request.session.peek_flash())
self.assertTrue(self.request.session.peek_flash("error"))
[msg] = self.request.session.pop_flash("error")
self.assertTrue(msg.startswith("Database failed to migrate: "))
self.assertTrue(msg.startswith("Migrate failed: "))