Skip to content

Commit 338df16

Browse files
sinkhahasinkhaha
andauthored
feat: add support for table comment in MySQL (#10017)
* feat: add table comment * feat: resolve conflict * feat: del auroraMysql comment * feat: add changeTableComment method for MySQL * feat: QueryRunner subclass add changeTableComment Implementation * oracle implement changeTableComment * add test * feat: loadTables support comment * feat: rename * feat: update mysql changeTableComment * fix: fix conflict * feat: add table comment * feat: resolve conflict * feat: del auroraMysql comment * feat: add changeTableComment method for MySQL * feat: QueryRunner subclass add changeTableComment Implementation * oracle implement changeTableComment * add test * feat: loadTables support comment * feat: rename * feat: update mysql changeTableComment --------- Co-authored-by: sinkhaha <[email protected]>
1 parent 15bc887 commit 338df16

File tree

22 files changed

+329
-3
lines changed

22 files changed

+329
-3
lines changed

src/decorator/entity/Entity.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ export function Entity(
4141
schema: options.schema ? options.schema : undefined,
4242
synchronize: options.synchronize,
4343
withoutRowid: options.withoutRowid,
44+
comment: options.comment ? options.comment : undefined,
4445
} as TableMetadataArgs)
4546
}
4647
}

src/decorator/options/EntityOptions.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,9 @@ export interface EntityOptions {
4646
* @see https://www.sqlite.org/withoutrowid.html.
4747
*/
4848
withoutRowid?: boolean
49+
50+
/**
51+
* Table comment. Not supported by all database types.
52+
*/
53+
comment?: string
4954
}

src/driver/aurora-mysql/AuroraMysqlQueryRunner.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2822,4 +2822,16 @@ export class AuroraMysqlQueryRunner
28222822

28232823
return false
28242824
}
2825+
2826+
/**
2827+
* Change table comment.
2828+
*/
2829+
changeTableComment(
2830+
tableOrName: Table | string,
2831+
comment?: string,
2832+
): Promise<void> {
2833+
throw new TypeORMError(
2834+
`aurora-mysql driver does not support change table comment.`,
2835+
)
2836+
}
28252837
}

src/driver/aurora-postgres/AuroraPostgresQueryRunner.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import { AuroraPostgresDriver } from "./AuroraPostgresDriver"
66
import { PostgresQueryRunner } from "../postgres/PostgresQueryRunner"
77
import { ReplicationMode } from "../types/ReplicationMode"
88
import { QueryResult } from "../../query-runner/QueryResult"
9+
import { Table } from "../../schema-builder/table/Table"
10+
import { TypeORMError } from "../../error"
911

1012
class PostgresQueryRunnerWrapper extends PostgresQueryRunner {
1113
driver: any
@@ -194,4 +196,16 @@ export class AuroraPostgresQueryRunner
194196

195197
return result
196198
}
199+
200+
/**
201+
* Change table comment.
202+
*/
203+
changeTableComment(
204+
tableOrName: Table | string,
205+
comment?: string,
206+
): Promise<void> {
207+
throw new TypeORMError(
208+
`aurora-postgres driver does not support change comment.`,
209+
)
210+
}
197211
}

src/driver/cockroachdb/CockroachQueryRunner.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4211,4 +4211,15 @@ export class CockroachQueryRunner
42114211

42124212
return c
42134213
}
4214+
/**
4215+
* Change table comment.
4216+
*/
4217+
changeTableComment(
4218+
tableOrName: Table | string,
4219+
comment?: string,
4220+
): Promise<void> {
4221+
throw new TypeORMError(
4222+
`cockroachdb driver does not support change table comment.`,
4223+
)
4224+
}
42144225
}

src/driver/mongodb/MongoQueryRunner.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1263,4 +1263,16 @@ export class MongoQueryRunner implements QueryRunner {
12631263
.db(this.connection.driver.database!)
12641264
.collection(collectionName)
12651265
}
1266+
1267+
/**
1268+
* Change table comment.
1269+
*/
1270+
changeTableComment(
1271+
tableOrName: Table | string,
1272+
comment?: string,
1273+
): Promise<void> {
1274+
throw new TypeORMError(
1275+
`mongodb driver does not support change table comment.`,
1276+
)
1277+
}
12661278
}

src/driver/mysql/MysqlQueryRunner.ts

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -746,6 +746,49 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
746746
this.replaceCachedTable(oldTable, newTable)
747747
}
748748

749+
/**
750+
* Change table comment.
751+
*/
752+
async changeTableComment(
753+
tableOrName: Table | string,
754+
newComment?: string,
755+
): Promise<void> {
756+
const upQueries: Query[] = []
757+
const downQueries: Query[] = []
758+
759+
const table = InstanceChecker.isTable(tableOrName)
760+
? tableOrName
761+
: await this.getCachedTable(tableOrName)
762+
763+
newComment = this.escapeComment(newComment)
764+
const comment = this.escapeComment(table.comment)
765+
766+
if (newComment === comment) {
767+
return
768+
}
769+
770+
const newTable = table.clone()
771+
772+
upQueries.push(
773+
new Query(
774+
`ALTER TABLE ${this.escapePath(
775+
newTable,
776+
)} COMMENT ${newComment}`,
777+
),
778+
)
779+
downQueries.push(
780+
new Query(
781+
`ALTER TABLE ${this.escapePath(table)} COMMENT ${comment}`,
782+
),
783+
)
784+
785+
await this.executeQueries(upQueries, downQueries)
786+
787+
// change table comment and replace it in cached tabled;
788+
table.comment = newTable.comment
789+
this.replaceCachedTable(table, newTable)
790+
}
791+
749792
/**
750793
* Creates a new column from the column in the table.
751794
*/
@@ -2346,11 +2389,15 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
23462389
// will cause the query to not hit the optimizations & do full scans. This is why
23472390
// a number of queries below do `UNION`s of single `WHERE` clauses.
23482391

2349-
const dbTables: { TABLE_SCHEMA: string; TABLE_NAME: string }[] = []
2392+
const dbTables: {
2393+
TABLE_SCHEMA: string
2394+
TABLE_NAME: string
2395+
TABLE_COMMENT: string
2396+
}[] = []
23502397

23512398
if (!tableNames) {
23522399
// Since we don't have any of this data we have to do a scan
2353-
const tablesSql = `SELECT \`TABLE_SCHEMA\`, \`TABLE_NAME\` FROM \`INFORMATION_SCHEMA\`.\`TABLES\``
2400+
const tablesSql = `SELECT \`TABLE_SCHEMA\`, \`TABLE_NAME\`, \`TABLE_COMMENT\` FROM \`INFORMATION_SCHEMA\`.\`TABLES\``
23542401

23552402
dbTables.push(...(await this.query(tablesSql)))
23562403
} else {
@@ -2367,7 +2414,7 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
23672414
database = currentDatabase
23682415
}
23692416

2370-
return `SELECT \`TABLE_SCHEMA\`, \`TABLE_NAME\` FROM \`INFORMATION_SCHEMA\`.\`TABLES\` WHERE \`TABLE_SCHEMA\` = '${database}' AND \`TABLE_NAME\` = '${name}'`
2417+
return `SELECT \`TABLE_SCHEMA\`, \`TABLE_NAME\`, \`TABLE_COMMENT\` FROM \`INFORMATION_SCHEMA\`.\`TABLES\` WHERE \`TABLE_SCHEMA\` = '${database}' AND \`TABLE_NAME\` = '${name}'`
23712418
})
23722419
.join(" UNION ")
23732420

@@ -2925,6 +2972,8 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
29252972
})
29262973
})
29272974

2975+
table.comment = dbTable["TABLE_COMMENT"]
2976+
29282977
return table
29292978
}),
29302979
)
@@ -3058,6 +3107,10 @@ export class MysqlQueryRunner extends BaseQueryRunner implements QueryRunner {
30583107

30593108
sql += `) ENGINE=${table.engine || "InnoDB"}`
30603109

3110+
if (table.comment) {
3111+
sql += ` COMMENT="${table.comment}"`
3112+
}
3113+
30613114
return new Query(sql)
30623115
}
30633116

src/driver/oracle/OracleQueryRunner.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3181,4 +3181,16 @@ export class OracleQueryRunner extends BaseQueryRunner implements QueryRunner {
31813181

31823182
return `"${tableName}"`
31833183
}
3184+
3185+
/**
3186+
* Change table comment.
3187+
*/
3188+
changeTableComment(
3189+
tableOrName: Table | string,
3190+
comment?: string,
3191+
): Promise<void> {
3192+
throw new TypeORMError(
3193+
`oracle driver does not support change table comment.`,
3194+
)
3195+
}
31843196
}

src/driver/postgres/PostgresQueryRunner.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4717,4 +4717,16 @@ export class PostgresQueryRunner
47174717
)
47184718
return result.length ? true : false
47194719
}
4720+
4721+
/**
4722+
* Change table comment.
4723+
*/
4724+
changeTableComment(
4725+
tableOrName: Table | string,
4726+
comment?: string,
4727+
): Promise<void> {
4728+
throw new TypeORMError(
4729+
`postgres driver does not support change table comment.`,
4730+
)
4731+
}
47204732
}

src/driver/sap/SapQueryRunner.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3354,4 +3354,16 @@ export class SapQueryRunner extends BaseQueryRunner implements QueryRunner {
33543354

33553355
return c
33563356
}
3357+
3358+
/**
3359+
* Change table comment.
3360+
*/
3361+
changeTableComment(
3362+
tableOrName: Table | string,
3363+
comment?: string,
3364+
): Promise<void> {
3365+
throw new TypeORMError(
3366+
`spa driver does not support change table comment.`,
3367+
)
3368+
}
33573369
}

0 commit comments

Comments
 (0)