Skip to content

Commit

Permalink
Make it work with PostgreSQL v13
Browse files Browse the repository at this point in the history
  • Loading branch information
exAspArk committed Jan 17, 2024
1 parent 99796dd commit ebcaa6f
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

#### [v0.2.2](https://github.com/BemiHQ/bemi-prisma/compare/v0.2.1...v0.2.2) - 2024-01-16

- Fix migration generation to work with PostgreSQL versions less than 14

#### [v0.2.1](https://github.com/BemiHQ/bemi-prisma/compare/v0.2.0...v0.2.1) - 2024-01-04

- Fix migration generation to work with table names containing double quotes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@bemi-db/prisma",
"version": "0.2.1",
"version": "0.2.2",
"description": "Automatic data change tracking for Prisma",
"main": "dist/index.js",
"module": "./dist/index.mjs",
Expand Down
29 changes: 22 additions & 7 deletions src/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,31 @@ CREATE OR REPLACE PROCEDURE _bemi_create_triggers()
AS $$
DECLARE
current_tablename TEXT;
pg_major_version INT;
BEGIN
pg_major_version := (SELECT SPLIT_PART(setting, '.', 1)::INT FROM pg_settings WHERE name = 'server_version');
FOR current_tablename IN
SELECT tablename FROM pg_tables WHERE schemaname = 'public'
LOOP
EXECUTE format(
'CREATE OR REPLACE TRIGGER _bemi_row_trigger_%s
BEFORE INSERT OR UPDATE OR DELETE ON %I FOR EACH ROW
EXECUTE FUNCTION _bemi_row_trigger_func()',
current_tablename, current_tablename
);
IF (pg_major_version >= 14) THEN
EXECUTE format(
'CREATE OR REPLACE TRIGGER _bemi_row_trigger_%s
BEFORE INSERT OR UPDATE OR DELETE ON %I FOR EACH ROW
EXECUTE FUNCTION _bemi_row_trigger_func()',
current_tablename, current_tablename
);
ELSE
EXECUTE format(
'DROP TRIGGER IF EXISTS _bemi_row_trigger_%s ON %I',
current_tablename, current_tablename
);
EXECUTE format(
'CREATE TRIGGER _bemi_row_trigger_%s
BEFORE INSERT OR UPDATE OR DELETE ON %I FOR EACH ROW
EXECUTE FUNCTION _bemi_row_trigger_func()',
current_tablename, current_tablename
);
END IF;
END LOOP;
END;
$$ LANGUAGE plpgsql;
Expand Down Expand Up @@ -72,7 +87,7 @@ const generateMigrationFile = async () => {

const program = new Command();

program.name("bemi").description("CLI to Bemi utilities").version("0.2.1");
program.name("bemi").description("CLI to Bemi utilities").version("0.2.2");

program.
command("migration:create").
Expand Down

0 comments on commit ebcaa6f

Please sign in to comment.