@@ -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
0 commit comments