11using System ;
22using System . Text ;
33using EfCore . Ydb . Metadata . Internal ;
4+ using Microsoft . EntityFrameworkCore . Diagnostics ;
45using Microsoft . EntityFrameworkCore . Metadata ;
56using Microsoft . EntityFrameworkCore . Migrations ;
67using Microsoft . EntityFrameworkCore . Migrations . Operations ;
8+ using Ydb . Sdk . Ado ;
79
810namespace EfCore . Ydb . Migrations ;
911
@@ -43,7 +45,7 @@ protected override void Generate(
4345 }
4446
4547 builder . Append ( ";" ) ;
46- EndStatement ( builder , suppressTransaction : true ) ;
48+ EndStatementSuppressTransaction ( builder ) ;
4749 }
4850
4951 protected override void ColumnDefinition (
@@ -69,10 +71,14 @@ MigrationCommandListBuilder builder
6971 } ;
7072 }
7173
74+ if ( operation . ComputedColumnSql is not null )
75+ {
76+ throw new NotSupportedException ( "Computed/generated columns aren't supported in YDB" ) ;
77+ }
78+
7279 builder
73- . Append ( Dependencies . SqlGenerationHelper . DelimitIdentifier ( name ) )
80+ . Append ( DelimitIdentifier ( name ) )
7481 . Append ( " " )
75- // TODO: Add DEFAULT logic somewhere here
7682 . Append ( columnType )
7783 . Append ( operation . IsNullable ? string . Empty : " NOT NULL" ) ;
7884 }
@@ -120,7 +126,7 @@ protected override void Generate(RenameTableOperation operation, IModel? model,
120126 . Append ( " RENAME TO " )
121127 . Append ( DelimitIdentifier ( operation . NewName , operation . Schema ) )
122128 . AppendLine ( ";" ) ;
123- EndStatement ( builder ) ;
129+ EndStatementSuppressTransaction ( builder ) ;
124130 }
125131
126132 protected override void Generate (
@@ -140,7 +146,7 @@ protected override void Generate(
140146
141147 if ( terminate )
142148 {
143- EndStatement ( builder , suppressTransaction : false ) ;
149+ EndStatement ( builder ) ;
144150 }
145151 }
146152
@@ -156,7 +162,133 @@ protected override void Generate(DeleteDataOperation operation, IModel? model, M
156162 }
157163
158164 builder . Append ( sqlBuilder . ToString ( ) ) ;
159- EndStatement ( builder , suppressTransaction : false ) ;
165+ EndStatement ( builder ) ;
166+ }
167+
168+ protected override void Generate (
169+ DropTableOperation operation ,
170+ IModel ? model ,
171+ MigrationCommandListBuilder builder ,
172+ bool terminate = true )
173+ {
174+ builder . Append ( "DROP TABLE " )
175+ . Append ( DelimitIdentifier ( operation . Name , operation . Schema ) ) ;
176+ if ( ! terminate )
177+ return ;
178+ builder . AppendLine ( Dependencies . SqlGenerationHelper . StatementTerminator ) ;
179+ EndStatementSuppressTransaction ( builder ) ;
180+ }
181+
182+ protected override void Generate (
183+ AddColumnOperation operation ,
184+ IModel ? model ,
185+ MigrationCommandListBuilder builder ,
186+ bool terminate = true )
187+ {
188+ if ( operation [ "Relational:ColumnOrder" ] != null )
189+ Dependencies . MigrationsLogger . ColumnOrderIgnoredWarning ( operation ) ;
190+ builder . Append ( "ALTER TABLE " )
191+ . Append ( DelimitIdentifier ( operation . Table , operation . Schema ) )
192+ . Append ( " ADD " ) ;
193+ ColumnDefinition ( operation , model , builder ) ;
194+ if ( ! terminate )
195+ return ;
196+ builder . AppendLine ( Dependencies . SqlGenerationHelper . StatementTerminator ) ;
197+ EndStatementSuppressTransaction ( builder ) ;
198+ }
199+
200+ protected override void Generate (
201+ DropColumnOperation operation ,
202+ IModel ? model ,
203+ MigrationCommandListBuilder builder ,
204+ bool terminate = true )
205+ {
206+ builder . Append ( "ALTER TABLE " )
207+ . Append ( DelimitIdentifier ( operation . Table , operation . Schema ) )
208+ . Append ( " DROP COLUMN " )
209+ . Append ( DelimitIdentifier ( operation . Name ) ) ;
210+
211+ if ( ! terminate )
212+ return ;
213+ builder . AppendLine ( Dependencies . SqlGenerationHelper . StatementTerminator ) ;
214+ EndStatementSuppressTransaction ( builder ) ;
215+ }
216+
217+ protected override void Generate (
218+ CreateIndexOperation operation ,
219+ IModel ? model ,
220+ MigrationCommandListBuilder builder ,
221+ bool terminate = true
222+ )
223+ {
224+ builder . Append ( "ALTER TABLE " )
225+ . Append ( DelimitIdentifier ( operation . Table , operation . Schema ) )
226+ . Append ( " ADD INDEX " )
227+ . Append ( DelimitIdentifier ( operation . Name ) )
228+ . Append ( " GLOBAL " ) ;
229+
230+ // if (operation.IsUnique)
231+ // {
232+ // builder.Append("UNIQUE ");
233+ // }
234+
235+ if ( operation . IsDescending != null )
236+ {
237+ throw new NotSupportedException ( "Descending columns in the index aren't supported in YDB" ) ;
238+ }
239+
240+ builder . Append ( "SYNC ON (" )
241+ . Append ( ColumnList ( operation . Columns ) )
242+ . Append ( ")" ) ;
243+
244+ if ( ! terminate )
245+ return ;
246+ builder . AppendLine ( Dependencies . SqlGenerationHelper . StatementTerminator ) ;
247+ EndStatementSuppressTransaction ( builder ) ;
248+ }
249+
250+ protected override void Generate (
251+ DropIndexOperation operation ,
252+ IModel ? model ,
253+ MigrationCommandListBuilder builder ,
254+ bool terminate = true )
255+ {
256+ if ( operation . Table == null )
257+ {
258+ throw new YdbException ( "Table name must be specified for DROP INDEX in YDB" ) ;
259+ }
260+
261+ builder . Append ( "ALTER TABLE " )
262+ . Append ( DelimitIdentifier ( operation . Table , operation . Schema ) )
263+ . Append ( " DROP INDEX " )
264+ . Append ( DelimitIdentifier ( operation . Name ) ) ;
265+
266+ if ( ! terminate )
267+ return ;
268+ builder . AppendLine ( Dependencies . SqlGenerationHelper . StatementTerminator ) ;
269+ EndStatementSuppressTransaction ( builder ) ;
270+ }
271+
272+ protected override void Generate (
273+ RenameIndexOperation operation ,
274+ IModel ? model ,
275+ MigrationCommandListBuilder builder
276+ )
277+ {
278+ if ( operation . Table == null )
279+ {
280+ throw new YdbException ( "Table name must be specified for RENAME INDEX in YDB" ) ;
281+ }
282+
283+ builder . Append ( "ALTER TABLE " )
284+ . Append ( DelimitIdentifier ( operation . Table , operation . Schema ) )
285+ . Append ( " RENAME INDEX " )
286+ . Append ( DelimitIdentifier ( operation . Name ) )
287+ . Append ( " TO " )
288+ . Append ( DelimitIdentifier ( operation . NewName ) ) ;
289+
290+ builder . AppendLine ( Dependencies . SqlGenerationHelper . StatementTerminator ) ;
291+ EndStatementSuppressTransaction ( builder ) ;
160292 }
161293
162294 protected override void Generate ( UpdateDataOperation operation , IModel ? model , MigrationCommandListBuilder builder )
@@ -171,9 +303,30 @@ protected override void Generate(UpdateDataOperation operation, IModel? model, M
171303 }
172304
173305 builder . Append ( sqlBuilder . ToString ( ) ) ;
174- EndStatement ( builder , suppressTransaction : false ) ;
306+ EndStatement ( builder ) ;
307+ }
308+
309+ protected override void Generate (
310+ DropUniqueConstraintOperation operation ,
311+ IModel ? model ,
312+ MigrationCommandListBuilder builder
313+ )
314+ {
315+ builder . Append ( "ALTER TABLE " )
316+ . Append ( Dependencies . SqlGenerationHelper . DelimitIdentifier ( operation . Table , operation . Schema ) )
317+ . Append ( " DROP INDEX " )
318+ . Append ( Dependencies . SqlGenerationHelper . DelimitIdentifier ( operation . Name ) )
319+ . AppendLine ( Dependencies . SqlGenerationHelper . StatementTerminator ) ;
320+
321+ EndStatementSuppressTransaction ( builder ) ;
175322 }
176323
324+ protected override void Generate (
325+ DropCheckConstraintOperation operation ,
326+ IModel ? model ,
327+ MigrationCommandListBuilder builder
328+ ) => throw new NotSupportedException ( "Drop check constraint isn't supported in YDB" ) ;
329+
177330 protected override void Generate (
178331 DropForeignKeyOperation operation ,
179332 IModel ? model ,
@@ -231,37 +384,20 @@ protected override void ForeignKeyConstraint(AddForeignKeyOperation operation, I
231384 // Same comment about Foreign keys
232385 }
233386
234- protected override void CreateTableUniqueConstraints ( CreateTableOperation operation , IModel ? model ,
235- MigrationCommandListBuilder builder )
236- {
237- // We don't have unique constraints
238- }
239-
240- protected override void UniqueConstraint ( AddUniqueConstraintOperation operation , IModel ? model ,
241- MigrationCommandListBuilder builder )
242- {
243- // Same comment about Unique constraints
244- }
245-
246- protected override void Generate (
247- CreateIndexOperation operation ,
387+ protected override void UniqueConstraint (
388+ AddUniqueConstraintOperation operation ,
248389 IModel ? model ,
249- MigrationCommandListBuilder builder ,
250- bool terminate = true
251- )
252- {
253- // TODO: We do have Indexes!
254- // But they're not implemented yet. Ignoring indexes because otherwise table generation during tests will fail
255- }
256-
390+ MigrationCommandListBuilder builder
391+ ) => builder
392+ . Append ( "INDEX " )
393+ . Append ( Dependencies . SqlGenerationHelper . DelimitIdentifier ( operation . Name ) )
394+ . Append ( " GLOBAL UNIQUE SYNC ON (" )
395+ . Append ( ColumnList ( operation . Columns ) )
396+ . Append ( ")" ) ;
257397
258- // ReSharper disable once RedundantOverriddenMember
259- protected override void EndStatement (
260- MigrationCommandListBuilder builder ,
261- // ReSharper disable once OptionalParameterHierarchyMismatch
262- bool suppressTransaction = true
263- ) => base . EndStatement ( builder , suppressTransaction ) ;
398+ private void EndStatementSuppressTransaction ( MigrationCommandListBuilder builder ) =>
399+ base . EndStatement ( builder , true ) ;
264400
265- private string DelimitIdentifier ( string name , string ? schema )
401+ private string DelimitIdentifier ( string name , string ? schema = null )
266402 => Dependencies . SqlGenerationHelper . DelimitIdentifier ( name , schema ) ;
267403}
0 commit comments