用于管理postgres迁移和测试的python工具集

saitama的Python项目详细描述


BuildCoverageLicensePyPIDownloadsCode style

埼玉:postgres迁移经理和测试运行者

埼玉提供了一种管理迁移和运行postgres单元测试的简单方法。在

安装

最简单的方法是使用pip安装埼玉。在

pip install --user saitama

配置

要为项目配置saitama,可以使用任何toml文件。在

^{pr2}$

所有设置都是可选的,默认的hostportdbnameuser和{}是由psycopg2指定的,默认迁移目录是migrations/,相对于toml文件的父目录,默认测试目录是tests/,也是相对于toml文件的。在

使用

Saitama一旦安装就提供了一个命令punch,它控制迁移和测试。punch遵循GNU对命令行接口的建议,并提供:

  • -h--help以打印帮助,并且
  • -V或{}打印版本

迁移

可以使用“冲压”向前或向后移植到特定移植。迁移应该命名为\d+_.+\.sql。向后是指数字后面第一个下划线后面跟backwards_的所有迁移。在

usage: punch migrate [-h] [-H HOST] [-P PORT] [-d DBNAME] [-u USER]
                     [-p PASSWORD] [-s SETTINGS] [-D] [-f] [-b] [-y]
                     [migration]

positional arguments:
  migration                        The target migration number. If unspecified,
                                   punch will migrate to latest one

optional arguments:
  -h, --help                       Show this help message and exit
  -H HOST, --host HOST             The postgres host
  -P PORT, --port PORT             The postgres port
  -d DBNAME, --dbname DBNAME       The postgres database
  -u USER, --user USER             The postgres user
  -p PASSWORD, --password PASSWORD The user's password
  -s SETTINGS, --settings SETTINGS The path to the settings file
  -D, --drop                       Drop the existing db and create a new one
  -f, --fake                       Fake the migrations up to the specified one
  -b, --backwards                  Backwards migration
  -y, --yes                        Run in non-interactive mode

测试

测试将创建一个名为test_<dbname>的数据库,运行该数据库的所有迁移,运行test目录中的测试,并生成一个报告。如果任何断言失败,则返回退出代码1。在

usage: punch test [-h] [-H HOST] [-P PORT] [-d DBNAME] [-u USER] [-p PASSWORD]
                  [-s SETTINGS]

optional arguments:
  -h, --help                       Show this help message and exit
  -H HOST, --host HOST             The postgres host
  -P PORT, --port PORT             The postgres port
  -d DBNAME, --dbname DBNAME       The postgres database
  -u USER, --user USER             The postgres user
  -p PASSWORD, --password PASSWORD The user's password
  -s SETTINGS, --settings SETTINGS The path to the settings file

编写迁移

要编写迁移,只需编写一个sql文件:

-- /path/to/0001_initial_migration.sqlCREATETABLEusers(user_idINTGENERATEDALWAYSASIDENTITYPRIMARYKEY,usernameTEXT,passwordTEXT);

还可以指定向后迁移

-- /path/to/0001_backwards_initial_migration.sqlDROPTABLEusers;

编写测试

为了编写迁移,只需在sql文件中编写PL/pgSQL函数,该文件返回unittest.result();的结果。您可以通过调用unittest.assert进行断言。函数应该在unittest模式中。在

-- /path/to/test_name.sqlCREATEFUNCTIONunittest.bar()RETURNSunittest.test_resultLANGUAGEplpgsqlAS$$DECLARE_cntINT;BEGININSERTINTOusers(username,password)VALUES('user','hashed_and_salted_password');SELECTcount(*)FROMusersINTO_cnt;PERFORMunittest.assert(_cnt<>0,'No user was created!');RETURNunittest.result();END$$;

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java通过Spring MVC web应用程序向客户端发送文本文件   java是否可以在运行时动态实例化DAO类?   调用VB。来自Java的net函数   java在Android中通过单击打开特定文件夹   java如何使用maven pom。xml标识非标准项目结构中的testng测试用例?   java为什么FOP在大文件上崩溃?   Architecture python+flask和spring boot+java   java Kafka工具根本没有启动Ubuntu 19.10   如何使用Eclipse运行Java USB API for Windows   java如何在Eclipse中查看J2EE预览服务器/容器的日志/控制台?   网页抓取是否可以使用Java crawler crawler4j暂停和恢复抓取?   java当我第二次按下按钮时,应用程序停止工作   带有偏移量和限制的java SQLite分页问题   java如何在OSX mavericks中将端口80转发到8080   java从泛型方法调用非泛型方法   java My代码未按预期工作。十进制输出不是它应该的样子   节点。java中的js加密(jasypt)和nodejs中的解密   java乘法表不工作数组索引超出范围   java将JDBC与Firebirdsql连接起来