Skip to content

Commit

Permalink
js: support postgres create enums statments
Browse files Browse the repository at this point in the history
  • Loading branch information
ronenlu committed Dec 18, 2023
1 parent 11c251c commit 25bf757
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 6 deletions.
14 changes: 14 additions & 0 deletions js/src/sequelize_schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,20 @@ const loadSequelizeModels = (dialect, ...models) => {
const attr = sequelize
.getQueryInterface()
.queryGenerator.attributesToSQL(def.getAttributes(), { ...def.options });
// create enum types for postgres
if (dialect === "postgres") {
for (const key in attr) {
if (!attr[key].startsWith("ENUM")) {
continue;
}
const enumValues = attr[key].substring(
attr[key].indexOf("("),
attr[key].lastIndexOf(")") + 1,
);
const enumName = `enum_${def.tableName}_${key}`;
sql += `CREATE TYPE "${enumName}" AS ENUM${enumValues};\n`;
}
}
sql +=
sequelize
.getQueryInterface()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CREATE TABLE `Recipes` (
`title` varchar(255) NOT NULL,
`description` text NOT NULL,
`instructions` text NOT NULL,
`meal` enum('breakfast','lunch','dinner','dessert') NULL DEFAULT "lunch",
`createdAt` datetime NOT NULL,
`updatedAt` datetime NOT NULL,
`deletedAt` datetime NULL,
Expand Down
4 changes: 2 additions & 2 deletions js/testdata/migrations/mysql/atlas.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
h1:iiEZ5BKOIIrn/v6gnOMLybPtmdE+CF9zgQGkBUNTSak=
20231217121351.sql h1:OloC5SnSg1SNwiBwdJ8/0JzGE+z1IJxrC7PZzCE52pM=
h1:cI6IUVRHfJbkk65AWg0ge7Xeb1RGs9oMJkszrVexwss=
20231218113756.sql h1:UGw8bFPavN0GrLFrN0nAl+P7VFaOX/a2yKZXq4P29fA=
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
-- Create enum type "enum_Recipes_meal"
CREATE TYPE "public"."enum_Recipes_meal" AS ENUM ('breakfast', 'lunch', 'dinner', 'dessert');
-- Create "Ingredients" table
CREATE TABLE "public"."Ingredients" (
"id" serial NOT NULL,
Expand All @@ -13,6 +15,7 @@ CREATE TABLE "public"."Recipes" (
"title" character varying(255) NOT NULL,
"description" text NOT NULL,
"instructions" text NOT NULL,
"meal" "public"."enum_Recipes_meal" NULL DEFAULT 'lunch',
"createdAt" timestamptz NOT NULL,
"updatedAt" timestamptz NOT NULL,
"deletedAt" timestamptz NULL,
Expand Down
4 changes: 2 additions & 2 deletions js/testdata/migrations/postgres/atlas.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
h1:uJ7wU+6p3ohypex+0pRKcsw86vEtzStoE6JJLatVoAo=
20231217121326.sql h1:d2jBf2rcjqt+l7WZhoeosIndYjndY0eAkBtcm4e8PcE=
h1:JCDQK9pnfdBoi3LjhfZWKQ0vmrl01yT+z/iI9z2xcO0=
20231218113750.sql h1:G5dZYzfZamTnaCw/gsxOz0ZtT2QuxwC+OwmeOaaKKbQ=
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ CREATE TABLE `Recipes` (
`title` varchar NOT NULL,
`description` text NOT NULL,
`instructions` text NOT NULL,
`meal` text NULL DEFAULT 'lunch',
`createdAt` datetime NOT NULL,
`updatedAt` datetime NOT NULL,
`deletedAt` datetime NULL
Expand Down
4 changes: 2 additions & 2 deletions js/testdata/migrations/sqlite/atlas.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
h1:BYAmffj+Yu0UriXgwsQxyg2oirRqrQYz2VS/GUE2oL4=
20231217121207.sql h1:fMjw2OFFzGMAzehqpEZjFdbgbHjyCBEwSiSG/rhNDek=
h1:XZPNKrbW25KYA84JijT+3mt1j7v061dL4noOCiKC1Ws=
20231218113759.sql h1:Xoq/BkJqpRZ05eyN12Ub/8SOTm/zHVyxelGCmBAtAl4=
5 changes: 5 additions & 0 deletions js/testdata/models/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ module.exports = function (sequelize, DataTypes) {
type: DataTypes.TEXT,
allowNull: false,
},
meal: {
type: DataTypes.ENUM,
values: ["breakfast", "lunch", "dinner", "dessert"],
defaultValue: "lunch",
},
},
{
paranoid: true,
Expand Down

0 comments on commit 25bf757

Please sign in to comment.