Skip to content

Commit

Permalink
Add metadata to stored procedures for 3rd party libs
Browse files Browse the repository at this point in the history
This way a third party can parse the meta data and knows how
to handle the bodies of a stored procedure
  • Loading branch information
mkurz committed Nov 26, 2024
1 parent 103e832 commit e99f922
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ protected void createOrReplaceFunction(DdlBuffer apply, String procedureName, St
apply
.append("create or replace function ").append(procedureName).append("() returns trigger as $$").newLine();

apply.append("-- play-ebean-start").newLine();

apply.append("declare").newLine()
.append(" lowerTs timestamptz;").newLine()
.append(" upperTs timestamptz;").newLine();
Expand All @@ -93,6 +95,7 @@ protected void createOrReplaceFunction(DdlBuffer apply, String procedureName, St
apply
.append(" end if;").newLine()
.append("end;").newLine()
.append("-- play-ebean-end").newLine()
.append("$$ LANGUAGE plpgsql;").newLine();

apply.end();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
-- Type used to hold common partitioning parameters such as period start and end etc
------------------------------------------------------------------------------------
do $$
-- play-ebean-start
begin
if not exists (select 1 from pg_type where typname = 'partition_meta') THEN
create type partition_meta as
Expand All @@ -22,7 +23,9 @@ begin
part_name text
);
end if;
end$$;
end;
-- play-ebean-end
$$;

------------------------------------------------------------------------------------
-- Function: _partition_create
Expand All @@ -34,6 +37,7 @@ create or replace function _partition_create(meta partition_meta)
language plpgsql
set timezone to 'UTC'
as $$
-- play-ebean-start
begin
if (meta.schema_name = '') then
execute format('create table if not exists %I partition of %I for values from (''%s'') TO (''%s'')', meta.part_name, meta.base_name, meta.period_start, meta.period_end);
Expand All @@ -42,6 +46,7 @@ begin
end if;
return meta.part_name;
end;
-- play-ebean-end
$$;


Expand All @@ -60,6 +65,7 @@ create or replace function _partition_meta(
language plpgsql
set timezone to 'UTC'
as $$
-- play-ebean-start
declare
partName text;
meta partition_meta;
Expand Down Expand Up @@ -92,6 +98,7 @@ begin

return meta;
end;
-- play-ebean-end
$$;

------------------------------------------------------------------------------------
Expand All @@ -108,6 +115,7 @@ create or replace function _partition_over(
returns TABLE(of_date date)
language plpgsql
as $$
-- play-ebean-start
declare
endDate date;
begin
Expand All @@ -132,6 +140,7 @@ begin
return query select s::date from generate_series(fromDate, endDate, '1 month') s;
end if;
end;
-- play-ebean-end
$$;


Expand All @@ -158,11 +167,13 @@ create or replace function partition(
language plpgsql
set timezone to 'UTC'
as $$
-- play-ebean-start
begin
perform _partition_create(_partition_meta(mode, poDate, baseName, schemaName))
from _partition_over(mode, fromDate, partitionCount) poDate;
return 'done';
end;
-- play-ebean-end
$$;
</ddl-script>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@ GO
-- deletes all indices referring to TABLE.COLUMN
--
CREATE OR ALTER PROCEDURE usp_ebean_drop_indices @tableName nvarchar(255), @columnName nvarchar(255)
AS SET NOCOUNT ON
AS
-- play-ebean-start
SET NOCOUNT ON
declare @sql nvarchar(1000)
declare @indexName nvarchar(255)
BEGIN
Expand All @@ -44,14 +46,17 @@ BEGIN
CLOSE index_cursor;
DEALLOCATE index_cursor;
END
-- play-ebean-end
GO

--
-- PROCEDURE: usp_ebean_drop_default_constraint TABLE, COLUMN
-- deletes the default constraint, which has a random name
--
CREATE OR ALTER PROCEDURE usp_ebean_drop_default_constraint @tableName nvarchar(255), @columnName nvarchar(255)
AS SET NOCOUNT ON
AS
-- play-ebean-start
SET NOCOUNT ON
declare @tmp nvarchar(1000)
BEGIN
select @tmp = t1.name from sys.default_constraints t1
Expand All @@ -60,14 +65,17 @@ BEGIN

if @tmp is not null EXEC('alter table [' + @tableName +'] drop constraint ' + @tmp);
END
-- play-ebean-end
GO

--
-- PROCEDURE: usp_ebean_drop_constraints TABLE, COLUMN
-- deletes constraints and foreign keys refering to TABLE.COLUMN
--
CREATE OR ALTER PROCEDURE usp_ebean_drop_constraints @tableName nvarchar(255), @columnName nvarchar(255)
AS SET NOCOUNT ON
AS
-- play-ebean-start
SET NOCOUNT ON
declare @sql nvarchar(1000)
declare @constraintName nvarchar(255)
BEGIN
Expand All @@ -93,14 +101,17 @@ BEGIN
CLOSE name_cursor;
DEALLOCATE name_cursor;
END
-- play-ebean-end
GO

--
-- PROCEDURE: usp_ebean_drop_column TABLE, COLUMN
-- deletes the column annd ensures that all indices and constraints are dropped first
--
CREATE OR ALTER PROCEDURE usp_ebean_drop_column @tableName nvarchar(255), @columnName nvarchar(255)
AS SET NOCOUNT ON
AS
-- play-ebean-start
SET NOCOUNT ON
declare @sql nvarchar(1000)
BEGIN
EXEC usp_ebean_drop_indices @tableName, @columnName;
Expand All @@ -110,6 +121,7 @@ BEGIN
set @sql = 'alter table [' + @tableName + '] drop column [' + @columnName + ']';
EXECUTE(@sql);
END
-- play-ebean-end
GO
</ddl-script>
<ddl-script name="create procs" platforms="mysql mariadb" init="true">-- Inital script to create stored procedures etc for mysql platform
Expand All @@ -121,6 +133,7 @@ delimiter $$
-- deletes all constraints and foreign keys referring to TABLE.COLUMN
--
CREATE PROCEDURE usp_ebean_drop_foreign_keys(IN p_table_name VARCHAR(255), IN p_column_name VARCHAR(255))
-- play-ebean-start
BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE c_fk_name CHAR(255);
Expand All @@ -143,6 +156,7 @@ END LOOP;

CLOSE curs;
END
-- play-ebean-end
$$

DROP PROCEDURE IF EXISTS usp_ebean_drop_column;
Expand All @@ -153,12 +167,14 @@ delimiter $$
-- deletes the column and ensures that all indices and constraints are dropped first
--
CREATE PROCEDURE usp_ebean_drop_column(IN p_table_name VARCHAR(255), IN p_column_name VARCHAR(255))
-- play-ebean-start
BEGIN
CALL usp_ebean_drop_foreign_keys(p_table_name, p_column_name);
SET @sql = CONCAT('ALTER TABLE `', p_table_name, '` DROP COLUMN `', p_column_name, '`');
PREPARE stmt FROM @sql;
EXECUTE stmt;
END
-- play-ebean-end
$$
</ddl-script>

Expand All @@ -170,6 +186,7 @@ delimiter $$
--
CREATE OR REPLACE PROCEDURE usp_ebean_drop_foreign_keys(IN table_name NVARCHAR(256), IN column_name NVARCHAR(256))
AS
-- play-ebean-start
BEGIN
DECLARE foreign_key_names TABLE(CONSTRAINT_NAME NVARCHAR(256), TABLE_NAME NVARCHAR(256));
DECLARE i INT;
Expand All @@ -181,6 +198,7 @@ EXEC 'ALTER TABLE "' || ESCAPE_DOUBLE_QUOTES(:foreign_key_names.TABLE_NAME[i]) |
END FOR;

END;
-- play-ebean-end
$$

delimiter $$
Expand All @@ -190,10 +208,12 @@ delimiter $$
--
CREATE OR REPLACE PROCEDURE usp_ebean_drop_column(IN table_name NVARCHAR(256), IN column_name NVARCHAR(256))
AS
-- play-ebean-start
BEGIN
CALL usp_ebean_drop_foreign_keys(table_name, column_name);
EXEC 'ALTER TABLE "' || UPPER(ESCAPE_DOUBLE_QUOTES(table_name)) || '" DROP ("' || UPPER(ESCAPE_DOUBLE_QUOTES(column_name)) || '")';
END;
-- play-ebean-end
$$
</ddl-script>
</extra-ddl>

0 comments on commit e99f922

Please sign in to comment.