stripe / pg-schema-diff

Go library for diffing Postgres schemas and generating SQL migrations
MIT License
270 stars 20 forks source link

Unable to resolve sequence SQL #110

Closed masaya-kato63 closed 4 months ago

masaya-kato63 commented 4 months ago

Execute pg-schema-diff


$ docker run --name postgres --rm -d -p 5432:5432 -v /tmp/postgres-data:/var/lib/postgresql/data -e POSTGRES_PASSWORD=postgres -e PGPASSWORD=postgres public.ecr.aws/docker/library/postgres:15 postgres -N 10000

adb9d2e107dccb3d4ebb944b7116410dc07047aeba9027e0a2f37d8a7c04bc42

$ cat before.sql | docker exec -i postgres psql -U postgres -h localhost -p 5432 postgres

CREATE FUNCTION
ALTER FUNCTION
CREATE SEQUENCE
CREATE TABLE

$ cp before.sql schema/after.sql
$ vi schema/after.sql

$ pg-schema-diff plan --dsn "postgres://postgres:postgres@localhost:5432/postgres" --schema-dir schema

Error: generating plan: generating plan statements: generating migration statements: resolving sequence sql graphs: generating SQL for diff {oldAndNew:{old:{SchemaQualifiedName:{SchemaName:public EscapedName:"seq_proc_no"} Owner:<nil> Type:bigint StartValue:1 Increment:1 MaxValue:9223372036854775807 MinValue:1 CacheSize:1 Cycle:false} new:{SchemaQualifiedName:{SchemaName:public EscapedName:"seq_proc_no"} Owner:<nil> Type:bigint StartValue:1 Increment:1 MaxValue:89999 MinValue:1 CacheSize:1 Cycle:true}}}: altering sequence to resolve the following diff   schema.Sequence{
    ... // 3 identical fields
    StartValue: 1,
    Increment:  1,
-   MaxValue:   9223372036854775807,
+   MaxValue:   89999,
    MinValue:   1,
    CacheSize:  1,
-   Cycle:      false,
+   Cycle:      true,
  }
: not implemented

before.sql

CREATE FUNCTION public.set_datetime() RETURNS trigger
    LANGUAGE plpgsql
    AS $$    BEGIN
IF TG_OP = 'INSERT' THEN
NEW.UPDATE_DATETIME := current_timestamp;
NEW.CREATE_DATETIME := current_timestamp;
ELSE
IF TG_OP = 'UPDATE' THEN
NEW.UPDATE_DATETIME := current_timestamp;
END IF ;
END IF ;
RETURN NEW;
    END ;
$$;
ALTER FUNCTION public.set_datetime() OWNER TO postgres;

CREATE SEQUENCE seq_proc_no;

CREATE TABLE proc_value (
   id bigint primary key,
   proc_no  bigint,
   value text
);

after.sql

CREATE FUNCTION public.set_datetime() RETURNS trigger
    LANGUAGE plpgsql
    AS $$    BEGIN
IF TG_OP = 'INSERT' THEN
NEW.UPDATE_DATETIME := current_timestamp;
NEW.CREATE_DATETIME := current_timestamp;
ELSE
IF TG_OP = 'UPDATE' THEN
NEW.UPDATE_DATETIME := current_timestamp;
END IF ;
END IF ;
RETURN NEW;
    END ;
$$;
ALTER FUNCTION public.set_datetime() OWNER TO postgres;

CREATE SEQUENCE seq_proc_no
    INCREMENT BY 1
    MAXVALUE 89999
    START WITH 1
    CYCLE
;   

CREATE TABLE proc_value (
   id bigint primary key,
   proc_no  bigint,
   value text
);
bplunkett-stripe commented 4 months ago

The PR above should add support for altering sequences!

bplunkett-stripe commented 4 months ago

It should be supported now that the change has been merged in!

masaya-kato63 commented 4 months ago

THX!