小编Mat*_*bin的帖子

使用常规方法和类方法实现 Python 协议

假设我有两个类Foo1并且Foo2实现了一个方法bar()

  • Foo1,bar()是一个正则方法
  • Foo2bar()是一个@classmethod
class Foo1:

    def bar(self) -> None:
        print("foo1.bar")


class Foo2:

    @classmethod
    def bar(cls) -> None:
        print("Foo2.bar")
Run Code Online (Sandbox Code Playgroud)

现在假设我有一个函数,它接受“任何有bar()方法的东西”列表并调用它:

def foreach_foo_call_bar(foos):
    for foo in foos:
        foo.bar()
Run Code Online (Sandbox Code Playgroud)

在运行时调用此函数工作正常:

foreach_foo_call_bar([Foo1(), Foo2])
Run Code Online (Sandbox Code Playgroud)

因为两者都有Foo1()并且Foo2有一个bar()方法。

但是,如何正确地将类型提示添加到foreach_foo_call_bar()?

我尝试创建一个名为的PEP544ProtocolSupportsBar

class SupportsBar(Protocol):

    def bar(self) -> None:
        pass
Run Code Online (Sandbox Code Playgroud)

并像这样注释:

def foreach_foo_call_bar(foos: Iterable[SupportsBar]):
   ...
Run Code Online (Sandbox Code Playgroud)

但是 mypy 说:

List item 1 has …
Run Code Online (Sandbox Code Playgroud)

python type-hinting python-3.x

22
推荐指数
3
解决办法
599
查看次数

使用 Flyway 和 Spring Boot 迁移具有不同生命周期的多个模式

迁飞路线常见问题分开3箱子多种模式的:

  1. 多个相同的模式
  2. 模式不同,但具有相同的生命周期
  3. 模式有一个独特的生命周期,或者必须是自主的和干净的分离

我们正在使用 Maven 构建一个多模块 Spring Boot 4.5.9 项目。每个模块都是完全独立的,并且有自己的数据库模式。所有模式都驻留在一个数据库中,因此只有一个 Spring 数据源。

因为模块是独立的,我们想分别管理它们各自的模式迁移,所以上面的选项(3)是最合适的。

但是,我找不到按照 Flyway FAQ 建议的方式配置 Spring Boot 的 Flyway 集成的方法:

使用多个 Flyway 实例。每个实例管理自己的模式并引用自己的模式历史表。将每个架构的迁移放在不同的位置。

理想情况下,每个模块都有自己的db/migration文件夹,其中包含自己的迁移 SQL 脚本。每个模块脚本的版本应该独立于其他模块中的脚本版本,并且每个模块的迁移历史应该存储在该模块模式的表中。

如果我将迁移脚本放在每个模块的resources/db/migration文件夹中,flyway 会检测到它们,然后抱怨:

org.flywaydb.core.api.FlywayException: Found more than one migration with version 0
Run Code Online (Sandbox Code Playgroud)

有人知道如何完成所需的设置吗?

PS 所有这一切的最终目标是能够(有一天,当系统扩展时)将这些模块拉入单独的服务中,而无需经历地狱将数据库分解为多个部分。

flyway spring-boot

9
推荐指数
1
解决办法
2437
查看次数

标签 统计

flyway ×1

python ×1

python-3.x ×1

spring-boot ×1

type-hinting ×1