I want to do a versioning control of my database.
I currently control my front-end applications through git, however I am creating my database and would like to have a versioning of my tables, function and procedures, how can I accomplish this for database? That is, I will make a change in a function but I would like to save the previous one I was executing in case there is any problem I can put the previous one again.
It is possible to call stored procedures using three procedural languages – SQL, PL/pgSQL, and C – which are by default supported by PostgreSQL.
In Postgres, the main functional difference between a function and a stored procedure is that a function returns a result, whereas a stored procedure does not. This is because the intention behind a stored procedure is to perform some sort of activity and then finish, which would then return control to the caller.
PostgreSQL allows the users to extend the database functionality with the help of user-defined functions and stored procedures through various procedural language elements, which are often referred to as stored procedures. The store procedures define functions for creating triggers or custom aggregate functions.
The user must have EXECUTE privilege on the procedure in order to be allowed to invoke it. To call a function (not a procedure), use SELECT instead. If CALL is executed in a transaction block, then the called procedure cannot execute transaction control statements.
Major tools for versioning of database structure including optional data migration are:
However, the specifics of your question are beyond what existing tools offer today.
If you need to have two or more versions of some object in the database for parallel use (e.g. step-by-step migration of tables, triggers, aso.) you are better off using either:
my_cool_function_v2
vs. my_cool_function_v3
...or:
CREATE FUNCTION my_schema_v2.my_cool_function
will not collide with my_schema_v1.my_cool_function
In both cases, you usually have to manage referencing the newer version where wanted. For the second approach, this can be further simplified with the schema search_path
, which you can modify to prefer a new schema containing new versions of the objects, e.g. with:
SET search_path TO my_schema_v2, my_schema_v1, public;
dynamically (useful for testing in the live system without affecting actual applications/users) and once you are confident the basics are set, include it into the PostgreSQL configuration (postgresql.conf
) so that the new schema becomes standard for every new connection:
search_path = 'my_schema_v2, my_schema_v1, public'
After you have migrated all of the new objects and everything is working fine, you can remove the old my_schema_v1
from the search_path
and also DROP ... CASCADE
it to remove all the old objects at once.
However, one downside of the schema approach is that if you always create all objects (functions, triggers, tables, ...) in all schemas, you'll lose the benefits of it when combined with the search_path
. Therefore, I would create different schemas for different objects, e.g. data_v1
for the data (tables, indexes, ...) and func_v1
for other things (functions, procedures, ...). That way you can evolve the structure independently of the data but at the same time, you can also start evolving the table structure and automatically benefit from fixes/improvements in functions but also test whether changes are forward-compatible.
Hi future readers of this Q, I am the maintainer of project #yuniql: a schema versioning and migration tool than you can run with zero dependencies. In scenario described, most tools treats every change in the database as immutable or atomic change and if you need to rollback your change, it will just be another minor or major version. This way you can fully reconstruct the database anywhere, anytime and at the same time tracks the evolution of your database schema.
In yuniql, your workspace will typically be organized like this
v0.00
+ create_schema.sql
+ create_table_customers.sql
v0.01
+ create_table_custmomers_with_contact.sql
v0.02
+ create_table_custmomers_with_picture.sql
You may refer to github for getting started and samples ;)
https://github.com/rdagumampan/yuniql
https://yuniql.io/docs/get-started-postgresql/
Br, Rodel
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With