@@ -122,23 +122,38 @@ protected override string GetComparisonDDLStatements(params TypeSchemaAndName[]
122122 }
123123 }
124124
125- //创建表
126125 var createTableName = _commonUtils . QuoteSqlName ( tbname [ 0 ] , tbname [ 1 ] ) ;
127- sb . Append ( "CREATE TABLE IF NOT EXISTS " ) . Append ( createTableName ) . Append ( " ( " ) ;
128- foreach ( var tbcol in tb . ColumnsByPosition )
126+ var tableExists = LocalExecuteScalar ( tbname [ 0 ] , _commonUtils . FormatSql ( " select first 1 1 from systables where tabname={0}" , new [ ] { tbname [ 1 ] } ) ) != null ;
127+ if ( tableExists == false )
129128 {
130- sb . Append ( " \r \n " ) . Append ( _commonUtils . QuoteSqlName ( tbcol . Attribute . Name ) ) . Append ( " " ) . Append ( tbcol . Attribute . DbType ) ;
131- sb . Append ( "," ) ;
129+ // 创建表
130+ sb . Append ( "CREATE TABLE IF NOT EXISTS " ) . Append ( createTableName ) . Append ( " ( " ) ;
131+ foreach ( var tbcol in tb . ColumnsByPosition )
132+ {
133+ sb . Append ( " \r \n " ) . Append ( _commonUtils . QuoteSqlName ( tbcol . Attribute . Name ) ) . Append ( " " ) . Append ( tbcol . Attribute . DbType ) ;
134+ sb . Append ( "," ) ;
135+ }
136+ if ( tb . Primarys . Any ( ) )
137+ {
138+ sb . Append ( " \r \n PRIMARY KEY (" ) ;
139+ foreach ( var tbcol in tb . Primarys ) sb . Append ( _commonUtils . QuoteSqlName ( tbcol . Attribute . Name ) ) . Append ( ", " ) ;
140+ sb . Remove ( sb . Length - 2 , 2 ) . Append ( ")," ) ;
141+ }
142+ sb . Remove ( sb . Length - 1 , 1 ) ;
143+ sb . Append ( "\r \n )" ) ;
144+ sb . Append ( ";\r \n " ) ;
132145 }
133- if ( tb . Primarys . Any ( ) )
146+ else
134147 {
135- sb . Append ( " \r \n PRIMARY KEY (" ) ;
136- foreach ( var tbcol in tb . Primarys ) sb . Append ( _commonUtils . QuoteSqlName ( tbcol . Attribute . Name ) ) . Append ( ", " ) ;
137- sb . Remove ( sb . Length - 2 , 2 ) . Append ( ")," ) ;
148+ var ds = _orm . Ado . ExecuteArray ( CommandType . Text , _commonUtils . FormatSql ( " select b.colname from syscolumns b where b.tabid=(select a.tabid from systables a where a.tabname={0})" , new [ ] { tbname [ 1 ] } ) ) ;
149+ var dbcols = new HashSet < string > ( ds . Select ( a => string . Concat ( a [ 0 ] ) ) , StringComparer . CurrentCultureIgnoreCase ) ;
150+ // 新增列
151+ foreach ( var tbcol in tb . ColumnsByPosition )
152+ {
153+ if ( dbcols . Contains ( tbcol . Attribute . Name ) == false )
154+ sb . Append ( "ALTER TABLE " ) . Append ( createTableName ) . Append ( " ADD " ) . Append ( _commonUtils . QuoteSqlName ( tbcol . Attribute . Name ) ) . Append ( " " ) . Append ( tbcol . Attribute . DbType ) . Append ( ";\r \n " ) ;
155+ }
138156 }
139- sb . Remove ( sb . Length - 1 , 1 ) ;
140- sb . Append ( "\r \n )" ) ;
141- sb . Append ( ";\r \n " ) ;
142157 //创建表的索引
143158 foreach ( var uk in tb . Indexes )
144159 {
@@ -152,7 +167,7 @@ protected override string GetComparisonDDLStatements(params TypeSchemaAndName[]
152167 }
153168 sb . Remove ( sb . Length - 2 , 2 ) . Append ( ");\r \n " ) ;
154169 }
155- //备注
170+ // 备注
156171 foreach ( var tbcol in tb . ColumnsByPosition )
157172 {
158173 if ( string . IsNullOrEmpty ( tbcol . Comment ) == false )
@@ -225,4 +240,4 @@ public override int ExecuteDDLStatements(string ddl)
225240 return affrows ;
226241 }
227242 }
228- }
243+ }
0 commit comments