Skip to content

Open-source manufacturing ERP, MES and QMS. Quoting, MRP, inventory, shop floor, quality and lot/serial traceability on one Postgres schema, with a REST API and MCP server. Self-host or use Carbon Cloud.

rework migration for contact import

Brad Barbin5ab2ad866204
diff --git a/packages/database/src/swagger-docs-schema.ts b/packages/database/src/swagger-docs-schema.ts
index 786bbaae90..163db3673c 100644
--- a/packages/database/src/swagger-docs-schema.ts
+++ b/packages/database/src/swagger-docs-schema.ts
@@ -10095,9 +10095,6 @@ export default {
{
$ref: "#/parameters/rowFilter.contact.lastName",
},
- {
- $ref: "#/parameters/rowFilter.contact.fullName",
- },
{
$ref: "#/parameters/rowFilter.contact.email",
},
@@ -10122,6 +10119,12 @@ export default {
{
$ref: "#/parameters/rowFilter.contact.companyId",
},
+ {
+ $ref: "#/parameters/rowFilter.contact.externalId",
+ },
+ {
+ $ref: "#/parameters/rowFilter.contact.fullName",
+ },
{
$ref: "#/parameters/select",
},
@@ -10190,9 +10193,6 @@ export default {
{
$ref: "#/parameters/rowFilter.contact.lastName",
},
- {
- $ref: "#/parameters/rowFilter.contact.fullName",
- },
{
$ref: "#/parameters/rowFilter.contact.email",
},
@@ -10217,6 +10217,12 @@ export default {
{
$ref: "#/parameters/rowFilter.contact.companyId",
},
+ {
+ $ref: "#/parameters/rowFilter.contact.externalId",
+ },
+ {
+ $ref: "#/parameters/rowFilter.contact.fullName",
+ },
{
$ref: "#/parameters/preferReturn",
},
@@ -10239,9 +10245,6 @@ export default {
{
$ref: "#/parameters/rowFilter.contact.lastName",
},
- {
- $ref: "#/parameters/rowFilter.contact.fullName",
- },
{
$ref: "#/parameters/rowFilter.contact.email",
},
@@ -10266,6 +10269,12 @@ export default {
{
$ref: "#/parameters/rowFilter.contact.companyId",
},
+ {
+ $ref: "#/parameters/rowFilter.contact.externalId",
+ },
+ {
+ $ref: "#/parameters/rowFilter.contact.fullName",
+ },
{
$ref: "#/parameters/body.contact",
},
@@ -19173,6 +19182,9 @@ export default {
{
$ref: "#/parameters/rowFilter.contractors.supplierName",
},
+ {
+ $ref: "#/parameters/rowFilter.contractors.fullName",
+ },
{
$ref: "#/parameters/rowFilter.contractors.firstName",
},
@@ -39356,6 +39368,11 @@ export default {
"Purchase Invoice",
"Document",
"Sales RFQ",
+ "Service",
+ "Tool",
+ "Consumable",
+ "Material",
+ "Fixture",
],
format: 'public."searchEntity"',
type: "string",
@@ -40716,7 +40733,7 @@ export default {
type: "object",
},
contact: {
- required: ["id", "firstName", "lastName", "email", "companyId"],
+ required: ["id", "email", "companyId"],
properties: {
id: {
default: "public.xid()",
@@ -40732,10 +40749,6 @@ export default {
format: "text",
type: "string",
},
- fullName: {
- format: "text",
- type: "string",
- },
email: {
format: "text",
type: "string",
@@ -40770,6 +40783,13 @@ export default {
format: "text",
type: "string",
},
+ externalId: {
+ format: "jsonb",
+ },
+ fullName: {
+ format: "text",
+ type: "string",
+ },
},
type: "object",
},
@@ -44841,6 +44861,10 @@ export default {
format: "text",
type: "string",
},
+ fullName: {
+ format: "text",
+ type: "string",
+ },
firstName: {
format: "text",
type: "string",
@@ -47209,7 +47233,7 @@ export default {
properties: {
id: {
description:
- "Note:\nThis is a Foreign Key to `supplierLocation.id`.<fk table='supplierLocation' column='id'/>",
+ "Note:\nThis is a Primary Key.<pk/>\nThis is a Foreign Key to `supplierLocation.id`.<fk table='supplierLocation' column='id'/>",
format: "text",
type: "string",
},
@@ -47258,7 +47282,7 @@ export default {
},
supplierLocationId: {
description:
- "Note:\nThis is a Primary Key.<pk/>\nThis is a Foreign Key to `supplierLocation.id`.<fk table='supplierLocation' column='id'/>",
+ "Note:\nThis is a Foreign Key to `supplierLocation.id`.<fk table='supplierLocation' column='id'/>",
format: "text",
type: "string",
},
@@ -57687,13 +57711,6 @@ export default {
in: "query",
type: "string",
},
- "rowFilter.contact.fullName": {
- name: "fullName",
- required: false,
- format: "text",
- in: "query",
- type: "string",
- },
"rowFilter.contact.email": {
name: "email",
required: false,
@@ -57750,6 +57767,20 @@ export default {
in: "query",
type: "string",
},
+ "rowFilter.contact.externalId": {
+ name: "externalId",
+ required: false,
+ format: "jsonb",
+ in: "query",
+ type: "string",
+ },
+ "rowFilter.contact.fullName": {
+ name: "fullName",
+ required: false,
+ format: "text",
+ in: "query",
+ type: "string",
+ },
"body.userDefaults": {
name: "userDefaults",
description: "userDefaults",
@@ -63074,6 +63105,13 @@ export default {
in: "query",
type: "string",
},
+ "rowFilter.contractors.fullName": {
+ name: "fullName",
+ required: false,
+ format: "text",
+ in: "query",
+ type: "string",
+ },
"rowFilter.contractors.firstName": {
name: "firstName",
required: false,
diff --git a/packages/database/supabase/migrations/20241015022612_contact-import.sql b/packages/database/supabase/migrations/20241015022612_contact-import.sql
index 23fa7d6ab0..a5db922c40 100644
--- a/packages/database/supabase/migrations/20241015022612_contact-import.sql
+++ b/packages/database/supabase/migrations/20241015022612_contact-import.sql
@@ -1,21 +1,50 @@
-ALTER TABLE "contact" ADD COLUMN "externalId" JSONB;
+ALTER TABLE "contact" ADD COLUMN IF NOT EXISTS "externalId" JSONB;
--- Make firstName and lastName nullable
-ALTER TABLE "contact" ALTER COLUMN "firstName" DROP NOT NULL;
-ALTER TABLE "contact" ALTER COLUMN "lastName" DROP NOT NULL;
+-- Check if firstName and lastName are already nullable
+DO $$
+BEGIN
+ IF EXISTS (
+ SELECT 1
+ FROM information_schema.columns
+ WHERE table_name = 'contact'
+ AND column_name IN ('firstName', 'lastName')
+ AND is_nullable = 'NO'
+ ) THEN
+ -- Make firstName and lastName nullable if they are not already
+ ALTER TABLE "contact" ALTER COLUMN "firstName" DROP NOT NULL;
+ ALTER TABLE "contact" ALTER COLUMN "lastName" DROP NOT NULL;
+ END IF;
+END $$;
--- Update the fullName generation to use email if firstName or lastName is null
-ALTER TABLE "contact" DROP COLUMN "fullName";
-ALTER TABLE "contact" ADD COLUMN "fullName" TEXT GENERATED ALWAYS AS (
- CASE
- WHEN "firstName" IS NULL AND "lastName" IS NULL THEN "email"
- WHEN "firstName" IS NULL THEN "lastName"
- WHEN "lastName" IS NULL THEN "firstName"
- ELSE "firstName" || ' ' || "lastName"
- END
-) STORED;
+-- Check if fullName column exists and update it if necessary
+DO $$
+BEGIN
+ IF EXISTS (
+ SELECT 1
+ FROM information_schema.columns
+ WHERE table_name = 'contact' AND column_name = 'fullName'
+ ) THEN
+ -- Drop the view that depends on fullName
+ DROP VIEW IF EXISTS "contractors";
+
+ -- Drop existing fullName column
+ ALTER TABLE "contact" DROP COLUMN "fullName";
+ END IF;
+ -- Add updated fullName column
+ ALTER TABLE "contact" ADD COLUMN "fullName" TEXT GENERATED ALWAYS AS (
+ CASE
+ WHEN "firstName" IS NULL AND "lastName" IS NULL THEN "email"
+ WHEN "firstName" IS NULL THEN "lastName"
+ WHEN "lastName" IS NULL THEN "firstName"
+ ELSE "firstName" || ' ' || "lastName"
+ END
+ ) STORED;
+END $$;
+
+DROP VIEW IF EXISTS "contractors";
+-- Recreate the contractors view
CREATE OR REPLACE VIEW "contractors" WITH (security_invoker = on) AS
SELECT
p.id AS "supplierContactId",