Embed versioning number of my database as expected by my app

时间:2015-07-29 00:35:47

标签: database postgresql versioning h2

What ways might I use to identify the version of my database in Postgres?

On launch, I want my app to examine the database to verify if its structure is as expected. If the database is old and predates the app's expectations, I want to throw an exception. Likewise if the database is newer, having changes to its schema not expected by that codebase.

These needs are most likely to come up when restoring a backup of the database or when forking the app’s codebase.

Obviously one solution is to create an extra table with a single row with a single column name something like "structure_version_".

Are there alternative places to tuck such meta-data? In general? In Postgres specifically?

1 个答案:

答案 0 :(得分:1)

您可以编写与CREATE FUNCTION structure_version (OUT string text, OUT major int, OUT minor int) AS $$ BEGIN major := 4; minor := 1; string := 'My app is version ' || major || '.' || minor; END; $$ LANGUAGE plpgsql; 非常相似的函数来返回所需信息:

SELECT * FROM structure_version;

然后:

grantAppRole()