75 lines
1.8 KiB
C#
75 lines
1.8 KiB
C#
using Microsoft.EntityFrameworkCore.Migrations;
|
|
|
|
#nullable disable
|
|
|
|
namespace Pf2eModel.Migrations;
|
|
|
|
/// <inheritdoc />
|
|
public partial class Function : Migration
|
|
{
|
|
/// <inheritdoc />
|
|
protected override void Up(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.Sql("""
|
|
CREATE OR REPLACE FUNCTION f_truncate_tables()
|
|
RETURNS void
|
|
LANGUAGE plpgsql AS
|
|
$func$
|
|
BEGIN
|
|
EXECUTE
|
|
(SELECT 'TRUNCATE TABLE '
|
|
|| string_agg(format('%I.%I', schemaname, tablename), ', ')
|
|
|| ' CASCADE;'
|
|
FROM pg_tables
|
|
WHERE schemaname = 'public'
|
|
AND tablename != '__EFMigrationsHistory'
|
|
);
|
|
END
|
|
$func$;
|
|
""");
|
|
migrationBuilder.Sql("""
|
|
CREATE OR REPLACE FUNCTION f_drop_tables()
|
|
RETURNS void
|
|
LANGUAGE plpgsql AS
|
|
$func$
|
|
BEGIN
|
|
EXECUTE
|
|
(SELECT 'DROP TABLE '
|
|
|| string_agg(format('%I.%I', schemaname, tablename), ', ')
|
|
|| ' CASCADE;'
|
|
FROM pg_tables
|
|
WHERE schemaname = 'public'
|
|
);
|
|
END
|
|
$func$;
|
|
""");
|
|
migrationBuilder.Sql("""
|
|
CREATE OR REPLACE FUNCTION f_permission_tables(_username text)
|
|
RETURNS void
|
|
LANGUAGE plpgsql AS
|
|
$func$
|
|
DECLARE
|
|
_tbl text;
|
|
_sch text;
|
|
BEGIN
|
|
FOR _sch, _tbl IN
|
|
SELECT schemaname, tablename
|
|
FROM pg_tables
|
|
WHERE schemaname = 'public'
|
|
LOOP
|
|
EXECUTE format('ALTER TABLE %I.%I OWNER TO %I', _sch, _tbl, _username);
|
|
END LOOP;
|
|
END
|
|
$func$;
|
|
""");
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
protected override void Down(MigrationBuilder migrationBuilder)
|
|
{
|
|
migrationBuilder.Sql("DROP FUNCTION f_truncate_tables;");
|
|
migrationBuilder.Sql("DROP FUNCTION f_drop_tables;");
|
|
migrationBuilder.Sql("DROP FUNCTION f_permission_tables;");
|
|
}
|
|
}
|