用于管理postgres迁移和测试的python工具集
saitama的Python项目详细描述
埼玉:postgres迁移经理和测试运行者
埼玉提供了一种管理迁移和运行postgres单元测试的简单方法。在
安装
最简单的方法是使用pip安装埼玉。在
pip install --user saitama
配置
要为项目配置saitama,可以使用任何toml文件。在
^{pr2}$所有设置都是可选的,默认的host
、port
、dbname
、user
和{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$$;
- 项目
标签: