From 9790756b984e0c19799f756f8242b5cb09ba589c Mon Sep 17 00:00:00 2001 From: Marc Scholten Date: Tue, 11 Jan 2022 08:10:56 +0100 Subject: [PATCH] Fixed migrations not normalising SERIAL and BIGSERIAL columns --- IHP/IDE/CodeGen/MigrationGenerator.hs | 2 + Test/IDE/CodeGeneration/MigrationGenerator.hs | 48 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/IHP/IDE/CodeGen/MigrationGenerator.hs b/IHP/IDE/CodeGen/MigrationGenerator.hs index cdc9d8f59..a6314f921 100644 --- a/IHP/IDE/CodeGen/MigrationGenerator.hs +++ b/IHP/IDE/CodeGen/MigrationGenerator.hs @@ -436,6 +436,8 @@ normalizeExpression (ExistsExpression a) = ExistsExpression (normalizeExpression normalizeSqlType :: PostgresType -> PostgresType normalizeSqlType (PCustomType customType) = PCustomType (Text.toLower customType) +normalizeSqlType PBigserial = PBigInt +normalizeSqlType PSerial = PInt normalizeSqlType otherwise = otherwise migrationPathFromPlan :: [GeneratorAction] -> Text diff --git a/Test/IDE/CodeGeneration/MigrationGenerator.hs b/Test/IDE/CodeGeneration/MigrationGenerator.hs index 484439526..e8a2aaa68 100644 --- a/Test/IDE/CodeGeneration/MigrationGenerator.hs +++ b/Test/IDE/CodeGeneration/MigrationGenerator.hs @@ -729,6 +729,54 @@ tests = do diffSchemas targetSchema actualSchema `shouldBe` [] + it "should normalize Bigserials" do + let targetSchema = sql [i| + CREATE TABLE testserial ( + testcol BIGSERIAL NOT NULL + ); + |] + let actualSchema = sql [i| + CREATE TABLE public.testserial ( + testcol bigint NOT NULL + ); + + CREATE SEQUENCE public.testserial_testcol_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + ALTER SEQUENCE public.testserial_testcol_seq OWNED BY public.testserial.testcol; + ALTER TABLE ONLY public.testserial ALTER COLUMN testcol SET DEFAULT nextval('public.testserial_testcol_seq'::regclass); + |] + + diffSchemas targetSchema actualSchema `shouldBe` [] + + it "should normalize Serials" do + let targetSchema = sql [i| + CREATE TABLE testserial ( + testcol SERIAL NOT NULL + ); + |] + let actualSchema = sql [i| + CREATE TABLE public.testserial ( + testcol int NOT NULL + ); + + CREATE SEQUENCE public.testserial_testcol_seq + START WITH 1 + INCREMENT BY 1 + NO MINVALUE + NO MAXVALUE + CACHE 1; + + ALTER SEQUENCE public.testserial_testcol_seq OWNED BY public.testserial.testcol; + ALTER TABLE ONLY public.testserial ALTER COLUMN testcol SET DEFAULT nextval('public.testserial_testcol_seq'::regclass); + |] + + diffSchemas targetSchema actualSchema `shouldBe` [] + sql :: Text -> [Statement] sql code = case Megaparsec.runParser Parser.parseDDL "" code of