8000 Indent trigger functions by tagliala · Pull Request #157 · ifad/chronomodel · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Indent trigger functions #157

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 29, 2022
Merged

Indent trigger functions #157

merged 3 commits into from
Apr 29, 2022

Conversation

tagliala
Copy link
Member

Default indentation used in structure.sql is 4 spaces. This will
create nicely indented trigger functions

Since Chronomodel supports Ruby 2.2, this commit uses strip_heredoc
method from activesupport rather than squiggly heredoc introduced
in Ruby 2.3

Before

--
-- Name: chronomodel_movies_delete(); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION public.chronomodel_movies_delete() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
              DECLARE _now timestamp;
              BEGIN
                _now := timezone('UTC', now());

                DELETE FROM history.movies
                WHERE id = old.id AND validity = tsrange(_now, NULL);

                UPDATE history.movies SET validity = tsrange(lower(validity), _now)
                WHERE id = old.id AND upper_inf(validity);

                DELETE FROM ONLY temporal.movies
                WHERE id = old.id;

                RETURN OLD;
              END;
            $$;


--
-- Name: chronomodel_movies_insert(); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION public.chronomodel_movies_insert() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
              BEGIN
                            IF NEW.id IS NULL THEN
              NEW.id := nextval('temporal.movies_id_seq');
            END IF;


                INSERT INTO temporal.movies ( id, "name", "created_at", "updated_at" )
                VALUES ( NEW.id, NEW."name", NEW."created_at", NEW."updated_at" );

                INSERT INTO history.movies ( id, "name", "created_at", "updated_at", validity )
                VALUES ( NEW.id, NEW."name", NEW."created_at", NEW."updated_at", tsrange(timezone('UTC', now()), NULL) );

                RETURN NEW;
              END;
            $$;


--
-- Name: chronomodel_movies_update(); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION public.chronomodel_movies_update() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
              DECLARE _now timestamp;
              DECLARE _hid integer;
              DECLARE _old record;
              DECLARE _new record;
              BEGIN
                IF OLD IS NOT DISTINCT FROM NEW THEN
                  RETURN NULL;
                END IF;

                _old := row(OLD."name", OLD."created_at");
                _new := row(NEW."name", NEW."created_at");

                IF _old IS NOT DISTINCT FROM _new THEN
                  UPDATE ONLY temporal.movies SET ( "name", "created_at", "updated_at" ) = ( NEW."name", NEW."created_at", NEW."updated_at" ) WHERE id = OLD.id;
                  RETURN NEW;
                END IF;

                _now := timezone('UTC', now());
                _hid := NULL;

                SELECT hid INTO _hid FROM history.movies WHERE id = OLD.id AND lower(validity) = _now;

                IF _hid IS NOT NULL THEN
                  UPDATE history.movies SET ( "name", "created_at", "updated_at" ) = ( NEW."name", NEW."created_at", NEW."updated_at" ) WHERE hid = _hid;
                ELSE
                  UPDATE history.movies SET validity = tsrange(lower(validity), _now)
                  WHERE id = OLD.id AND upper_inf(validity);

                  INSERT INTO history.movies ( id, "name", "created_at", "updated_at", validity )
                       VALUES ( OLD.id, NEW."name", NEW."created_at", NEW."updated_at", tsrange(_now, NULL) );
                END IF;

                UPDATE ONLY temporal.movies SET ( "name", "created_at", "updated_at" ) = ( NEW."name", NEW."created_at", NEW."updated_at" ) WHERE id = OLD.id;

                RETURN NEW;
              END;
            $$;


--
-- Name: chronomodel_songs_insert(); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION public.chronomodel_songs_insert() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
              BEGIN
                

                INSERT INTO temporal.songs ( id, "name", "created_at", "updated_at" )
                VALUES ( NEW.id, NEW."name", NEW."created_at", NEW."updated_at" );

                INSERT INTO history.songs ( id, "name", "created_at", "updated_at", validity )
                VALUES ( NEW.id, NEW."name", NEW."created_at", NEW."updated_at", tsrange(timezone('UTC', now()), NULL) );

                RETURN NEW;
              END;
            $$;

After

--
-- Name: chronomodel_movies_delete(); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION public.chronomodel_movies_delete() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
    DECLARE _now timestamp;
    BEGIN
        _now := timezone('UTC', now());

        DELETE FROM history.movies
        WHERE id = old.id AND validity = tsrange(_now, NULL);

        UPDATE history.movies SET validity = tsrange(lower(validity), _now)
        WHERE id = old.id AND upper_inf(validity);

        DELETE FROM ONLY temporal.movies
        WHERE id = old.id;

        RETURN OLD;
    END;

$$;


--
-- Name: chronomodel_movies_insert(); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION public.chronomodel_movies_insert() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
    BEGIN
        IF NEW.id IS NULL THEN
            NEW.id := nextval('temporal.movies_id_seq');
        END IF;

        INSERT INTO temporal.movies ( id, "name", "created_at", "updated_at" )
        VALUES ( NEW.id, NEW."name", NEW."created_at", NEW."updated_at" );

        INSERT INTO history.movies ( id, "name", "created_at", "updated_at", validity )
        VALUES ( NEW.id, NEW."name", NEW."created_at", NEW."updated_at", tsrange(timezone('UTC', now()), NULL) );

        RETURN NEW;
    END;

$$;


--
-- Name: chronomodel_movies_update(); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION public.chronomodel_movies_update() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
    DECLARE _now timestamp;
    DECLARE _hid integer;
    DECLARE _old record;
    DECLARE _new record;
    BEGIN
        IF OLD IS NOT DISTINCT FROM NEW THEN
            RETURN NULL;
        END IF;

        _old := row(OLD."name", OLD."created_at");
        _new := row(NEW."name", NEW."created_at");

        IF _old IS NOT DISTINCT FROM _new THEN
            UPDATE ONLY temporal.movies SET ( "name", "created_at", "updated_at" ) = ( NEW."name", NEW."created_at", NEW."updated_at" ) WHERE id = OLD.id;
            RETURN NEW;
        END IF;

        _now := timezone('UTC', now());
        _hid := NULL;

        SELECT hid INTO _hid FROM history.movies WHERE id = OLD.id AND lower(validity) = _now;

        IF _hid IS NOT NULL THEN
            UPDATE history.movies SET ( "name", "created_at", "updated_at" ) = ( NEW."name", NEW."created_at", NEW."updated_at" ) WHERE hid = _hid;
        ELSE
            UPDATE history.movies SET validity = tsrange(lower(validity), _now)
            WHERE id = OLD.id AND upper_inf(validity);

            INSERT INTO history.movies ( id, "name", "created_at", "updated_at", validity )
                VALUES ( OLD.id, NEW."name", NEW."created_at", NEW."updated_at", tsrange(_now, NULL) );
        END IF;

        UPDATE ONLY temporal.movies SET ( "name", "created_at", "updated_at" ) = ( NEW."name", NEW."created_at", NEW."updated_at" ) WHERE id = OLD.id;

        RETURN NEW;
    END;

$$;


--
-- Name: chronomodel_songs_insert(); Type: FUNCTION; Schema: public; Owner: -
--

CREATE FUNCTION public.chronomodel_songs_insert() RETURNS trigger
    LANGUAGE plpgsql
    AS $$
    BEGIN
        INSERT INTO temporal.songs ( id, "name", "created_at", "updated_at" )
        VALUES ( NEW.id, NEW."name", NEW."created_at", NEW."updated_at" );

        INSERT INTO history.songs ( id, "name", "created_at", "updated_at", validity )
        VALUES ( NEW.id, NEW."name", NEW."created_at", NEW."updated_at", tsrange(timezone('UTC', now()), NULL) );

        RETURN NEW;
    END;

$$;

Default indentation used in structure.sql is 4 spaces. This will
create nicely indented trigger functions

Since Chronomodel supports Ruby 2.2, this commit uses `strip_heredoc`
method from activesupport rather than squiggly heredoc introduced
in Ruby 2.3
@tagliala tagliala force-pushed the feature/strip-heredocs branch 2 times, most recently from ba5094f to d4c0832 Compare April 29, 2022 07:53
@tagliala tagliala marked this pull request as ready for review April 29, 2022 07:56
The latest released version 5.2.7.1 is not compatible with Ruby 2.2

Revert this commit when a new 5.2 version will be released

Ref: rails/rails#44966
@tagliala tagliala force-pushed the feature/strip-heredocs branch from d4c0832 to d25d110 Compare April 29, 2022 08:03
@tagliala tagliala merged commit 2636c40 into master Apr 29, 2022
@tagliala tagliala deleted the feature/strip-heredocs branch April 29, 2022 08:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant
0