Skip to content

Commit

Permalink
Fixed migrations not normalising SERIAL and BIGSERIAL columns
Browse files Browse the repository at this point in the history
  • Loading branch information
mpscholten committed Jan 11, 2022
1 parent e3a5c8f commit 9790756
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
2 changes: 2 additions & 0 deletions IHP/IDE/CodeGen/MigrationGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
48 changes: 48 additions & 0 deletions Test/IDE/CodeGeneration/MigrationGenerator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9790756

Please sign in to comment.