diff --git a/pkg/schema/pg/enum.go b/pkg/schema/pg/enum.go index f38c188..7a9c646 100644 --- a/pkg/schema/pg/enum.go +++ b/pkg/schema/pg/enum.go @@ -31,7 +31,10 @@ func (s *Schema) CreateEnum(name string, values []string, opts ...schema.CreateE options.Values = values if s.Context.MigrationDirection == types.MigrationDirectionDown { - s.rollbackMode().DropEnum(name) + s.rollbackMode().DropEnum(name, schema.DropEnumOptions{ + Schema: options.Schema, + IfExists: true, + }) return } diff --git a/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240527192300_enum.snap.sql b/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240527192300_enum.snap.sql new file mode 100644 index 0000000..7be627c --- /dev/null +++ b/testdata/Test_2e2_postgres/migration_with_change_migrations_with_change/20240527192300_enum.snap.sql @@ -0,0 +1,113 @@ +SET statement_timeout = 0; +SET lock_timeout = 0; +SET idle_in_transaction_session_timeout = 0; +SET client_encoding = 'UTF8'; +SET standard_conforming_strings = on; +SELECT pg_catalog.set_config('search_path', '', false); +SET check_function_bodies = false; +SET xmloption = content; +SET client_min_messages = warning; +SET row_security = off; + +-- +-- Name: migrations_with_change; Type: SCHEMA; Schema: -; Owner: - +-- + +CREATE SCHEMA migrations_with_change; + + +-- +-- Name: status; Type: TYPE; Schema: migrations_with_change; Owner: - +-- + +CREATE TYPE migrations_with_change.status AS ENUM ( + 'active', + 'inactive' +); + + +SET default_table_access_method = heap; + +-- +-- Name: mig_schema_versions; Type: TABLE; Schema: migrations_with_change; Owner: - +-- + +CREATE TABLE migrations_with_change.mig_schema_versions ( + id text NOT NULL +); + + +-- +-- Name: users; Type: TABLE; Schema: migrations_with_change; Owner: - +-- + +CREATE TABLE migrations_with_change.users ( + id integer NOT NULL, + name text, + email text, + created_at timestamp(6) without time zone DEFAULT now() NOT NULL, + updated_at timestamp(6) without time zone DEFAULT now() NOT NULL +); + + +-- +-- Name: users_id_seq; Type: SEQUENCE; Schema: migrations_with_change; Owner: - +-- + +CREATE SEQUENCE migrations_with_change.users_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: users_id_seq; Type: SEQUENCE OWNED BY; Schema: migrations_with_change; Owner: - +-- + +ALTER SEQUENCE migrations_with_change.users_id_seq OWNED BY migrations_with_change.users.id; + + +-- +-- Name: users id; Type: DEFAULT; Schema: migrations_with_change; Owner: - +-- + +ALTER TABLE ONLY migrations_with_change.users ALTER COLUMN id SET DEFAULT nextval('migrations_with_change.users_id_seq'::regclass); + + +-- +-- Name: mig_schema_versions mig_schema_versions_pkey; Type: CONSTRAINT; Schema: migrations_with_change; Owner: - +-- + +ALTER TABLE ONLY migrations_with_change.mig_schema_versions + ADD CONSTRAINT mig_schema_versions_pkey PRIMARY KEY (id); + + +-- +-- Name: users users_pkey; Type: CONSTRAINT; Schema: migrations_with_change; Owner: - +-- + +ALTER TABLE ONLY migrations_with_change.users + ADD CONSTRAINT users_pkey PRIMARY KEY (id); + + +-- +-- Name: idx_users_email; Type: INDEX; Schema: migrations_with_change; Owner: - +-- + +CREATE UNIQUE INDEX idx_users_email ON migrations_with_change.users USING btree (email); + + +-- +-- Name: idx_users_name; Type: INDEX; Schema: migrations_with_change; Owner: - +-- + +CREATE INDEX idx_users_name ON migrations_with_change.users USING btree (name); + + +-- +-- PostgreSQL database dump complete +-- + diff --git a/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240527192355_enum.snap.sql b/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240527192355_enum.snap.sql new file mode 100644 index 0000000..24c9fcc --- /dev/null +++ b/testdata/Test_2e2_postgres/migration_with_classic_migrations_with_classic/20240527192355_enum.snap.sql @@ -0,0 +1,113 @@ +SET statement_timeout = 0; +SET lock_timeout = 0; +SET idle_in_transaction_session_timeout = 0; +SET client_encoding = 'UTF8'; +SET standard_conforming_strings = on; +SELECT pg_catalog.set_config('search_path', '', false); +SET check_function_bodies = false; +SET xmloption = content; +SET client_min_messages = warning; +SET row_security = off; + +-- +-- Name: migrations_with_classic; Type: SCHEMA; Schema: -; Owner: - +-- + +CREATE SCHEMA migrations_with_classic; + + +-- +-- Name: status; Type: TYPE; Schema: migrations_with_classic; Owner: - +-- + +CREATE TYPE migrations_with_classic.status AS ENUM ( + 'active', + 'inactive' +); + + +SET default_table_access_method = heap; + +-- +-- Name: mig_schema_versions; Type: TABLE; Schema: migrations_with_classic; Owner: - +-- + +CREATE TABLE migrations_with_classic.mig_schema_versions ( + id text NOT NULL +); + + +-- +-- Name: users; Type: TABLE; Schema: migrations_with_classic; Owner: - +-- + +CREATE TABLE migrations_with_classic.users ( + id integer NOT NULL, + name text, + email text, + created_at timestamp(6) without time zone DEFAULT now() NOT NULL, + updated_at timestamp(6) without time zone DEFAULT now() NOT NULL +); + + +-- +-- Name: users_id_seq; Type: SEQUENCE; Schema: migrations_with_classic; Owner: - +-- + +CREATE SEQUENCE migrations_with_classic.users_id_seq + AS integer + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + +-- +-- Name: users_id_seq; Type: SEQUENCE OWNED BY; Schema: migrations_with_classic; Owner: - +-- + +ALTER SEQUENCE migrations_with_classic.users_id_seq OWNED BY migrations_with_classic.users.id; + + +-- +-- Name: users id; Type: DEFAULT; Schema: migrations_with_classic; Owner: - +-- + +ALTER TABLE ONLY migrations_with_classic.users ALTER COLUMN id SET DEFAULT nextval('migrations_with_classic.users_id_seq'::regclass); + + +-- +-- Name: mig_schema_versions mig_schema_versions_pkey; Type: CONSTRAINT; Schema: migrations_with_classic; Owner: - +-- + +ALTER TABLE ONLY migrations_with_classic.mig_schema_versions + ADD CONSTRAINT mig_schema_versions_pkey PRIMARY KEY (id); + + +-- +-- Name: users users_pkey; Type: CONSTRAINT; Schema: migrations_with_classic; Owner: - +-- + +ALTER TABLE ONLY migrations_with_classic.users + ADD CONSTRAINT users_pkey PRIMARY KEY (id); + + +-- +-- Name: idx_users_email; Type: INDEX; Schema: migrations_with_classic; Owner: - +-- + +CREATE UNIQUE INDEX idx_users_email ON migrations_with_classic.users USING btree (email); + + +-- +-- Name: idx_users_name; Type: INDEX; Schema: migrations_with_classic; Owner: - +-- + +CREATE INDEX idx_users_name ON migrations_with_classic.users USING btree (name); + + +-- +-- PostgreSQL database dump complete +-- + diff --git a/testdata/e2e/pg/migrations_with_change/20240527192300_enum.go b/testdata/e2e/pg/migrations_with_change/20240527192300_enum.go new file mode 100644 index 0000000..eb7f234 --- /dev/null +++ b/testdata/e2e/pg/migrations_with_change/20240527192300_enum.go @@ -0,0 +1,24 @@ +package migrations + +import ( + "github.com/alexisvisco/amigo/pkg/schema" + "github.com/alexisvisco/amigo/pkg/schema/pg" + "time" +) + +type Migration20240527192300Enum struct{} + +func (m Migration20240527192300Enum) Change(s *pg.Schema) { + s.CreateEnum("status", []string{"active", "inactive"}, schema.CreateEnumOptions{ + Schema: "migrations_with_change", + }) +} + +func (m Migration20240527192300Enum) Name() string { + return "enum" +} + +func (m Migration20240527192300Enum) Date() time.Time { + t, _ := time.Parse(time.RFC3339, "2024-05-27T21:23:00+02:00") + return t +} diff --git a/testdata/e2e/pg/migrations_with_change/migrations.go b/testdata/e2e/pg/migrations_with_change/migrations.go index dd314d1..91b9cce 100644 --- a/testdata/e2e/pg/migrations_with_change/migrations.go +++ b/testdata/e2e/pg/migrations_with_change/migrations.go @@ -11,4 +11,5 @@ var Migrations = []schema.Migration{ &Migration20240518071740CreateUser{}, &Migration20240518071842AddIndexUserEmail{}, &Migration20240518071938CustomSeed{}, + &Migration20240527192300Enum{}, } diff --git a/testdata/e2e/pg/migrations_with_classic/20240527192355_enum.go b/testdata/e2e/pg/migrations_with_classic/20240527192355_enum.go new file mode 100644 index 0000000..dcba8f5 --- /dev/null +++ b/testdata/e2e/pg/migrations_with_classic/20240527192355_enum.go @@ -0,0 +1,28 @@ +package migrations + +import ( + "github.com/alexisvisco/amigo/pkg/schema" + "github.com/alexisvisco/amigo/pkg/schema/pg" + "time" +) + +type Migration20240527192355Enum struct{} + +func (m Migration20240527192355Enum) Up(s *pg.Schema) { + s.CreateEnum("status", []string{"active", "inactive"}, schema.CreateEnumOptions{ + Schema: "migrations_with_classic", + }) +} + +func (m Migration20240527192355Enum) Down(s *pg.Schema) { + s.DropEnum("status", schema.DropEnumOptions{Schema: "migrations_with_classic"}) +} + +func (m Migration20240527192355Enum) Name() string { + return "enum" +} + +func (m Migration20240527192355Enum) Date() time.Time { + t, _ := time.Parse(time.RFC3339, "2024-05-27T21:23:55+02:00") + return t +} diff --git a/testdata/e2e/pg/migrations_with_classic/migrations.go b/testdata/e2e/pg/migrations_with_classic/migrations.go index dd314d1..ff9d7d6 100644 --- a/testdata/e2e/pg/migrations_with_classic/migrations.go +++ b/testdata/e2e/pg/migrations_with_classic/migrations.go @@ -11,4 +11,5 @@ var Migrations = []schema.Migration{ &Migration20240518071740CreateUser{}, &Migration20240518071842AddIndexUserEmail{}, &Migration20240518071938CustomSeed{}, + &Migration20240527192355Enum{}, }