Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
chore: parse typeorm (#10803)
Danny Xu54b8fa1efa63
diff --git a/backend/plugin/parser/typeorm/parser.go b/backend/plugin/parser/typeorm/parser.gonew file mode 100644index 0000000000..9f64ea77e4--- /dev/null+++ b/backend/plugin/parser/typeorm/parser.go@@ -0,0 +1,41 @@+// Package typeorm defines the sql extractor for typeorm migration scripts.+package typeorm++import (+ "regexp"+ "strings"++ "github.com/pkg/errors"+)++// Parse parses the typescript migration file.+func Parse(s string) ([]string, error) {+ upIndex := strings.Index(s, "public async up(")+ if upIndex < 0 {+ return nil, errors.Errorf("unable to find up() function")+ }+ s = s[upIndex:]+ downIndex := strings.Index(s, "public async down(")+ if downIndex >= 0 {+ s = s[:downIndex]+ }+ pattern := `await queryRunner\.query\(\s*(.+?)\s*[,]?\s*\);`++ // Compile the regular expression+ re := regexp.MustCompile(pattern)++ // Find all matched statements+ matches := re.FindAllStringSubmatch(s, -1)+ var stmts []string+ for _, m := range matches {+ stmt := m[1]+ if len(stmt) <= 2 {+ return nil, errors.Errorf("invalid statement %s", stmt)+ }+ if stmt[0] != stmt[len(stmt)-1] {+ return nil, errors.Errorf("invalid statement %s", stmt)+ }+ stmts = append(stmts, stmt[1:len(stmt)-1])+ }+ return stmts, nil+}diff --git a/backend/plugin/parser/typeorm/parser_test.go b/backend/plugin/parser/typeorm/parser_test.gonew file mode 100644index 0000000000..19efc5b357--- /dev/null+++ b/backend/plugin/parser/typeorm/parser_test.go@@ -0,0 +1,27 @@+package typeorm++import (+ "io"+ "os"+ "testing"++ "github.com/stretchr/testify/require"+)++func TestParse(t *testing.T) {+ want := []string{+ `CREATE TABLE "core"."refreshToken" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "userId" uuid NOT NULL, "expiresAt" TIMESTAMP WITH TIME ZONE NOT NULL, "deletedAt" TIMESTAMP WITH TIME ZONE, "revokedAt" TIMESTAMP WITH TIME ZONE, "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), CONSTRAINT "PK_7d8bee0204106019488c4c50ffa" PRIMARY KEY ("id"))`,+ `CREATE TABLE "core"."workspace" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "domainName" character varying, "displayName" character varying, "logo" character varying, "inviteHash" character varying, "deletedAt" TIMESTAMP, "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), CONSTRAINT "PK_098656ae401f3e1a4586f47fd8e" PRIMARY KEY ("id"))`,+ `CREATE TABLE "core"."user" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "firstName" character varying NOT NULL DEFAULT '', "lastName" character varying NOT NULL DEFAULT '', "email" character varying NOT NULL, "emailVerified" boolean NOT NULL DEFAULT false, "disabled" boolean NOT NULL DEFAULT false, "passwordHash" character varying, "canImpersonate" boolean NOT NULL DEFAULT false, "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deletedAt" TIMESTAMP, "defaultWorkspaceId" uuid, CONSTRAINT "PK_a3ffb1c0c8416b9fc6f907b7433" PRIMARY KEY ("id"))`,+ `ALTER TABLE "core"."refreshToken" ADD CONSTRAINT "FK_610102b60fea1455310ccd299de" FOREIGN KEY ("userId") REFERENCES "core"."user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,+ `ALTER TABLE "core"."user" ADD CONSTRAINT "FK_5d77e050eabd28d203b301235a7" FOREIGN KEY ("defaultWorkspaceId") REFERENCES "core"."workspace"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,+ }+ f, err := os.Open("test-data/core_table.ts")+ require.NoError(t, err)+ defer f.Close()+ bytes, err := io.ReadAll(f)+ require.NoError(t, err)+ got, err := Parse(string(bytes))+ require.NoError(t, err)+ require.Equal(t, want, got)+}diff --git a/backend/plugin/parser/typeorm/test-data/core_table.ts b/backend/plugin/parser/typeorm/test-data/core_table.tsnew file mode 100644index 0000000000..1a860cc3e5--- /dev/null+++ b/backend/plugin/parser/typeorm/test-data/core_table.ts@@ -0,0 +1,35 @@+import { MigrationInterface, QueryRunner } from 'typeorm';++export class SetupCoreTables implements MigrationInterface {+ name = 'SetupCoreTables1700324860820';++ public async up(queryRunner: QueryRunner): Promise<void> {+ await queryRunner.query(+ `CREATE TABLE "core"."refreshToken" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "userId" uuid NOT NULL, "expiresAt" TIMESTAMP WITH TIME ZONE NOT NULL, "deletedAt" TIMESTAMP WITH TIME ZONE, "revokedAt" TIMESTAMP WITH TIME ZONE, "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), CONSTRAINT "PK_7d8bee0204106019488c4c50ffa" PRIMARY KEY ("id"))`,+ );+ await queryRunner.query(+ `CREATE TABLE "core"."workspace" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "domainName" character varying, "displayName" character varying, "logo" character varying, "inviteHash" character varying, "deletedAt" TIMESTAMP, "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), CONSTRAINT "PK_098656ae401f3e1a4586f47fd8e" PRIMARY KEY ("id"))`,+ );+ await queryRunner.query(+ `CREATE TABLE "core"."user" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "firstName" character varying NOT NULL DEFAULT '', "lastName" character varying NOT NULL DEFAULT '', "email" character varying NOT NULL, "emailVerified" boolean NOT NULL DEFAULT false, "disabled" boolean NOT NULL DEFAULT false, "passwordHash" character varying, "canImpersonate" boolean NOT NULL DEFAULT false, "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updatedAt" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "deletedAt" TIMESTAMP, "defaultWorkspaceId" uuid, CONSTRAINT "PK_a3ffb1c0c8416b9fc6f907b7433" PRIMARY KEY ("id"))`,+ );+ await queryRunner.query(+ `ALTER TABLE "core"."refreshToken" ADD CONSTRAINT "FK_610102b60fea1455310ccd299de" FOREIGN KEY ("userId") REFERENCES "core"."user"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,+ );+ await queryRunner.query(+ `ALTER TABLE "core"."user" ADD CONSTRAINT "FK_5d77e050eabd28d203b301235a7" FOREIGN KEY ("defaultWorkspaceId") REFERENCES "core"."workspace"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`,+ );+ }++ public async down(queryRunner: QueryRunner): Promise<void> {+ await queryRunner.query(+ `ALTER TABLE "core"."user" DROP CONSTRAINT "FK_5d77e050eabd28d203b301235a7"`,+ );+ await queryRunner.query(+ `ALTER TABLE "core"."refreshToken" DROP CONSTRAINT "FK_610102b60fea1455310ccd299de"`,+ );+ await queryRunner.query(`DROP TABLE "core"."user"`);+ await queryRunner.query(`DROP TABLE "core"."workspace"`);+ await queryRunner.query(`DROP TABLE "core"."refreshToken"`);+ }+}\ No newline at end of file