



GRANT USAGE, CREATE ON SCHEMA swp TO sathumper;
 
ALTER DEFAULT PRIVILEGES IN SCHEMA swp 
GRANT ALL PRIVILEGES ON TABLES TO sathumper;

GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA swp TO sathumper;

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA swp TO sathumper;


select * from dev.app_configuration

CREATE OR REPLACE FUNCTION dev.notify_config_change()
RETURNS TRIGGER AS $$
BEGIN
    IF TG_OP = 'DELETE' THEN
        PERFORM pg_notify(
            'config_changes', 
            json_build_object('operation', TG_OP, 'data', row_to_json(OLD))::text
        );
        RETURN OLD;  -- Return OLD for DELETE operations
    ELSE
        PERFORM pg_notify(
            'config_changes', 
            json_build_object('operation', TG_OP, 'data', row_to_json(NEW))::text
        );
        RETURN NEW;  -- Return NEW for INSERT/UPDATE operations
    END IF;
END;
$$ LANGUAGE plpgsql;



-- Trigger på configuration tabellen
CREATE TRIGGER config_change_trigger
AFTER INSERT OR UPDATE OR DELETE ON dev.app_configuration
FOR EACH ROW EXECUTE FUNCTION dev.notify_config_change();


update dev.app_configuration 
set "label"  = "label" where id = 3

SELECT row_to_json(t) 
FROM (SELECT 1 as id, 'test' as key) t;

SET myapp.tenant_id = '1';

SHOW myapp.tenant_id;

create TABLE dev.app_configuration1 (
    tenant_id varchar(25),
    config_key VARCHAR(255),
    config_value TEXT,
    PRIMARY KEY (tenant_id, config_key)
);

ALTER TABLE dev.app_configuration1 ENABLE ROW LEVEL SECURITY;

CREATE drop POLICY tenant_policy
ON dev.app_configuration1
FOR SELECT
USING (
    current_setting('myapp.tenant_id', true) IS NOT NULL AND 
    tenant_id = current_setting('myapp.tenant_id')::INT
);

CREATE POLICY tenant_policy
ON dev.app_configuration1
FOR SELECT
USING (
  tenant_id::text = current_user
);

insert into dev.app_configuration1
(tenant_id, config_key, config_value) values('dev', 't2', 'best dat')

SET myapp.tenant_id = 0

ALTER USER din_bruger NOBYPASS RLS;
select * from dev.app_configuration1

SHOW row_security;




GRANT USAGE, CREATE ON SCHEMA dev TO sathumper;

ALTER DEFAULT PRIVILEGES IN SCHEMA dev 
GRANT ALL PRIVILEGES ON TABLES TO sathumper;

GRANT ALL PRIVILEGES ON ALL SEQUENCES IN SCHEMA dev TO sathumper;

GRANT ALL PRIVILEGES ON ALL TABLES IN SCHEMA dev TO sathumper;

