用于管理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仿射变换以旋转通用路径   IntelliJ IDEA中的java代码标记为编译器错误,在Eclipse中可以正常工作   java ArrayList的containsAll返回了错误的值   如何在安卓中的片段活动中添加java代码   java通过使用netbeans调用jbutton,将带有行的jbutton添加到jtable中   java在更新arrayAdapter/listView之前更新最终列表   java如何在另一个项目中导入包含AspectJ方面和注释的项目   为什么我的java布尔测试总是失败?   Eclipse中的java自动激活   在Bluetooth for Java(更具体地说是Android)之上是否有TCP/IP协议栈实现?   java Android Studio通过intent传递ArrayList并填充ListView