├── .github └── workflows │ └── main.yml ├── .gitignore ├── .vscode └── settings.json ├── AutomaticMigrationConsole ├── AutomaticMigrationConsole.csproj ├── Models │ ├── Account.cs │ ├── AccountEvent.cs │ ├── CalendarEvent.cs │ ├── Config.cs │ ├── ERPContext.cs │ ├── Order.cs │ ├── Product.cs │ ├── Talent.cs │ └── TalentEvent.cs └── Program.cs ├── LICENSE ├── NeuroSpeech.EFCoreAutomaticMigration.sln ├── NeuroSpeech.EFCoreAutomaticMigration ├── Column.cs ├── Columns.cs ├── DbColumnInfo.cs ├── DbIndex.cs ├── DbTableInfo.cs ├── IMigrationEvents.cs ├── IdentityExtensions.cs ├── Index.cs ├── MigrationEventList.cs ├── MigrationEvents.cs ├── ModelMigration.cs ├── ModelMigrationBase.cs ├── NeuroSpeech.EFCoreAutomaticMigration.csproj ├── OldName.cs ├── PropertyExtensions.cs ├── Scripts.cs ├── SqlIndexEx.cs ├── SqlRowSet.cs ├── SqlServerMigrationHelper.cs └── StringExtensions.cs ├── PostGreSql ├── NeuroSpeech.EFCoreAutomaticMigration.PostGreSql │ ├── MigrationHelperExtensions.cs │ ├── NeuroSpeech.EFCoreAutomaticMigration.PostGreSql.csproj │ ├── PostGreSqlMigration.cs │ ├── PostGreSqlServerMigrationHelper.cs │ └── Scripts.cs └── PostGreSqlMigrationConsole │ ├── Models │ ├── Account.cs │ ├── Config.cs │ ├── ERPContext.cs │ └── Product.cs │ ├── PostGreSqlAutomaticMigrationConsole.csproj │ └── Program.cs ├── README.md └── version.json /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: .NET 2 | 3 | on: 4 | push: 5 | branches: [ main ] 6 | pull_request: 7 | branches: [ main ] 8 | 9 | jobs: 10 | build: 11 | 12 | runs-on: ubuntu-latest 13 | 14 | steps: 15 | - uses: actions/checkout@v2 16 | with: 17 | fetch-depth: 0 18 | - name: Setup .NET 19 | uses: actions/setup-dotnet@v1 20 | with: 21 | dotnet-version: 5.0.x 22 | - name: Restore dependencies 23 | run: dotnet restore 24 | - name: Build 25 | run: dotnet build --no-restore 26 | - name: Test 27 | run: dotnet test --no-build --verbosity normal 28 | - name: Pack 29 | run: dotnet pack --no-restore 30 | - name: Publish Live 31 | run: dotnet nuget push **/*.nupkg -k ${NUGET_TOKEN} -s https://api.nuget.org/v3/index.json -n true 32 | env: 33 | NUGET_TOKEN: ${{secrets.PUBLIC_NUGET_TOKEN}} 34 | 35 | - name: Publish Proget 36 | run: dotnet nuget push **/*.nupkg -k ${NUGET_KEY} -s ${NUGET_SOURCE} -n true 37 | env: 38 | NUGET_SOURCE: ${{secrets.PROGET_NUGET}} 39 | NUGET_KEY: ${{secrets.PROGET_NUGET_TOKEN}} 40 | 41 | 42 | - name: Create tag 43 | uses: actions/github-script@v6 44 | with: 45 | script: | 46 | const { NBGV_SemVer2 } = process.env 47 | github.rest.git.createRef({ 48 | owner: context.repo.owner, 49 | repo: context.repo.repo, 50 | ref: `refs/tags/v${NBGV_SemVer2}`, 51 | sha: context.sha 52 | }) 53 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Aa][Rr][Mm]/ 27 | [Aa][Rr][Mm]64/ 28 | bld/ 29 | [Bb]in/ 30 | [Oo]bj/ 31 | [Ll]og/ 32 | [Ll]ogs/ 33 | 34 | # Visual Studio 2015/2017 cache/options directory 35 | .vs/ 36 | # Uncomment if you have tasks that create the project's static files in wwwroot 37 | #wwwroot/ 38 | 39 | # Visual Studio 2017 auto generated files 40 | Generated\ Files/ 41 | 42 | # MSTest test Results 43 | [Tt]est[Rr]esult*/ 44 | [Bb]uild[Ll]og.* 45 | 46 | # NUnit 47 | *.VisualState.xml 48 | TestResult.xml 49 | nunit-*.xml 50 | 51 | # Build Results of an ATL Project 52 | [Dd]ebugPS/ 53 | [Rr]eleasePS/ 54 | dlldata.c 55 | 56 | # Benchmark Results 57 | BenchmarkDotNet.Artifacts/ 58 | 59 | # .NET Core 60 | project.lock.json 61 | project.fragment.lock.json 62 | artifacts/ 63 | 64 | # StyleCop 65 | StyleCopReport.xml 66 | 67 | # Files built by Visual Studio 68 | *_i.c 69 | *_p.c 70 | *_h.h 71 | *.ilk 72 | *.meta 73 | *.obj 74 | *.iobj 75 | *.pch 76 | *.pdb 77 | *.ipdb 78 | *.pgc 79 | *.pgd 80 | *.rsp 81 | *.sbr 82 | *.tlb 83 | *.tli 84 | *.tlh 85 | *.tmp 86 | *.tmp_proj 87 | *_wpftmp.csproj 88 | *.log 89 | *.vspscc 90 | *.vssscc 91 | .builds 92 | *.pidb 93 | *.svclog 94 | *.scc 95 | 96 | # Chutzpah Test files 97 | _Chutzpah* 98 | 99 | # Visual C++ cache files 100 | ipch/ 101 | *.aps 102 | *.ncb 103 | *.opendb 104 | *.opensdf 105 | *.sdf 106 | *.cachefile 107 | *.VC.db 108 | *.VC.VC.opendb 109 | 110 | # Visual Studio profiler 111 | *.psess 112 | *.vsp 113 | *.vspx 114 | *.sap 115 | 116 | # Visual Studio Trace Files 117 | *.e2e 118 | 119 | # TFS 2012 Local Workspace 120 | $tf/ 121 | 122 | # Guidance Automation Toolkit 123 | *.gpState 124 | 125 | # ReSharper is a .NET coding add-in 126 | _ReSharper*/ 127 | *.[Rr]e[Ss]harper 128 | *.DotSettings.user 129 | 130 | # TeamCity is a build add-in 131 | _TeamCity* 132 | 133 | # DotCover is a Code Coverage Tool 134 | *.dotCover 135 | 136 | # AxoCover is a Code Coverage Tool 137 | .axoCover/* 138 | !.axoCover/settings.json 139 | 140 | # Visual Studio code coverage results 141 | *.coverage 142 | *.coveragexml 143 | 144 | # NCrunch 145 | _NCrunch_* 146 | .*crunch*.local.xml 147 | nCrunchTemp_* 148 | 149 | # MightyMoose 150 | *.mm.* 151 | AutoTest.Net/ 152 | 153 | # Web workbench (sass) 154 | .sass-cache/ 155 | 156 | # Installshield output folder 157 | [Ee]xpress/ 158 | 159 | # DocProject is a documentation generator add-in 160 | DocProject/buildhelp/ 161 | DocProject/Help/*.HxT 162 | DocProject/Help/*.HxC 163 | DocProject/Help/*.hhc 164 | DocProject/Help/*.hhk 165 | DocProject/Help/*.hhp 166 | DocProject/Help/Html2 167 | DocProject/Help/html 168 | 169 | # Click-Once directory 170 | publish/ 171 | 172 | # Publish Web Output 173 | *.[Pp]ublish.xml 174 | *.azurePubxml 175 | # Note: Comment the next line if you want to checkin your web deploy settings, 176 | # but database connection strings (with potential passwords) will be unencrypted 177 | *.pubxml 178 | *.publishproj 179 | 180 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 181 | # checkin your Azure Web App publish settings, but sensitive information contained 182 | # in these scripts will be unencrypted 183 | PublishScripts/ 184 | 185 | # NuGet Packages 186 | *.nupkg 187 | # NuGet Symbol Packages 188 | *.snupkg 189 | # The packages folder can be ignored because of Package Restore 190 | **/[Pp]ackages/* 191 | # except build/, which is used as an MSBuild target. 192 | !**/[Pp]ackages/build/ 193 | # Uncomment if necessary however generally it will be regenerated when needed 194 | #!**/[Pp]ackages/repositories.config 195 | # NuGet v3's project.json files produces more ignorable files 196 | *.nuget.props 197 | *.nuget.targets 198 | 199 | # Microsoft Azure Build Output 200 | csx/ 201 | *.build.csdef 202 | 203 | # Microsoft Azure Emulator 204 | ecf/ 205 | rcf/ 206 | 207 | # Windows Store app package directories and files 208 | AppPackages/ 209 | BundleArtifacts/ 210 | Package.StoreAssociation.xml 211 | _pkginfo.txt 212 | *.appx 213 | *.appxbundle 214 | *.appxupload 215 | 216 | # Visual Studio cache files 217 | # files ending in .cache can be ignored 218 | *.[Cc]ache 219 | # but keep track of directories ending in .cache 220 | !?*.[Cc]ache/ 221 | 222 | # Others 223 | ClientBin/ 224 | ~$* 225 | *~ 226 | *.dbmdl 227 | *.dbproj.schemaview 228 | *.jfm 229 | *.pfx 230 | *.publishsettings 231 | orleans.codegen.cs 232 | 233 | # Including strong name files can present a security risk 234 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 235 | #*.snk 236 | 237 | # Since there are multiple workflows, uncomment next line to ignore bower_components 238 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 239 | #bower_components/ 240 | 241 | # RIA/Silverlight projects 242 | Generated_Code/ 243 | 244 | # Backup & report files from converting an old project file 245 | # to a newer Visual Studio version. Backup files are not needed, 246 | # because we have git ;-) 247 | _UpgradeReport_Files/ 248 | Backup*/ 249 | UpgradeLog*.XML 250 | UpgradeLog*.htm 251 | ServiceFabricBackup/ 252 | *.rptproj.bak 253 | 254 | # SQL Server files 255 | *.mdf 256 | *.ldf 257 | *.ndf 258 | 259 | # Business Intelligence projects 260 | *.rdl.data 261 | *.bim.layout 262 | *.bim_*.settings 263 | *.rptproj.rsuser 264 | *- [Bb]ackup.rdl 265 | *- [Bb]ackup ([0-9]).rdl 266 | *- [Bb]ackup ([0-9][0-9]).rdl 267 | 268 | # Microsoft Fakes 269 | FakesAssemblies/ 270 | 271 | # GhostDoc plugin setting file 272 | *.GhostDoc.xml 273 | 274 | # Node.js Tools for Visual Studio 275 | .ntvs_analysis.dat 276 | node_modules/ 277 | 278 | # Visual Studio 6 build log 279 | *.plg 280 | 281 | # Visual Studio 6 workspace options file 282 | *.opt 283 | 284 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 285 | *.vbw 286 | 287 | # Visual Studio LightSwitch build output 288 | **/*.HTMLClient/GeneratedArtifacts 289 | **/*.DesktopClient/GeneratedArtifacts 290 | **/*.DesktopClient/ModelManifest.xml 291 | **/*.Server/GeneratedArtifacts 292 | **/*.Server/ModelManifest.xml 293 | _Pvt_Extensions 294 | 295 | # Paket dependency manager 296 | .paket/paket.exe 297 | paket-files/ 298 | 299 | # FAKE - F# Make 300 | .fake/ 301 | 302 | # CodeRush personal settings 303 | .cr/personal 304 | 305 | # Python Tools for Visual Studio (PTVS) 306 | __pycache__/ 307 | *.pyc 308 | 309 | # Cake - Uncomment if you are using it 310 | # tools/** 311 | # !tools/packages.config 312 | 313 | # Tabs Studio 314 | *.tss 315 | 316 | # Telerik's JustMock configuration file 317 | *.jmconfig 318 | 319 | # BizTalk build output 320 | *.btp.cs 321 | *.btm.cs 322 | *.odx.cs 323 | *.xsd.cs 324 | 325 | # OpenCover UI analysis results 326 | OpenCover/ 327 | 328 | # Azure Stream Analytics local run output 329 | ASALocalRun/ 330 | 331 | # MSBuild Binary and Structured Log 332 | *.binlog 333 | 334 | # NVidia Nsight GPU debugger configuration file 335 | *.nvuser 336 | 337 | # MFractors (Xamarin productivity tool) working folder 338 | .mfractor/ 339 | 340 | # Local History for Visual Studio 341 | .localhistory/ 342 | 343 | # BeatPulse healthcheck temp database 344 | healthchecksdb 345 | 346 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 347 | MigrationBackup/ 348 | 349 | # Ionide (cross platform F# VS Code tools) working folder 350 | .ionide/ 351 | -------------------------------------------------------------------------------- /.vscode/settings.json: -------------------------------------------------------------------------------- 1 | { 2 | "cSpell.words": [ 3 | "Neuro" 4 | ] 5 | } -------------------------------------------------------------------------------- /AutomaticMigrationConsole/AutomaticMigrationConsole.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | false 7 | LiveMigrationConsole.Program 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /AutomaticMigrationConsole/Models/Account.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using NeuroSpeech.EFCoreAutomaticMigration; 3 | using System.Collections.Generic; 4 | using System.ComponentModel; 5 | using System.ComponentModel.DataAnnotations; 6 | using System.ComponentModel.DataAnnotations.Schema; 7 | 8 | namespace LiveMigrationConsole.Models 9 | { 10 | [Table("Accounts")] 11 | public class Account 12 | { 13 | 14 | [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] 15 | public long AccountID { get; set; } 16 | 17 | [MaxLength(200)] 18 | [Index] 19 | [OldName("AccountName")] 20 | public string DisplayName { get; set; } 21 | 22 | [Column(TypeName = "varchar(20)")] 23 | public string AccountType { get; set; } 24 | 25 | [MaxLength(200)] 26 | public string EmailAddress { get; set; } 27 | 28 | [InverseProperty(nameof(Product.Vendor))] 29 | public Product[] VendorProducts { get; set; } 30 | 31 | [Column(TypeName = "decimal(18,2)")] 32 | public decimal Balance { get; set; } 33 | 34 | public decimal? Total { get; set; } 35 | 36 | [Column(TypeName = "datetime")] 37 | public DateTime DateCreated { get; set; } 38 | 39 | public ICollection Events { get; set; } 40 | } 41 | } -------------------------------------------------------------------------------- /AutomaticMigrationConsole/Models/AccountEvent.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations.Schema; 2 | 3 | namespace LiveMigrationConsole.Models 4 | { 5 | public class AccountEvent: CalendarEvent 6 | { 7 | public bool IsBusy { get; set; } 8 | 9 | public long AccountID { get; set; } 10 | 11 | [ForeignKey(nameof(AccountID))] 12 | public Account Account { get; set; } 13 | } 14 | 15 | 16 | 17 | } 18 | -------------------------------------------------------------------------------- /AutomaticMigrationConsole/Models/CalendarEvent.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.ComponentModel.DataAnnotations; 3 | using System.ComponentModel.DataAnnotations.Schema; 4 | 5 | namespace LiveMigrationConsole.Models 6 | { 7 | [Table("CalenderEvents")] 8 | public class CalendarEvent 9 | { 10 | [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] 11 | public long CalendarEventID { get; set; } 12 | public DateTimeOffset Start { get; set; } 13 | 14 | public DateTimeOffset End { get; set; } 15 | } 16 | 17 | 18 | } 19 | -------------------------------------------------------------------------------- /AutomaticMigrationConsole/Models/Config.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations; 2 | using System.ComponentModel.DataAnnotations.Schema; 3 | 4 | namespace LiveMigrationConsole.Models 5 | { 6 | [Table("Configs")] 7 | public class Config 8 | { 9 | 10 | [Key, MaxLength(20)] 11 | public string Key { get; set; } 12 | 13 | public string Value { get; set; } 14 | 15 | } 16 | } -------------------------------------------------------------------------------- /AutomaticMigrationConsole/Models/ERPContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace LiveMigrationConsole.Models 6 | { 7 | public partial class ERPContext : DbContext 8 | { 9 | 10 | public ERPContext(DbContextOptions options):base(options) 11 | { 12 | 13 | } 14 | 15 | public DbSet Products { get; set; } 16 | 17 | public DbSet Accounts { get; set; } 18 | 19 | public DbSet Configs { get; set; } 20 | 21 | public DbSet CalendarEvents { get; set; } 22 | 23 | public DbSet Talents { get; set; } 24 | 25 | public DbSet Features { get; set; } 26 | 27 | public DbSet Orders { get; set; } 28 | 29 | protected override void OnModelCreating(ModelBuilder modelBuilder) 30 | { 31 | modelBuilder.Entity().HasKey(x => new { 32 | x.ProductID, 33 | x.FeatureID 34 | }); 35 | 36 | modelBuilder.Entity() 37 | .HasIndex(x => new { 38 | x.EmailAddress, 39 | x.AccountType 40 | }) 41 | .IsUnique(); 42 | 43 | modelBuilder.Entity().ToTable("Accounts"); 44 | modelBuilder.Entity().ToTable("Talents"); 45 | } 46 | 47 | 48 | } 49 | 50 | 51 | } 52 | -------------------------------------------------------------------------------- /AutomaticMigrationConsole/Models/Order.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.ComponentModel.DataAnnotations; 5 | using System.ComponentModel.DataAnnotations.Schema; 6 | using System.Text; 7 | 8 | namespace LiveMigrationConsole.Models 9 | { 10 | [Table("Orders")] 11 | public class Order 12 | { 13 | 14 | [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] 15 | public long OrderID { get; set; } 16 | 17 | 18 | public Address Shipping { get; set; } 19 | 20 | public Address Billing { get; set; } 21 | } 22 | 23 | [Owned] 24 | public class Address 25 | { 26 | public string StreetAddress { get; set; } 27 | public string City { get; set; } 28 | public string State { get; set; } 29 | public string Country { get; set; } 30 | 31 | public string ZipCode { get; set; } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /AutomaticMigrationConsole/Models/Product.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations; 2 | using System.ComponentModel.DataAnnotations.Schema; 3 | 4 | namespace LiveMigrationConsole.Models 5 | { 6 | [Table("Products")] 7 | public class Product 8 | { 9 | 10 | [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] 11 | public long ProductID { get; set; } 12 | 13 | 14 | public long? VendorID { get; set; } 15 | 16 | [ForeignKey(nameof(VendorID))] 17 | [InverseProperty(nameof(Models.Account.VendorProducts))] 18 | public Account? Vendor { get; set; } 19 | 20 | } 21 | 22 | 23 | [Table("ProductFeatures")] 24 | public class ProductFeature { 25 | 26 | //[Key, Column( Order = 1)] 27 | public long ProductID { get; set; } 28 | 29 | [MaxLength(20)] 30 | public string FeatureID { get; set; } 31 | 32 | } 33 | } -------------------------------------------------------------------------------- /AutomaticMigrationConsole/Models/Talent.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations; 4 | using System.ComponentModel.DataAnnotations.Schema; 5 | 6 | namespace LiveMigrationConsole.Models 7 | { 8 | [Table("Talents")] 9 | public class Talent: Account { 10 | 11 | 12 | public Name Legal { get; set; } 13 | 14 | } 15 | 16 | [Owned] 17 | public class Name 18 | { 19 | public string FirstName { get; set; } 20 | public string LastName { get; set; } 21 | } 22 | } -------------------------------------------------------------------------------- /AutomaticMigrationConsole/Models/TalentEvent.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations.Schema; 2 | 3 | namespace LiveMigrationConsole.Models 4 | { 5 | public class TalentEvent: CalendarEvent 6 | { 7 | public long TalentID { get; set; } 8 | 9 | [ForeignKey(nameof(TalentID))] 10 | public Talent Talent { get; set; } 11 | } 12 | } -------------------------------------------------------------------------------- /AutomaticMigrationConsole/Program.cs: -------------------------------------------------------------------------------- 1 | using LiveMigrationConsole.Models; 2 | using Microsoft.EntityFrameworkCore; 3 | using NeuroSpeech.EFCoreAutomaticMigration; 4 | using System; 5 | using System.Collections.Generic; 6 | 7 | namespace LiveMigrationConsole 8 | { 9 | class Program 10 | { 11 | static void Main(string[] args) 12 | { 13 | DbContextOptionsBuilder options = new DbContextOptionsBuilder(); 14 | options.UseSqlServer("Server=(localdb)\\mssqllocaldb;Database=ERPModel;Trusted_Connection=True;MultipleActiveResultSets=true"); 15 | 16 | using (var db = new ERPContext(options.Options)) { 17 | 18 | var r = db.MigrationForSqlServer().Migrate(); 19 | Console.WriteLine(r.Log); 20 | 21 | var acc = new Account { 22 | DisplayName = "A", 23 | AccountType = "Admin", 24 | DateCreated = DateTime.UtcNow, 25 | Events = new List 26 | { 27 | new AccountEvent{ 28 | Start = DateTimeOffset.UtcNow, 29 | End = DateTimeOffset.UtcNow.AddDays(1), 30 | IsBusy = true 31 | } 32 | } 33 | }; 34 | 35 | db.Accounts.Add(acc); 36 | 37 | db.Orders.Add(new Order { 38 | Billing = new Address { }, 39 | Shipping = new Address { } 40 | }); 41 | 42 | 43 | db.SaveChanges(); 44 | 45 | } 46 | 47 | } 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 NeuroSpeech Technologies Pvt Ltd 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /NeuroSpeech.EFCoreAutomaticMigration.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 16 4 | VisualStudioVersion = 16.0.31515.178 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NeuroSpeech.EFCoreAutomaticMigration", "NeuroSpeech.EFCoreAutomaticMigration\NeuroSpeech.EFCoreAutomaticMigration.csproj", "{963A3244-96BF-423E-93A4-1F2B66B42F32}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "AutomaticMigrationConsole", "AutomaticMigrationConsole\AutomaticMigrationConsole.csproj", "{B815E304-F342-49E8-8082-98E29CB9BB56}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NeuroSpeech.EFCoreAutomaticMigration.PostGreSql", "PostGreSql\NeuroSpeech.EFCoreAutomaticMigration.PostGreSql\NeuroSpeech.EFCoreAutomaticMigration.PostGreSql.csproj", "{75942E58-8306-4971-BAE6-A8948E676D1C}" 11 | EndProject 12 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PostGreSql", "PostGreSql", "{B71AE9A5-9212-4A50-B558-A60F9FEE48FD}" 13 | EndProject 14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PostGreSqlAutomaticMigrationConsole", "PostGreSql\PostGreSqlMigrationConsole\PostGreSqlAutomaticMigrationConsole.csproj", "{303D73A9-BA85-42AC-A0D2-7B5D4B998404}" 15 | EndProject 16 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{633316B3-2372-420E-AB39-A2B6809B69BA}" 17 | ProjectSection(SolutionItems) = preProject 18 | README.md = README.md 19 | EndProjectSection 20 | EndProject 21 | Global 22 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 23 | Debug|Any CPU = Debug|Any CPU 24 | Release|Any CPU = Release|Any CPU 25 | EndGlobalSection 26 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 27 | {963A3244-96BF-423E-93A4-1F2B66B42F32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 28 | {963A3244-96BF-423E-93A4-1F2B66B42F32}.Debug|Any CPU.Build.0 = Debug|Any CPU 29 | {963A3244-96BF-423E-93A4-1F2B66B42F32}.Release|Any CPU.ActiveCfg = Release|Any CPU 30 | {963A3244-96BF-423E-93A4-1F2B66B42F32}.Release|Any CPU.Build.0 = Release|Any CPU 31 | {B815E304-F342-49E8-8082-98E29CB9BB56}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 32 | {B815E304-F342-49E8-8082-98E29CB9BB56}.Debug|Any CPU.Build.0 = Debug|Any CPU 33 | {B815E304-F342-49E8-8082-98E29CB9BB56}.Release|Any CPU.ActiveCfg = Release|Any CPU 34 | {B815E304-F342-49E8-8082-98E29CB9BB56}.Release|Any CPU.Build.0 = Release|Any CPU 35 | {75942E58-8306-4971-BAE6-A8948E676D1C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 36 | {75942E58-8306-4971-BAE6-A8948E676D1C}.Debug|Any CPU.Build.0 = Debug|Any CPU 37 | {75942E58-8306-4971-BAE6-A8948E676D1C}.Release|Any CPU.ActiveCfg = Release|Any CPU 38 | {75942E58-8306-4971-BAE6-A8948E676D1C}.Release|Any CPU.Build.0 = Release|Any CPU 39 | {303D73A9-BA85-42AC-A0D2-7B5D4B998404}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 40 | {303D73A9-BA85-42AC-A0D2-7B5D4B998404}.Debug|Any CPU.Build.0 = Debug|Any CPU 41 | {303D73A9-BA85-42AC-A0D2-7B5D4B998404}.Release|Any CPU.ActiveCfg = Release|Any CPU 42 | {303D73A9-BA85-42AC-A0D2-7B5D4B998404}.Release|Any CPU.Build.0 = Release|Any CPU 43 | EndGlobalSection 44 | GlobalSection(SolutionProperties) = preSolution 45 | HideSolutionNode = FALSE 46 | EndGlobalSection 47 | GlobalSection(NestedProjects) = preSolution 48 | {75942E58-8306-4971-BAE6-A8948E676D1C} = {B71AE9A5-9212-4A50-B558-A60F9FEE48FD} 49 | {303D73A9-BA85-42AC-A0D2-7B5D4B998404} = {B71AE9A5-9212-4A50-B558-A60F9FEE48FD} 50 | EndGlobalSection 51 | GlobalSection(ExtensibilityGlobals) = postSolution 52 | SolutionGuid = {156E3275-44A5-4A09-A4EE-87E4414412EB} 53 | EndGlobalSection 54 | EndGlobal 55 | -------------------------------------------------------------------------------- /NeuroSpeech.EFCoreAutomaticMigration/Column.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using Microsoft.EntityFrameworkCore.Metadata; 3 | using System; 4 | using System.Text; 5 | 6 | namespace NeuroSpeech.EFCoreAutomaticMigration 7 | { 8 | public class Column 9 | { 10 | public string ColumnName; 11 | public bool IsPrimaryKey; 12 | public bool IsNullable; 13 | public string ColumnDefault; 14 | public string DataType; 15 | public int DataLength; 16 | public decimal? NumericPrecision; 17 | public decimal? NumericScale; 18 | public bool IsIdentity; 19 | 20 | public DbTableInfo Table { get; internal set; } 21 | 22 | public string EscapedColumnName { get; internal set; } 23 | public string TableNameAndColumnName { get; internal set; } 24 | public string EscapedTableNameAndColumnName { get; internal set; } 25 | 26 | 27 | internal bool IsSame(DbColumnInfo column) 28 | { 29 | var x = column.Property; 30 | if (!x.GetColumnTypeForSql().EqualsIgnoreCase(this.DataType)) 31 | return false; 32 | int xLength = x.GetMaxLength() ?? 0; 33 | if (DataLength > 0 && xLength > 0) 34 | { 35 | if (DataLength != xLength) 36 | { 37 | // smaller value is fine... 38 | if(DataLength < xLength) 39 | return false; 40 | } 41 | } 42 | 43 | if (IsNullable != x.IsColumnNullable()) 44 | return false; 45 | 46 | //var xColumnDefault = x.GetDefaultValueSql() ?? x.GetDefaultValue()?.ToString(); 47 | //if (!x.IsColumnNullable()) 48 | //{ 49 | // if (!(string.IsNullOrWhiteSpace(ColumnDefault) 50 | // && string.IsNullOrWhiteSpace(xColumnDefault))) 51 | // { 52 | // return ColumnDefault == xColumnDefault; 53 | // } 54 | //} 55 | return true; 56 | } 57 | 58 | public override string ToString() 59 | { 60 | var sb = new StringBuilder(); 61 | sb.Append(DataType); 62 | if(DataLength > 0) 63 | { 64 | sb.Append('('); 65 | sb.Append(DataLength); 66 | sb.Append(')'); 67 | } 68 | if(NumericPrecision != null) 69 | { 70 | sb.Append('('); 71 | sb.Append(NumericPrecision); 72 | sb.Append(','); 73 | sb.Append(NumericScale); 74 | sb.Append(')'); 75 | } 76 | if (IsNullable) 77 | { 78 | sb.Append(" NULL "); 79 | } else 80 | { 81 | sb.Append(" NOT NULL "); 82 | } 83 | return sb.ToString(); 84 | } 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /NeuroSpeech.EFCoreAutomaticMigration/Columns.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using Microsoft.EntityFrameworkCore.Metadata; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | namespace NeuroSpeech.EFCoreAutomaticMigration 7 | { 8 | public class Columns 9 | { 10 | private ModelMigrationBase modelMigration; 11 | 12 | public Columns(ModelMigrationBase modelMigration) 13 | { 14 | this.modelMigration = modelMigration; 15 | tables = new Dictionary>(); 16 | indexes = new Dictionary>(); 17 | } 18 | 19 | public Dictionary> tables { get; set; } 20 | 21 | public Dictionary> indexes { get; set; } 22 | 23 | 24 | public Column this[DbColumnInfo property] 25 | { 26 | get 27 | { 28 | var tableName = property.Table.EscapedNameWithSchema; 29 | if (!tables.TryGetValue(tableName, out var columns)) 30 | { 31 | columns = LoadColumns(property.Table); 32 | } 33 | return columns.FirstOrDefault(x => x.ColumnName == property.ColumnName); 34 | } 35 | } 36 | 37 | public DbIndex this[SqlIndexEx index] 38 | { 39 | get 40 | { 41 | var tableName = index.Table.EscapedNameWithSchema; 42 | if (!indexes.TryGetValue(tableName, out var ind)) 43 | { 44 | ind = LoadIndexes(index.DeclaringEntityType); 45 | } 46 | return ind.FirstOrDefault(x => x.Name == index.Name); 47 | } 48 | } 49 | 50 | private List LoadIndexes(IEntityType entity) 51 | { 52 | List list = new List(); 53 | var indexSql = modelMigration.LoadIndexes(entity); 54 | var tableName = entity.GetTableName(); 55 | var schameName = entity.GetSchemaOrDefault(); 56 | using (var reader = modelMigration.Read(indexSql, new Dictionary { 57 | { "@TableName", tableName } , 58 | { "@SchemaName", schameName } 59 | })) 60 | { 61 | 62 | while (reader.Read()) 63 | { 64 | var index = new DbIndex(); 65 | 66 | index.Name = reader.GetValue("IndexName"); 67 | index.Columns = new string[] { 68 | reader.GetValue("ColumnName") 69 | }; 70 | index.Filter = reader.GetValue("Filter"); 71 | list.Add(index); 72 | } 73 | 74 | list = list.GroupBy(x => x.Name).Select(x => new DbIndex 75 | { 76 | Name = x.Key, 77 | Columns = x.SelectMany(c => c.Columns).Select(c => $"[{c}]").ToArray(), 78 | Filter = x.First().Filter 79 | }).ToList(); 80 | 81 | } 82 | return list; 83 | } 84 | 85 | private List LoadColumns(DbTableInfo table) 86 | { 87 | List columns = new List(); 88 | string sqlColumns = modelMigration.LoadTableColumns(); 89 | using (var reader = modelMigration.Read(sqlColumns, new Dictionary { 90 | { "@TableName", table.TableName } , 91 | { "@SchemaName", table.Schema } 92 | })) 93 | { 94 | 95 | 96 | while (reader.Read()) 97 | { 98 | Column col = new Column(); 99 | 100 | col.ColumnName = reader.GetValue("ColumnName"); 101 | col.IsPrimaryKey = reader.GetValue("IsPrimaryKey"); 102 | col.IsNullable = reader.GetValue("IsNullable"); 103 | col.ColumnDefault = reader.GetValue("ColumnDefault"); 104 | col.DataType = reader.GetValue("DataType"); 105 | col.DataLength = reader.GetValue("DataLength"); 106 | col.NumericPrecision = reader.GetValue("NumericPrecision"); 107 | col.NumericScale = reader.GetValue("NumericScale"); 108 | col.IsIdentity = reader.GetValue("IsIdentity"); 109 | 110 | columns.Add(col); 111 | } 112 | 113 | } 114 | return columns; 115 | } 116 | 117 | public void Clear(DbTableInfo entity) 118 | { 119 | tables.Remove(entity.EscapedNameWithSchema); 120 | indexes.Remove(entity.EscapedNameWithSchema); 121 | } 122 | 123 | internal bool Exists(DbTableInfo entity) 124 | { 125 | var tableName = entity.EscapedNameWithSchema; 126 | if (!tables.TryGetValue(tableName, out var columns)) 127 | { 128 | columns = LoadColumns(entity); 129 | tables[tableName] = columns; 130 | } 131 | return columns.Count > 0; 132 | } 133 | 134 | } 135 | } 136 | -------------------------------------------------------------------------------- /NeuroSpeech.EFCoreAutomaticMigration/DbColumnInfo.cs: -------------------------------------------------------------------------------- 1 | #nullable enable 2 | using Microsoft.EntityFrameworkCore; 3 | using Microsoft.EntityFrameworkCore.Metadata; 4 | using System; 5 | using System.ComponentModel; 6 | using System.Reflection; 7 | using System.Text; 8 | 9 | namespace NeuroSpeech.EFCoreAutomaticMigration 10 | { 11 | public class BaseDbColumnInfo 12 | { 13 | 14 | } 15 | 16 | public class DbColumnInfo 17 | { 18 | public readonly DbTableInfo Table; 19 | public readonly IProperty Property; 20 | public readonly string ColumnName; 21 | public readonly string EscapedColumnName; 22 | public readonly string TableNameAndColumnName; 23 | public readonly string EscapedTableNameAndColumnName; 24 | public readonly string DataType; 25 | public readonly int? DataLength; 26 | public readonly int? Precision; 27 | public readonly int? DecimalScale; 28 | public readonly bool IsKey; 29 | public readonly bool IsIdentity; 30 | public readonly bool IsNullable; 31 | public readonly string? DefaultValue; 32 | 33 | public DbColumnInfo( 34 | DbTableInfo table, 35 | IProperty property, 36 | Func escape) 37 | { 38 | this.Table = table; 39 | this.Property = property; 40 | this.ColumnName = property.ColumnName(); 41 | this.TableNameAndColumnName = Table.TableName + "." + ColumnName; 42 | this.EscapedColumnName = escape(this.ColumnName); 43 | this.EscapedTableNameAndColumnName = table.EscapedTableName + "." + this.EscapedColumnName; 44 | 45 | var (length, d) = property.GetColumnDataLength(); 46 | 47 | this.DataType = property.GetColumnTypeForSql(); 48 | if (property.ClrType.IsAssignableFrom(typeof(decimal))) 49 | { 50 | #if NET_STANDARD_2_1 51 | var ps = property.GetScale(); 52 | var pp = property.GetPrecision(); 53 | this.Precision = pp ?? length; 54 | this.DecimalScale = ps ?? d; 55 | #else 56 | this.Precision = 18; 57 | this.DecimalScale = 2; 58 | #endif 59 | } 60 | else 61 | { 62 | this.DataLength = length 63 | ?? (property.ClrType == typeof(string) ? (int?)int.MaxValue : null); 64 | } 65 | this.IsKey = property.IsKey(); 66 | this.IsIdentity = property.IsIdentityColumn(table.EntityType); 67 | this.IsNullable = property.IsColumnNullable(); 68 | this.DefaultValue = property.GetDefaultValueSql(); 69 | 70 | if(this.DefaultValue == null) 71 | { 72 | var dv = property.PropertyInfo?.GetCustomAttribute(); 73 | if(dv?.Value != null) 74 | { 75 | if (dv.Value is string sv) 76 | { 77 | this.DefaultValue = sv; 78 | } else 79 | { 80 | throw new InvalidOperationException($"Default value must be provided in string literal equivalent in SQL"); 81 | } 82 | } 83 | } 84 | } 85 | 86 | public override string ToString() 87 | { 88 | var sb = new StringBuilder(); 89 | sb.Append(DataType); 90 | if (DataLength > 0) 91 | { 92 | sb.Append('('); 93 | sb.Append(DataLength); 94 | sb.Append(')'); 95 | } 96 | if (Precision != null) 97 | { 98 | sb.Append('('); 99 | sb.Append(Precision); 100 | sb.Append(','); 101 | sb.Append(DecimalScale); 102 | sb.Append(')'); 103 | } 104 | if (IsNullable) 105 | { 106 | sb.Append(" NULL "); 107 | } 108 | else 109 | { 110 | sb.Append(" NOT NULL "); 111 | } 112 | return sb.ToString(); 113 | } 114 | } 115 | } -------------------------------------------------------------------------------- /NeuroSpeech.EFCoreAutomaticMigration/DbIndex.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | 3 | namespace NeuroSpeech.EFCoreAutomaticMigration 4 | { 5 | public class DbIndex 6 | { 7 | public string Name; 8 | internal string[] Columns; 9 | internal string Filter; 10 | 11 | internal bool IsSame(SqlIndexEx index, ModelMigrationBase migrationBase) 12 | { 13 | string thisColumns = string.Join(",", Columns); 14 | string indexColumns = string.Join(",", index.Properties.Select(x => migrationBase.Escape( x.ColumnName()))); 15 | 16 | if (!thisColumns.EqualsIgnoreCase(indexColumns)) 17 | return false; 18 | var existingFilter = index.GetFilter(); 19 | if (Filter == null) 20 | return existingFilter == null; 21 | if (existingFilter == null) 22 | return false; 23 | if (Filter.Trim('(', ')').Trim().ToLower() == existingFilter.Trim('(',')').Trim().ToLower()) 24 | return true; 25 | return false; 26 | } 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /NeuroSpeech.EFCoreAutomaticMigration/DbTableInfo.cs: -------------------------------------------------------------------------------- 1 | #nullable enable 2 | using Microsoft.EntityFrameworkCore; 3 | using Microsoft.EntityFrameworkCore.Metadata; 4 | using System; 5 | using System.Collections.Generic; 6 | 7 | namespace NeuroSpeech.EFCoreAutomaticMigration 8 | { 9 | public class DbTableInfo 10 | { 11 | public readonly string TableName; 12 | 13 | public readonly string Schema; 14 | 15 | public readonly string EscapedNameWithSchema; 16 | 17 | public readonly string EscapedTableName; 18 | 19 | public readonly string EscapedSchema; 20 | 21 | public readonly Type ClrType; 22 | 23 | public readonly IEntityType EntityType; 24 | 25 | internal readonly List columnsAdded = new List(); 26 | internal readonly List<(Column from, DbColumnInfo to)> columnsRenamed = new List<(Column from, DbColumnInfo to)>(); 27 | internal readonly List<(bool Dropped, SqlIndexEx Index)> indexedUpdated = new List<(bool Dropped, SqlIndexEx Index)>(); 28 | 29 | public IReadOnlyCollection ColumnsAdded => columnsAdded.AsReadOnly(); 30 | 31 | public IReadOnlyCollection<(Column from, DbColumnInfo to)> ColumnsRenamed => columnsRenamed.AsReadOnly(); 32 | 33 | public IReadOnlyCollection<(bool Dropped, SqlIndexEx Index)> IndexesUpdated => indexedUpdated.AsReadOnly(); 34 | 35 | public DbTableInfo(IEntityType type, Func escape) 36 | { 37 | this.EntityType = type; 38 | this.ClrType = type.ClrType; 39 | this.TableName = type.GetTableName(); 40 | this.EscapedTableName = escape(this.TableName); 41 | this.Schema = type.GetSchemaOrDefault(); 42 | this.EscapedSchema = escape(this.Schema); 43 | this.EscapedNameWithSchema = $"{this.EscapedSchema}.{this.EscapedTableName}"; 44 | } 45 | 46 | public DbTableInfo(string schemaName, string tableName, Func escape) 47 | { 48 | this.EntityType = null!; 49 | this.ClrType = null!; 50 | this.TableName = tableName; 51 | this.EscapedTableName = escape(tableName); 52 | this.Schema = schemaName; 53 | this.EscapedSchema = escape(this.Schema); 54 | this.EscapedNameWithSchema = $"{this.EscapedSchema}.{this.EscapedTableName}"; 55 | } 56 | } 57 | } -------------------------------------------------------------------------------- /NeuroSpeech.EFCoreAutomaticMigration/IMigrationEvents.cs: -------------------------------------------------------------------------------- 1 | #nullable enable 2 | using System.Collections.Generic; 3 | 4 | namespace NeuroSpeech.EFCoreAutomaticMigration 5 | { 6 | public interface IMigrationEvents 7 | { 8 | 9 | void OnTableCreated(DbTableInfo table); 10 | 11 | void OnColumnAdded(DbColumnInfo column, Column? existing); 12 | 13 | void OnIndexDropped(SqlIndexEx index); 14 | 15 | void OnIndexCreated(SqlIndexEx index); 16 | 17 | void OnTableModified( 18 | DbTableInfo table, 19 | IReadOnlyCollection columnsAdded, 20 | IReadOnlyCollection<(Column from, DbColumnInfo to)> columnsRenamed, 21 | IReadOnlyCollection<(bool Dropped, SqlIndexEx index)> indexesUpdated); 22 | void OnColumnChanged(DbColumnInfo column, Column existing); 23 | } 24 | 25 | } -------------------------------------------------------------------------------- /NeuroSpeech.EFCoreAutomaticMigration/IdentityExtensions.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using Microsoft.EntityFrameworkCore.Metadata; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | namespace NeuroSpeech.EFCoreAutomaticMigration 8 | { 9 | public static class IdentityExtensions 10 | { 11 | 12 | public static bool IsIdentityColumn(this IProperty property, IEntityType source) 13 | { 14 | if(property.GetValueGenerationStrategy() == SqlServerValueGenerationStrategy.IdentityColumn) 15 | { 16 | var entity = source; 17 | var baseEntity = source.BaseType; 18 | if (baseEntity == null) 19 | { 20 | return true; 21 | } 22 | var entityTable = entity.GetTableName(); 23 | var baseEntityTable = baseEntity.GetTableName(); 24 | return entityTable == baseEntityTable; 25 | } 26 | return false; 27 | } 28 | 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /NeuroSpeech.EFCoreAutomaticMigration/Index.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace NeuroSpeech.EFCoreAutomaticMigration 4 | { 5 | 6 | /// 7 | /// 8 | /// 9 | public class IndexAttribute : Attribute 10 | { 11 | } 12 | 13 | public class IgnoreMigrationAttribute: Attribute 14 | { 15 | 16 | } 17 | } -------------------------------------------------------------------------------- /NeuroSpeech.EFCoreAutomaticMigration/MigrationEventList.cs: -------------------------------------------------------------------------------- 1 | #nullable enable 2 | using System; 3 | using System.Collections.Generic; 4 | 5 | namespace NeuroSpeech.EFCoreAutomaticMigration 6 | { 7 | internal class MigrationEventList: IMigrationEvents 8 | { 9 | List? events; 10 | 11 | public void Add(IMigrationEvents handler) 12 | { 13 | this.events = this.events ?? new List(); 14 | this.events.Add(handler); 15 | } 16 | 17 | public void OnColumnAdded(DbColumnInfo column, Column? existing = null) 18 | { 19 | if (this.events != null) 20 | { 21 | foreach (var e in this.events) 22 | { 23 | e.OnColumnAdded(column, existing); 24 | } 25 | } 26 | Console.WriteLine($"Column {column.TableNameAndColumnName} Added."); 27 | } 28 | 29 | public void OnIndexCreated(SqlIndexEx index) 30 | { 31 | if (this.events != null) 32 | { 33 | foreach (var e in this.events) 34 | { 35 | e.OnIndexCreated(index); 36 | } 37 | } 38 | Console.WriteLine($"Index {index.Name} Added."); 39 | } 40 | 41 | public void OnIndexDropped(SqlIndexEx index) 42 | { 43 | if (this.events != null) 44 | { 45 | foreach (var e in this.events) 46 | { 47 | e.OnIndexDropped(index); 48 | } 49 | } 50 | Console.WriteLine($"Index {index.Name} Dropped."); 51 | } 52 | 53 | public void OnTableCreated(DbTableInfo table) 54 | { 55 | if (this.events != null) 56 | { 57 | foreach (var e in this.events) 58 | { 59 | e.OnTableCreated(table); 60 | } 61 | } 62 | Console.WriteLine($"Table {table.EscapedNameWithSchema} Added."); 63 | } 64 | 65 | public void OnTableModified( 66 | DbTableInfo table, 67 | IReadOnlyCollection columnsAdded, 68 | IReadOnlyCollection<(Column from, DbColumnInfo to)> columnsRenamed, 69 | IReadOnlyCollection<(bool Dropped, SqlIndexEx index)> indexesUpdated) 70 | { 71 | if (this.events != null) 72 | { 73 | foreach (var e in this.events) 74 | { 75 | e.OnTableModified(table, columnsAdded, columnsRenamed, indexesUpdated); 76 | } 77 | } 78 | Console.WriteLine($"Table {table.EscapedNameWithSchema} Sync Successful."); 79 | } 80 | 81 | public void OnColumnChanged(DbColumnInfo column, Column existing) 82 | { 83 | if (this.events != null) 84 | { 85 | foreach (var e in this.events) 86 | { 87 | e.OnColumnChanged(column, existing); 88 | } 89 | } 90 | Console.WriteLine($"Column {column.TableNameAndColumnName} Changed."); 91 | } 92 | } 93 | } -------------------------------------------------------------------------------- /NeuroSpeech.EFCoreAutomaticMigration/MigrationEvents.cs: -------------------------------------------------------------------------------- 1 | #nullable enable 2 | using System.Collections.Generic; 3 | 4 | namespace NeuroSpeech.EFCoreAutomaticMigration 5 | { 6 | 7 | public abstract class MigrationEvents : IMigrationEvents 8 | { 9 | void IMigrationEvents.OnColumnAdded(DbColumnInfo column, Column? existing) 10 | { 11 | if(column.Table.ClrType == typeof(T)) 12 | { 13 | OnColumnAdded(column, existing); 14 | } 15 | } 16 | 17 | 18 | void IMigrationEvents.OnIndexCreated(SqlIndexEx index) 19 | { 20 | if (index.Table.ClrType == typeof(T)) 21 | { 22 | OnIndexCreated(index); 23 | } 24 | } 25 | 26 | 27 | void IMigrationEvents.OnIndexDropped(SqlIndexEx index) 28 | { 29 | if (index.Table.ClrType == typeof(T)) 30 | { 31 | OnIndexDropped(index); 32 | } 33 | } 34 | 35 | 36 | void IMigrationEvents.OnTableCreated(DbTableInfo table) 37 | { 38 | if (table.ClrType == typeof(T)) 39 | { 40 | OnTableCreated(table); 41 | } 42 | } 43 | 44 | 45 | void IMigrationEvents.OnTableModified(DbTableInfo table, IReadOnlyCollection columnsAdded, IReadOnlyCollection<(Column from, DbColumnInfo to)> columnsRenamed, IReadOnlyCollection<(bool Dropped, SqlIndexEx index)> indexesUpdated) 46 | { 47 | if (table.ClrType == typeof(T)) 48 | { 49 | OnTableModified(table, columnsAdded, columnsRenamed, indexesUpdated); 50 | } 51 | } 52 | 53 | protected abstract void OnIndexCreated(SqlIndexEx index); 54 | protected abstract void OnIndexDropped(SqlIndexEx index); 55 | protected abstract void OnTableCreated(DbTableInfo table); 56 | protected abstract void OnColumnAdded(DbColumnInfo column, Column? existing); 57 | protected abstract void OnTableModified(DbTableInfo table, IReadOnlyCollection columnsAdded, IReadOnlyCollection<(Column from, DbColumnInfo to)> columnsRenamed, IReadOnlyCollection<(bool Dropped, SqlIndexEx index)> indexesUpdated); 58 | 59 | protected abstract void OnColumnChanged(DbColumnInfo column, Column existing); 60 | 61 | void IMigrationEvents.OnColumnChanged(DbColumnInfo column, Column existing) 62 | { 63 | if(column.Table.ClrType == typeof(T)) 64 | { 65 | OnColumnChanged(column, existing); 66 | } 67 | } 68 | } 69 | 70 | } -------------------------------------------------------------------------------- /NeuroSpeech.EFCoreAutomaticMigration/ModelMigration.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using Microsoft.EntityFrameworkCore.Metadata; 3 | using NeuroSpeech.TemplatedQuery; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.ComponentModel.DataAnnotations.Schema; 7 | using System.Data.Common; 8 | using System.Linq; 9 | using System.Reflection; 10 | using System.Text; 11 | 12 | namespace NeuroSpeech.EFCoreAutomaticMigration 13 | { 14 | public class ModelMigration : ModelMigrationBase 15 | { 16 | 17 | public ModelMigration(DbContext context) : base(context) 18 | { 19 | 20 | } 21 | 22 | 23 | internal protected override string LoadTableColumns() 24 | { 25 | return Scripts.SqlServerGetSchema; 26 | } 27 | 28 | internal protected override string LoadIndexes(IEntityType entity) 29 | { 30 | return Scripts.SqlServerGetIndexes; 31 | } 32 | 33 | public override string Escape(string name) 34 | { 35 | return $"[{name}]"; 36 | } 37 | 38 | protected override void AddColumn(DbColumnInfo property) 39 | { 40 | Run($"ALTER TABLE {property.Table.EscapedNameWithSchema} ADD " 41 | + ToColumn(property)); 42 | } 43 | 44 | protected override void RenameColumn(DbColumnInfo property, string postFix) 45 | { 46 | Run($"EXEC sp_rename '{property.Table.Schema}.{property.Table.TableName}.{property.ColumnName}', '{property.ColumnName}{postFix}'"); 47 | } 48 | 49 | protected override void CreateTable( 50 | DbTableInfo entity, 51 | List keys) 52 | { 53 | var createTable = TemplateQuery.Literal(@$" 54 | CREATE TABLE {entity.EscapedNameWithSchema} ({ string.Join(",", keys.Select(ToColumn)) }, 55 | PRIMARY KEY( { string.Join(", ", keys.Select(x => x.EscapedColumnName )) } ))"); 56 | 57 | Run(createTable); 58 | } 59 | 60 | 61 | private static string[] textTypes = new[] { "nvarchar", "varchar" }; 62 | 63 | protected override bool IsText(string n) => textTypes.Any(a => a.Equals(n, StringComparison.OrdinalIgnoreCase)); 64 | 65 | protected override bool IsDecimal(string n) => n.Equals("decimal", StringComparison.OrdinalIgnoreCase); 66 | 67 | protected override string GetTableNameWithSchema(IEntityType entity) 68 | { 69 | return $"{Escape(entity.GetSchemaOrDefault())}.{Escape(entity.GetTableName())}"; 70 | } 71 | 72 | protected internal override bool HasAnyRows(DbTableInfo table) 73 | { 74 | var sql = $"SELECT TOP (1) 1 FROM {table.EscapedNameWithSchema}"; 75 | using var cmd = CreateCommand(sql); 76 | using var i = cmd.ExecuteReader(); 77 | return i.Read(); 78 | } 79 | 80 | protected override string ToColumn(DbColumnInfo c) 81 | { 82 | 83 | var name = $"{c.EscapedColumnName} {c.DataType}"; 84 | 85 | if (c.DataLength != null) 86 | { 87 | if (c.DataLength > 0 && c.DataLength < int.MaxValue) 88 | { 89 | name += $"({ c.DataLength })"; 90 | } 91 | else 92 | { 93 | name += "(MAX)"; 94 | } 95 | } 96 | if (c.Precision != null) 97 | { 98 | var np = c.Precision ?? 18; 99 | var nps = c.DecimalScale ?? 2; 100 | 101 | name += $"({ np },{ nps })"; 102 | } 103 | if (!c.IsKey) 104 | { 105 | // lets allow nullable to every field... 106 | if (c.IsNullable) 107 | { 108 | name += " NULL "; 109 | } 110 | else 111 | { 112 | name += " NOT NULL "; 113 | } 114 | } 115 | 116 | if (c.IsIdentity) 117 | { 118 | name += " Identity "; 119 | } 120 | 121 | if (!string.IsNullOrWhiteSpace(c.DefaultValue)) 122 | { 123 | name += " DEFAULT " + c.DefaultValue; 124 | } 125 | return name; 126 | } 127 | 128 | protected override void DropIndex(SqlIndexEx index) 129 | { 130 | Run($"DROP INDEX {index.GetName()} ON { GetTableNameWithSchema(index.DeclaringEntityType) }"); 131 | } 132 | 133 | protected override void CreateIndex(SqlIndexEx index) 134 | { 135 | var name = index.GetName(); 136 | var columns = index.Properties; 137 | var newColumns = columns.Select(x => $"{Escape(x.ColumnName())} ASC").ToJoinString(); 138 | var filter = index.GetFilter() == null ? "" : $" WHERE {index.GetFilter()}"; 139 | var indexType = index.Unique ? " UNIQUE " : " NONCLUSTERED "; 140 | Run(@$"CREATE {indexType} INDEX {name} 141 | ON {GetTableNameWithSchema(index.DeclaringEntityType)} ({ newColumns }) 142 | {filter}"); 143 | 144 | } 145 | 146 | protected override bool ModelExists(string tableName, string model) 147 | { 148 | 149 | if (!TableExists(tableName, "dbo")) 150 | { 151 | Run($"CREATE TABLE {tableName} (DateApplied datetime, CurrentModel nvarchar(max), PRIMARY KEY (DateApplied))"); 152 | } 153 | 154 | using (var r = Read($"SELECT TOP 1 CurrentModel FROM {tableName} ORDER BY DateApplied DESC", null)) 155 | { 156 | if (r.Read()) 157 | { 158 | if (r.GetValue("CurrentModel") == model) 159 | return true; 160 | } 161 | } 162 | var tableNameLiteral = TemplateQuery.Literal(tableName); 163 | Run(TemplateQuery.New($"INSERT INTO {tableNameLiteral} (DateApplied, CurrentModel) VALUES ({DateTime.UtcNow}, {model})")); 164 | 165 | return false; 166 | } 167 | } 168 | 169 | public static class EntityTypeExtensions 170 | { 171 | public static string ColumnName(this IProperty property) 172 | { 173 | var name = property.DeclaringEntityType.GetTableName(); 174 | var schema = property.DeclaringEntityType.GetSchemaOrDefault(); 175 | #if NET_STANDARD_2_1 176 | var n = property.GetColumnName(StoreObjectIdentifier.Table(name, null)); 177 | #else 178 | var n = property.GetColumnName(); 179 | #endif 180 | return n; 181 | } 182 | } 183 | 184 | } 185 | -------------------------------------------------------------------------------- /NeuroSpeech.EFCoreAutomaticMigration/ModelMigrationBase.cs: -------------------------------------------------------------------------------- 1 | #nullable enable 2 | using Microsoft.EntityFrameworkCore; 3 | using Microsoft.EntityFrameworkCore.Metadata; 4 | using NeuroSpeech.TemplatedQuery; 5 | using System; 6 | using System.Collections.Generic; 7 | using System.Data.Common; 8 | using System.Linq; 9 | using System.Reflection; 10 | using System.Text; 11 | 12 | namespace NeuroSpeech.EFCoreAutomaticMigration 13 | { 14 | 15 | public class MigrationResult 16 | { 17 | private readonly List tables; 18 | 19 | public MigrationResult(List tables) 20 | { 21 | this.tables = tables; 22 | } 23 | 24 | public IReadOnlyCollection Modifications => tables; 25 | 26 | public string Log 27 | { 28 | get 29 | { 30 | StringBuilder sb = new StringBuilder(); 31 | foreach(var m in tables) 32 | { 33 | sb.AppendLine($"Table {m.TableName} modified."); 34 | foreach(var c in m.columnsAdded) 35 | { 36 | sb.AppendLine($"\tColumn: {c.ColumnName} added."); 37 | } 38 | foreach (var c in m.columnsRenamed) 39 | { 40 | sb.AppendLine($"\tColumn: {c.from.ColumnName} {c.from} renamed to {c.to}."); 41 | } 42 | foreach (var i in m.indexedUpdated) 43 | { 44 | if (i.Dropped) 45 | { 46 | sb.AppendLine($"\tIndex: {i.Index.Name} dropped."); 47 | continue; 48 | } 49 | sb.AppendLine($"\t{i.Index.Name} created."); 50 | } 51 | } 52 | return sb.ToString(); 53 | } 54 | } 55 | } 56 | 57 | public class MigrationFailedException: Exception 58 | { 59 | public MigrationFailedException(string message, Exception innerException) 60 | : base(message, innerException) 61 | { 62 | 63 | } 64 | } 65 | 66 | 67 | public delegate void ColumnChangedDelegate(ModelMigrationBase migration, 68 | Column existing, 69 | DbColumnInfo currentColumn, 70 | string tempName); 71 | 72 | 73 | public abstract class ModelMigrationBase 74 | { 75 | 76 | internal string MigrationsTable = "__AutomaticMigrations"; 77 | 78 | protected internal readonly DbContext context; 79 | private readonly Columns columns; 80 | protected DbTransaction? Transaction { get; private set; } 81 | 82 | internal MigrationEventList handler = new MigrationEventList(); 83 | 84 | internal ColumnChangedDelegate? onColumnChange = null; 85 | 86 | // private bool preventRename = false; 87 | 88 | 89 | public ModelMigrationBase(DbContext context) 90 | { 91 | this.context = context; 92 | this.columns = new Columns(this); 93 | } 94 | 95 | public ModelMigrationBase AddEvent(MigrationEvents events) 96 | { 97 | handler.Add(events); 98 | return this; 99 | } 100 | 101 | internal protected abstract string LoadTableColumns(); 102 | 103 | protected abstract bool IsText(string n); 104 | 105 | protected abstract bool IsDecimal(string n); 106 | 107 | public abstract string Escape(string name); 108 | 109 | 110 | protected abstract string GetTableNameWithSchema(IEntityType entity); 111 | 112 | 113 | 114 | private List GetEntityTypes() 115 | { 116 | var r = new List(); 117 | var all = context.Model.GetEntityTypes(); 118 | var pending = new List(); 119 | var owned = new List(); 120 | foreach(var entity in all) 121 | { 122 | if(entity.BaseType != null) 123 | { 124 | pending.Add(entity); 125 | continue; 126 | } 127 | if(entity.ClrType.GetCustomAttribute() != null) 128 | { 129 | owned.Add(entity); 130 | continue; 131 | } 132 | r.Add(entity); 133 | } 134 | 135 | r.AddRange(pending); 136 | r.AddRange(owned); 137 | return r; 138 | } 139 | 140 | public bool TableExists(string tableName, string? schemaName = null) 141 | { 142 | var infoSchema = this.LoadTableColumns(); 143 | using (var s = Read(infoSchema, new Dictionary { 144 | { "@TableName", tableName } , 145 | { "@SchemaName", schemaName ?? context.Model.GetDefaultSchema() } 146 | })) 147 | { 148 | return s.Read(); 149 | } 150 | } 151 | 152 | protected abstract bool ModelExists(string tableName, string model); 153 | 154 | /// 155 | /// 156 | /// 157 | /// Set it to false for development 158 | /// 159 | public MigrationResult Migrate(bool preventChangeColumn = true) 160 | { 161 | if (preventChangeColumn) 162 | { 163 | this.onColumnChange ??= (m,e,p,t) => throw new NotSupportedException($"Changing column {e.ColumnName}{e} to {p.ColumnName}{p} not supported "); 164 | } 165 | var entities = GetEntityTypes(); 166 | List modifications = new List(); 167 | try 168 | { 169 | context.Database.OpenConnection(); 170 | 171 | using (var tx = context.Database.GetDbConnection().BeginTransaction(System.Data.IsolationLevel.Serializable)) 172 | { 173 | this.Transaction = tx; 174 | 175 | if (!string.IsNullOrEmpty(MigrationsTable)) 176 | { 177 | var name = TemplateQuery.Literal(MigrationsTable); 178 | 179 | var rm = context.Database.GenerateCreateScript(); 180 | 181 | if (ModelExists(MigrationsTable, rm)) 182 | { 183 | return new MigrationResult(modifications) { }; 184 | } 185 | } 186 | 187 | 188 | foreach (var entity in entities) 189 | { 190 | if (entity.ClrType.GetCustomAttribute() != null) 191 | continue; 192 | 193 | var table = new DbTableInfo(entity, Escape); 194 | modifications.Add(table); 195 | MigrateEntity(table); 196 | 197 | } 198 | foreach (var entity in entities) 199 | { 200 | if (entity.ClrType.GetCustomAttribute() != null) 201 | continue; 202 | 203 | var table = modifications.FirstOrDefault(x => x.EntityType == entity); 204 | if(table == null) 205 | { 206 | table = new DbTableInfo(entity, Escape); 207 | modifications.Add(table); 208 | } 209 | PostMigrateEntity(table); 210 | } 211 | tx.Commit(); 212 | } 213 | } catch(Exception ex) 214 | { 215 | throw new MigrationFailedException(ex.Message + "\r\n" + new MigrationResult(modifications).Log, ex); 216 | } 217 | finally 218 | { 219 | context.Database.CloseConnection(); 220 | } 221 | return new MigrationResult(modifications); 222 | } 223 | 224 | public virtual DbCommand CreateCommand(string command, IEnumerable>? plist = null) 225 | { 226 | var cmd = context.Database.GetDbConnection().CreateCommand(); 227 | cmd.CommandText = command; 228 | cmd.Transaction = Transaction; 229 | if (plist != null) 230 | { 231 | foreach (var p in plist) 232 | { 233 | var px = cmd.CreateParameter(); 234 | px.ParameterName = p.Key; 235 | px.Value = p.Value; 236 | cmd.Parameters.Add(px); 237 | } 238 | } 239 | return cmd; 240 | } 241 | 242 | public SqlRowSet Read(TemplateQuery query) 243 | { 244 | var cmd = CreateCommand(query.Text, query.Values); 245 | return new SqlRowSet(cmd, cmd.ExecuteReader()); 246 | } 247 | 248 | 249 | public SqlRowSet Read(string command, Dictionary plist) 250 | { 251 | var cmd = CreateCommand(command, plist); 252 | return new SqlRowSet(cmd, cmd.ExecuteReader()); 253 | } 254 | 255 | public int Run(TemplateQuery query) 256 | { 257 | var cmd = CreateCommand(query.Text, query.Values); 258 | return cmd.ExecuteNonQuery(); 259 | } 260 | 261 | public int Run(string command, Dictionary? plist = null) 262 | { 263 | using (var cmd = CreateCommand(command, plist)) 264 | { 265 | try 266 | { 267 | return cmd.ExecuteNonQuery(); 268 | } 269 | catch (Exception ex) 270 | { 271 | throw new InvalidOperationException($"RunAsync failed for {command}", ex); 272 | } 273 | } 274 | } 275 | 276 | protected virtual void EnsureCreated(DbColumnInfo property, bool forceDefault) 277 | { 278 | if (property.Property.DeclaringEntityType != property.Table.EntityType) 279 | return; 280 | 281 | var existing = columns[property]; 282 | if (existing != null && existing.IsSame(property)) 283 | { 284 | return; 285 | } 286 | 287 | if (forceDefault) 288 | { 289 | if (!property.IsNullable) 290 | { 291 | if (property.DefaultValue == null) { 292 | if(existing != null) 293 | { 294 | property.Table.columnsRenamed.Add((existing, property)); 295 | } else 296 | { 297 | property.Table.columnsAdded.Add(property); 298 | } 299 | throw new InvalidOperationException($"You must specify the default value for property {property} as table contains rows"); 300 | } 301 | } 302 | } 303 | 304 | if (existing != null) 305 | { 306 | property.Table.columnsRenamed.Add((existing, property)); 307 | string postFix = $"_{DateTime.UtcNow.Ticks}"; 308 | // rename... 309 | //if (preventRename) 310 | //{ 311 | // throw new InvalidOperationException($"Renaming of existing column not allowed from {existing.ColumnName} {existing} to {property.ColumnName}{property}"); 312 | //} 313 | //RenameColumn(property, postFix); 314 | existing.Table = property.Table; 315 | if (this.onColumnChange != null) 316 | { 317 | existing.ColumnName = property.ColumnName; 318 | existing.TableNameAndColumnName = property.TableNameAndColumnName; 319 | existing.EscapedTableNameAndColumnName = property.EscapedTableNameAndColumnName; 320 | existing.EscapedColumnName = property.EscapedColumnName; 321 | this.onColumnChange(this, existing, property, postFix); 322 | handler.OnColumnChanged(property, existing); 323 | return; 324 | } 325 | else 326 | { 327 | RenameColumn(property, postFix); 328 | } 329 | } 330 | else 331 | { 332 | property.Table.columnsAdded.Add(property); 333 | } 334 | 335 | AddColumn(property); 336 | 337 | handler.OnColumnAdded(property, existing); 338 | 339 | } 340 | 341 | internal protected abstract string LoadIndexes(IEntityType entity); 342 | 343 | protected void MigrateEntity(DbTableInfo table) 344 | { 345 | 346 | this.columns.Clear(table); 347 | 348 | if (!this.columns.Exists(table)) 349 | { 350 | var keys = table.EntityType.GetProperties().Where(x => x.IsKey()) 351 | .Select(x => new DbColumnInfo(table, x, Escape)) 352 | .ToList(); 353 | CreateTable(table, keys); 354 | handler.OnTableCreated(table); 355 | } 356 | 357 | var forceDefault = HasAnyRows(table); 358 | 359 | foreach (var property in table.EntityType.GetProperties().Where(x => !x.IsKey())) 360 | { 361 | var column = new DbColumnInfo(table, property, Escape); 362 | EnsureCreated(column, forceDefault); 363 | } 364 | } 365 | 366 | protected void PostMigrateEntity(DbTableInfo table) 367 | { 368 | // create indexes... 369 | foreach (var index in table.EntityType.GetIndexes()) 370 | { 371 | // if all properties are part of primary key 372 | // and it is unique 373 | // we should ignore it... 374 | if (index.IsUnique) 375 | { 376 | if (index.Properties.All(x => x.IsKey())) 377 | continue; 378 | } 379 | var i = new SqlIndexEx(table, index, this); 380 | EnsureCreated(i); 381 | } 382 | 383 | handler.OnTableModified(table, table.columnsAdded, table.columnsRenamed, table.indexedUpdated); 384 | } 385 | 386 | internal protected abstract bool HasAnyRows(DbTableInfo table); 387 | 388 | protected abstract void CreateTable(DbTableInfo entity, List keys); 389 | 390 | protected void EnsureCreated(SqlIndexEx index) 391 | { 392 | var existing = columns[index]; 393 | if (existing != null && existing.IsSame(index, this)) 394 | { 395 | return; 396 | } 397 | 398 | if (existing != null) 399 | { 400 | // rename... 401 | DropIndex(index); 402 | handler.OnIndexDropped(index); 403 | } 404 | 405 | CreateIndex(index); 406 | handler.OnIndexCreated(index); 407 | index.Table.indexedUpdated.Add((existing != null, index)); 408 | } 409 | 410 | protected abstract void DropIndex(SqlIndexEx index); 411 | protected abstract void CreateIndex(SqlIndexEx index); 412 | protected abstract void AddColumn(DbColumnInfo property); 413 | 414 | protected abstract void RenameColumn(DbColumnInfo property, string postFix); 415 | 416 | 417 | protected abstract string ToColumn(DbColumnInfo column); 418 | } 419 | } -------------------------------------------------------------------------------- /NeuroSpeech.EFCoreAutomaticMigration/NeuroSpeech.EFCoreAutomaticMigration.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netstandard2.0;netstandard2.1 5 | NeuroSpeech.EFCoreAutomaticMigration 6 | Akash Kava 7 | NeuroSpeech Technologies Pvt Ltd 8 | EF Core Automatic Migration 9 | Automatic Migrations for EF Core 10 | https://github.com/neurospeech/ef-core-automatic-migration/main/master/LICENSE 11 | https://github.com/neurospeech/ef-core-automatic-migration 12 | https://github.com/neurospeech/ef-core-automatic-migration 13 | EF, Core, Live Migration, Automatic, Migration 14 | 1.0.2 15 | true 16 | latest 17 | true 18 | true 19 | snupkg 20 | True 21 | embedded 22 | $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb 23 | 24 | 25 | 26 | NET_STANDARD_2_1 27 | 28 | 29 | NET_STANDARD_2_0 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | all 46 | runtime; build; native; contentfiles; analyzers 47 | 48 | 49 | 50 | 51 | -------------------------------------------------------------------------------- /NeuroSpeech.EFCoreAutomaticMigration/OldName.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace NeuroSpeech.EFCoreAutomaticMigration 4 | { 5 | public class OldNameAttribute : Attribute 6 | { 7 | public string Name { get; set; } 8 | 9 | public OldNameAttribute(string name) 10 | { 11 | Name = name; 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /NeuroSpeech.EFCoreAutomaticMigration/PropertyExtensions.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using Microsoft.EntityFrameworkCore.Metadata; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Text; 6 | 7 | namespace NeuroSpeech.EFCoreAutomaticMigration 8 | { 9 | public static class PropertyExtensions 10 | { 11 | 12 | public static string GetSchemaOrDefault(this IEntityType type) 13 | { 14 | return type.GetSchema() ?? type.GetDefaultSchema() ?? "dbo"; 15 | } 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /NeuroSpeech.EFCoreAutomaticMigration/Scripts.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace NeuroSpeech.EFCoreAutomaticMigration 6 | { 7 | class Scripts 8 | { 9 | 10 | public const string SqlServerGetSchema = @"SELECT 11 | IC.ORDINAL_POSITION as Ordinal, 12 | IC.COLUMN_NAME as ColumnName, 13 | IC.COLUMN_DEFAULT as ColumnDefault, 14 | (CASE IC.IS_NULLABLE WHEN 'YES' THEN 1 ELSE 0 END) as IsNullable, 15 | IC.DATA_TYPE as DataType, 16 | IC.CHARACTER_MAXIMUM_LENGTH as DataLength, 17 | IC.NUMERIC_PRECISION as NumericPrecision, 18 | IC.NUMERIC_SCALE as NumericScale, 19 | COLUMNPROPERTY(object_id(TABLE_SCHEMA+'.'+TABLE_NAME), COLUMN_NAME, 'IsIdentity') as IsIdentity, 20 | (SELECT 1 FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE as CCU WHERE 21 | CCU.COLUMN_NAME = IC.COLUMN_NAME AND 22 | CCU.TABLE_NAME = IC.TABLE_NAME AND EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS as TC WHERE 23 | TC.CONSTRAINT_NAME = CCU.CONSTRAINT_NAME AND 24 | TC.TABLE_NAME=IC.TABLE_NAME AND TC.CONSTRAINT_TYPE='PRIMARY KEY') ) as IsPrimaryKey 25 | FROM INFORMATION_SCHEMA.COLUMNS AS IC WHERE IC.TABLE_NAME=@TableName AND IC.TABLE_SCHEMA=@SchemaName;"; 26 | 27 | 28 | public const string SqlServerGetIndexes = @"SELECT 29 | t.name as TableName, 30 | ind.name as IndexName, 31 | ind.index_id as IndexId, 32 | ic.index_column_id as ColumnId, 33 | col.name as ColumnName, 34 | ind.filter_definition as [Filter] 35 | FROM 36 | sys.indexes ind 37 | INNER JOIN 38 | sys.index_columns ic ON ind.object_id = ic.object_id and ind.index_id = ic.index_id 39 | INNER JOIN 40 | sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id 41 | INNER JOIN 42 | sys.tables t ON ind.object_id = t.object_id 43 | INNER JOIN 44 | sys.schemas s ON t.schema_id = s.schema_id 45 | WHERE 46 | ind.is_primary_key = 0 47 | AND ind.is_unique_constraint = 0 48 | AND t.is_ms_shipped = 0 49 | AND t.name = @TableName 50 | AND s.name = @SchemaName 51 | ORDER BY 52 | t.name, ind.name, ind.index_id, ic.index_column_id;"; 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /NeuroSpeech.EFCoreAutomaticMigration/SqlIndexEx.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using Microsoft.EntityFrameworkCore.Metadata; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | 6 | namespace NeuroSpeech.EFCoreAutomaticMigration 7 | { 8 | public class SqlIndexEx 9 | { 10 | public readonly string Name; 11 | public readonly IEntityType DeclaringEntityType; 12 | public readonly IReadOnlyList Properties; 13 | public readonly IReadOnlyList IncludedProperties; 14 | public readonly string Filter; 15 | public readonly bool Unique; 16 | public readonly DbTableInfo Table; 17 | public readonly IIndex Index; 18 | 19 | public SqlIndexEx(DbTableInfo table, IIndex index, ModelMigrationBase modelMigration) 20 | { 21 | this.Table = table; 22 | this.Index = index; 23 | #if NETSTANDARD2_1 24 | this.Name = index.GetDatabaseName(); 25 | #else 26 | this.Name = index.GetName(); 27 | #endif 28 | this.DeclaringEntityType = index.DeclaringEntityType; 29 | this.Properties = index.Properties; 30 | this.IncludedProperties = index.GetIncludeProperties(); 31 | this.Filter = index.GetFilter(); 32 | this.Unique = index.IsUnique; 33 | 34 | if(this.Filter == null) 35 | { 36 | // create filter based on nullable foreign key... 37 | 38 | var nullables = this.Properties.Where(x => x.IsColumnNullable() && x.IsForeignKey()); 39 | if (index.DeclaringEntityType.BaseType != null) 40 | { 41 | nullables = this.Properties; 42 | } 43 | if(nullables.Any()) 44 | { 45 | var columns = string.Join(" AND ", 46 | nullables.Select(x => $"{modelMigration.Escape(x.ColumnName())} IS NOT NULL") 47 | ); 48 | this.Filter = $"({columns})"; 49 | } 50 | 51 | } 52 | } 53 | 54 | public string GetName() => Name; 55 | public string GetFilter() => Filter; 56 | } 57 | } -------------------------------------------------------------------------------- /NeuroSpeech.EFCoreAutomaticMigration/SqlRowSet.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data.Common; 3 | using System.Threading.Tasks; 4 | 5 | namespace NeuroSpeech.EFCoreAutomaticMigration 6 | { 7 | public class SqlRowSet : IDisposable 8 | { 9 | 10 | DbDataReader reader = null; 11 | DbCommand command = null; 12 | 13 | public SqlRowSet(DbCommand command, DbDataReader reader) 14 | { 15 | this.reader = reader; 16 | this.command = command; 17 | } 18 | 19 | public bool Read() 20 | { 21 | return reader.Read(); 22 | } 23 | 24 | public T GetValue(string name) 25 | { 26 | int ordinal = reader.GetOrdinal(name); 27 | if (reader.IsDBNull(ordinal)) 28 | { 29 | return default(T); 30 | } 31 | object val = reader.GetValue(ordinal); 32 | Type type = Nullable.GetUnderlyingType(typeof(T)); 33 | if (type == null) 34 | { 35 | type = typeof(T); 36 | } 37 | if (val.GetType() != type) 38 | val = (T)Convert.ChangeType(val, type); 39 | return (T)val; 40 | } 41 | 42 | public void Dispose() 43 | { 44 | 45 | this.reader.Dispose(); 46 | this.command.Dispose(); 47 | } 48 | 49 | internal object GetRawValue(string name) 50 | { 51 | int ordinal = reader.GetOrdinal(name); 52 | if (reader.IsDBNull(ordinal)) 53 | { 54 | return null; 55 | } 56 | return reader.GetValue(ordinal); 57 | } 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /NeuroSpeech.EFCoreAutomaticMigration/SqlServerMigrationHelper.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using Microsoft.EntityFrameworkCore.Metadata; 3 | using System; 4 | using System.Collections.Generic; 5 | using System.Data.Common; 6 | using System.Linq; 7 | using System.Threading.Tasks; 8 | 9 | namespace NeuroSpeech.EFCoreAutomaticMigration 10 | { 11 | public static class SqlServerMigrationExtensions 12 | { 13 | 14 | /// 15 | /// 16 | /// 17 | /// 18 | /// 19 | /// Set to null to not check for old cache 20 | /// 21 | public static T WithMigrationTable(this T migration, string name = null) 22 | where T : ModelMigrationBase 23 | { 24 | migration.MigrationsTable = name; 25 | return migration; 26 | } 27 | 28 | 29 | public static T OnColumnChange(this T migration, ColumnChangedDelegate d) 30 | where T: ModelMigrationBase 31 | { 32 | migration.onColumnChange = d; 33 | return migration; 34 | } 35 | 36 | public static ModelMigrationBase MigrationForSqlServer(this DbContext context) 37 | { 38 | return new ModelMigration(context); 39 | } 40 | } 41 | 42 | } 43 | -------------------------------------------------------------------------------- /NeuroSpeech.EFCoreAutomaticMigration/StringExtensions.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using Microsoft.EntityFrameworkCore.Metadata; 3 | using System; 4 | using System.Linq; 5 | using System.Collections.Generic; 6 | using System.Reflection; 7 | using System.Text; 8 | 9 | namespace NeuroSpeech.EFCoreAutomaticMigration 10 | { 11 | public static class StringExtensions 12 | { 13 | 14 | public static (int? length, int? @decimal) GetColumnDataLength(this IProperty property) 15 | { 16 | string type = property.GetColumnType(); 17 | var tokens = type.Split('('); 18 | if(tokens.Length > 1) 19 | { 20 | tokens = tokens[1] 21 | .Trim() 22 | .Trim(')') 23 | .Split(','); 24 | if (tokens.Length > 1) 25 | { 26 | var first = tokens[0].Trim(); 27 | var second = tokens[1].Trim(); 28 | if(int.TryParse(first, out var f1)) 29 | { 30 | if(int.TryParse(second, out var s1)) 31 | { 32 | return (f1, s1); 33 | } 34 | return (f1, null); 35 | } 36 | } 37 | if(tokens.Length == 1) 38 | { 39 | if (int.TryParse(tokens[0], out var l)) 40 | return (l, null); 41 | } 42 | } 43 | return (null, null); 44 | } 45 | 46 | public static string GetColumnTypeForSql(this IProperty property) { 47 | 48 | string type = property.GetColumnType(); 49 | return type.Split('(')[0].Trim(); 50 | } 51 | 52 | public static string[] GetOldNames(this IProperty property) 53 | { 54 | if (property.PropertyInfo == null) 55 | { 56 | return null; 57 | } 58 | var oa = property.PropertyInfo.GetCustomAttributes(); 59 | 60 | return oa.Select(x=>x.Name).ToArray(); 61 | } 62 | 63 | public static string ToJoinString(this IEnumerable list, string separator = ", ") { 64 | return string.Join(separator, list); 65 | } 66 | 67 | public static string ToJoinString( 68 | this IEnumerable list, 69 | Func escape, 70 | string separator = ", ") 71 | { 72 | return string.Join(separator, list.Select(x => escape(x))); 73 | } 74 | 75 | } 76 | } 77 | -------------------------------------------------------------------------------- /PostGreSql/NeuroSpeech.EFCoreAutomaticMigration.PostGreSql/MigrationHelperExtensions.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using NeuroSpeech.EFCoreAutomaticMigration.PostGreSql; 3 | 4 | namespace NeuroSpeech.EFCoreAutomaticMigration 5 | { 6 | public static class MigrationHelperExtensions 7 | { 8 | public static ModelMigrationBase MigrationForPostGreSql(this DbContext context) 9 | { 10 | return new PostGreSqlMigration(context); 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /PostGreSql/NeuroSpeech.EFCoreAutomaticMigration.PostGreSql/NeuroSpeech.EFCoreAutomaticMigration.PostGreSql.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netstandard2.0;netstandard2.1 5 | NeuroSpeech.EFCoreAutomaticMigration.PostGreSql 6 | Akash Kava 7 | NeuroSpeech Technologies Pvt Ltd 8 | EF Core Automatic Migration 9 | Automatic Migrations for EF Core for PostGreSql 10 | https://github.com/neurospeech/ef-core-automatic-migration/main/master/LICENSE 11 | https://github.com/neurospeech/ef-core-automatic-migration 12 | https://github.com/neurospeech/ef-core-automatic-migration 13 | EF, Core, Live Migration, Automatic, Migration,PostGreSql 14 | 1.0.2 15 | true 16 | latest 17 | true 18 | true 19 | snupkg 20 | True 21 | embedded 22 | $(AllowedOutputExtensionsInPackageBuildOutputFolder);.pdb 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | all 40 | runtime; build; native; contentfiles; analyzers 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | -------------------------------------------------------------------------------- /PostGreSql/NeuroSpeech.EFCoreAutomaticMigration.PostGreSql/PostGreSqlMigration.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using Microsoft.EntityFrameworkCore.Metadata; 3 | using NeuroSpeech.TemplatedQuery; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.ComponentModel.DataAnnotations.Schema; 7 | using System.Linq; 8 | using System.Reflection; 9 | using System.Text; 10 | 11 | namespace NeuroSpeech.EFCoreAutomaticMigration.PostGreSql 12 | { 13 | public class PostGreSqlMigration : ModelMigrationBase 14 | { 15 | public PostGreSqlMigration(DbContext context) : base(context) 16 | { 17 | } 18 | 19 | public override string Escape(string name) 20 | { 21 | return $"\"{name.Trim('\"')}\""; 22 | } 23 | 24 | protected override void AddColumn(DbColumnInfo property) 25 | { 26 | Run($"ALTER TABLE {property.Table.EscapedNameWithSchema} ADD " + ToColumn(property)); 27 | } 28 | 29 | protected override void CreateIndex(SqlIndexEx index) 30 | { 31 | var name = index.Name; 32 | var tableName = GetTableNameWithSchema(index.DeclaringEntityType); 33 | var columns = string.Join(", ", index.Properties.Select(x => Escape(x.ColumnName()))); 34 | string filter = index.Filter == null ? "" : " WHERE " + index.Filter; 35 | Run($"CREATE INDEX {name} ON {tableName} ({ columns }) {filter}"); 36 | } 37 | 38 | protected override void DropIndex(SqlIndexEx index) 39 | { 40 | Run(TemplateQuery.New($"DROP INDEX IF EXISTS {Literal.DoubleQuoted(index.Name)}")); 41 | } 42 | 43 | protected override void CreateTable(DbTableInfo entity, List pkeys) 44 | { 45 | var tableName = entity.EscapedNameWithSchema; 46 | 47 | string createTable = $" CREATE TABLE {tableName} ({ string.Join(",", pkeys.Select(c => ToColumn(c))) }, " + 48 | $"CONSTRAINT {entity.TableName}_pkey PRIMARY KEY( { string.Join(", ", pkeys.Select(x => x.EscapedColumnName)) } ))"; 49 | 50 | Run(createTable); 51 | } 52 | 53 | protected override string GetTableNameWithSchema(IEntityType entity) 54 | { 55 | return Escape(entity.GetSchemaOrDefault()) + "." + Escape(entity.GetTableName()); 56 | } 57 | 58 | private static string[] textTypes = new[] { "character varying", "varchar" }; 59 | 60 | protected override bool IsText(string n) => textTypes.Any(a => a.Equals(n, StringComparison.OrdinalIgnoreCase)); 61 | 62 | protected override bool IsDecimal(string n) => n.Equals("numeric", StringComparison.OrdinalIgnoreCase); 63 | 64 | protected override string LoadTableColumns() 65 | { 66 | return Scripts.SqlServerGetSchema; 67 | } 68 | 69 | protected override void RenameColumn(DbColumnInfo property, string postFix) 70 | { 71 | var table = property.Table.EscapedNameWithSchema; 72 | var name = property.ColumnName; 73 | var newName = name + postFix; 74 | Run( $"ALTER TABLE {table} RENAME {Escape(name)} TO {Escape(newName)}"); 75 | } 76 | 77 | protected override string ToColumn(DbColumnInfo c) 78 | { 79 | var name = $"{c.EscapedColumnName} {c.DataType}"; 80 | 81 | if (c.DataLength != null) 82 | { 83 | if (c.DataLength > 0 && c.DataLength < int.MaxValue) 84 | { 85 | name += $"({ c.DataLength })"; 86 | } 87 | else 88 | { 89 | name += "(MAX)"; 90 | } 91 | } 92 | if (c.Precision != null) 93 | { 94 | var np = 18; 95 | var nps = 2; 96 | 97 | name += $"({ np },{ nps })"; 98 | } 99 | if (!c.IsKey) 100 | { 101 | // lets allow nullable to every field... 102 | if (!c.IsNullable) 103 | { 104 | name += " NOT NULL "; 105 | } 106 | } 107 | 108 | if (c.IsIdentity) 109 | { 110 | name += " GENERATED ALWAYS AS IDENTITY "; 111 | } 112 | 113 | if (!string.IsNullOrWhiteSpace(c.DefaultValue)) 114 | { 115 | name += " DEFAULT " + c.DefaultValue; 116 | } 117 | return name; 118 | 119 | } 120 | 121 | protected override string LoadIndexes(IEntityType entity) 122 | { 123 | return NeuroSpeech.EFCoreAutomaticMigration.PostGreSql.Scripts.SqlServerGetIndexes; 124 | } 125 | 126 | protected override bool HasAnyRows(DbTableInfo table) 127 | { 128 | var sql = $"SELECT 1 FROM {table.EscapedNameWithSchema} LIMIT 1"; 129 | var cmd = CreateCommand(sql); 130 | var i = cmd.ExecuteScalar(); 131 | if (i == null) 132 | return false; 133 | return true; 134 | } 135 | 136 | protected override bool ModelExists(string tableName, string model) 137 | { 138 | return false; 139 | } 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /PostGreSql/NeuroSpeech.EFCoreAutomaticMigration.PostGreSql/PostGreSqlServerMigrationHelper.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using Microsoft.EntityFrameworkCore.Metadata; 3 | using NeuroSpeech.TemplatedQuery; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Data.Common; 7 | using System.Linq; 8 | using System.Threading.Tasks; 9 | 10 | namespace NeuroSpeech.EFCoreAutomaticMigration.PostGreSql 11 | { 12 | 13 | internal class PostGreSqlMigrationHelper : MigrationHelper 14 | { 15 | 16 | internal PostGreSqlMigrationHelper(DbContext context) : base(context) 17 | { 18 | } 19 | 20 | protected override string GetDefaultSchema() => "public"; 21 | 22 | public string RenameColumn(in TableName table, string oldName, string newName) 23 | { 24 | return $"ALTER TABLE {table.EscapedFullName} RENAME {Escape(oldName)} TO {Escape(newName)}"; 25 | } 26 | 27 | public override string Escape(string name) => 28 | $"\"{name.Trim('\"')}\""; 29 | 30 | public override DbCommand CreateCommand(string command, IEnumerable> plist = null) 31 | { 32 | var cmd = context.Database.GetDbConnection().CreateCommand(); 33 | cmd.CommandText = command; 34 | cmd.Transaction = Transaction; 35 | if (plist != null) 36 | { 37 | foreach (var p in plist) 38 | { 39 | var px = cmd.CreateParameter(); 40 | px.ParameterName = p.Key; 41 | px.Value = p.Value; 42 | cmd.Parameters.Add(px); 43 | } 44 | } 45 | return cmd; 46 | } 47 | 48 | public override List GetCommonSchema(in TableName table) 49 | { 50 | 51 | 52 | List columns = new List(); 53 | string sqlColumns = Scripts.SqlServerGetSchema; 54 | 55 | using (var reader = Read(sqlColumns, new Dictionary { { "@TableName", table.Name } })) 56 | { 57 | 58 | 59 | while (reader.Read()) 60 | { 61 | SqlColumn col = new SqlColumn(); 62 | 63 | col.ColumnName = reader.GetValue("ColumnName"); 64 | col.IsPrimaryKey = reader.GetValue("IsPrimaryKey"); 65 | col.IsNullable = reader.GetValue("IsNullable"); 66 | col.ColumnDefault = reader.GetValue("ColumnDefault"); 67 | col.DataType = reader.GetValue("DataType"); 68 | col.DataLength = reader.GetValue("DataLength"); 69 | col.NumericPrecision = reader.GetValue("NumericPrecision"); 70 | col.NumericScale = reader.GetValue("NumericScale"); 71 | col.IsIdentity = reader.GetValue("IsIdentity"); 72 | 73 | columns.Add(col); 74 | } 75 | 76 | } 77 | return columns; 78 | } 79 | 80 | public override void SyncSchema(in TableName table, List columns) 81 | { 82 | 83 | var pkeys = columns.Where(x => x.IsPrimaryKey).ToList(); 84 | 85 | string createTable = $" CREATE TABLE IF NOT EXISTS {table.EscapedFullName} ({ string.Join(",", pkeys.Select(c => ToColumn(c))) }, " + 86 | $"CONSTRAINT {table.Name}_pkey PRIMARY KEY( { string.Join(", ", pkeys.Select(x=> this.Escape(x.ColumnName) )) } ))"; 87 | 88 | Run(createTable); 89 | 90 | var destColumns = GetCommonSchema(table); 91 | 92 | List columnsToAdd = new List(); 93 | 94 | foreach (var column in columns) 95 | { 96 | var dest = destColumns.FirstOrDefault(x => x.ColumnName == column.ColumnName); 97 | if (dest == null) 98 | { 99 | 100 | // look for old names.... 101 | dest = destColumns.FirstOrDefault(x => column.OldNames != null && 102 | column.OldNames.Any( oc => oc.EqualsIgnoreCase(x.ColumnName) )); 103 | 104 | if (dest == null) 105 | { 106 | columnsToAdd.Add(column); 107 | continue; 108 | } 109 | 110 | Run(RenameColumn(table, dest.ColumnName, column.ColumnName)); 111 | dest.ColumnName = column.ColumnName; 112 | 113 | } 114 | if (dest.Equals(column)) 115 | continue; 116 | 117 | 118 | columnsToAdd.Add(column); 119 | 120 | long m = DateTime.UtcNow.Ticks; 121 | 122 | column.CopyFrom = $"{dest.ColumnName}_{m}"; 123 | 124 | Run(RenameColumn(table, dest.ColumnName, column.CopyFrom)); 125 | 126 | } 127 | 128 | foreach (var column in columnsToAdd) 129 | { 130 | Run($"ALTER TABLE {table.EscapedFullName} ADD " + ToColumn(column)); 131 | } 132 | 133 | Console.WriteLine($"Table {table} sync complete"); 134 | 135 | 136 | var copies = columns.Where(x => x.CopyFrom != null).ToList(); 137 | 138 | if (copies.Any()) { 139 | foreach (var copy in copies) 140 | { 141 | var update = $"UPDATE {table.EscapedFullName} SET {this.Escape(copy.ColumnName)} = {this.Escape(copy.CopyFrom)};"; 142 | Run(update); 143 | } 144 | } 145 | } 146 | 147 | private static string[] textTypes = new[] { "character varying", "varchar" }; 148 | 149 | private static bool IsText(string n) => textTypes.Any(a => a.Equals(n, StringComparison.OrdinalIgnoreCase)); 150 | 151 | private static bool IsDecimal(string n) => n.Equals("numeric", StringComparison.OrdinalIgnoreCase); 152 | 153 | private string ToColumn(SqlColumn c) 154 | { 155 | var name = $"{this.Escape(c.ColumnName)} {c.DataType}"; 156 | if (IsText(c.DataType)) 157 | { 158 | if (c.DataLength > 0 && c.DataLength < int.MaxValue) 159 | { 160 | name += $"({ c.DataLength })"; 161 | } 162 | else 163 | { 164 | name += "(MAX)"; 165 | } 166 | } 167 | if (IsDecimal(c.DataType)) 168 | { 169 | var np = c.NumericPrecision ?? 18; 170 | var nps = c.NumericScale ?? 2; 171 | 172 | name += $"({ np },{ nps })"; 173 | } 174 | if (!c.IsPrimaryKey) 175 | { 176 | // lets allow nullable to every field... 177 | if (c.IsNullable) 178 | { 179 | // name += " NULL "; 180 | } 181 | else { 182 | name += " NOT NULL "; 183 | } 184 | } 185 | else 186 | { 187 | //name += " PRIMARY KEY "; 188 | if (c.IsIdentity) { 189 | name += " GENERATED ALWAYS AS IDENTITY "; 190 | } 191 | } 192 | if (!string.IsNullOrWhiteSpace(c.ColumnDefault)) 193 | { 194 | name += " DEFAULT " + c.ColumnDefault; 195 | } 196 | return name; 197 | } 198 | 199 | protected override void SyncIndexes(in TableName table, IEnumerable indexes) 200 | { 201 | 202 | 203 | var allIndexes = indexes.Select(x => new SqlIndex{ 204 | Name = x.GetName(), 205 | Columns = x.Properties.Select(p => p.GetColumnName()).ToArray() 206 | }); 207 | 208 | EnsureIndexes(in table, allIndexes); 209 | 210 | 211 | 212 | } 213 | 214 | private void EnsureIndexes(in TableName table, IEnumerable allIndexes) 215 | { 216 | var destIndexes = GetIndexes(table); 217 | foreach (var index in allIndexes) 218 | { 219 | 220 | var name = index.Name; 221 | var columns = index.Columns; 222 | 223 | var newColumns = columns.Select(x => $"{Escape(x)}").ToJoinString(); 224 | 225 | var existing = destIndexes.FirstOrDefault(x => x.Name.EqualsIgnoreCase(name)); 226 | if (existing != null) 227 | { 228 | // see if all are ok... 229 | var existingColumns = existing.Columns.Select(x => $"{Escape(x)}").ToJoinString(Escape); 230 | 231 | if (existingColumns.EqualsIgnoreCase(newColumns)) 232 | continue; 233 | 234 | // rename old index... 235 | //var n = $"{name}_{System.DateTime.UtcNow.Ticks}"; 236 | 237 | //Run($"EXEC sp_rename @FromName, @ToName, @Type", new Dictionary { 238 | // { "@FromName", table.EscapedFullName + "." + name }, 239 | // { "@ToName", n}, 240 | // { "@Type", "INDEX" } 241 | //}); 242 | 243 | /// delete old... 244 | Run(TemplatedQuery.TemplateQuery.New($"DROP INDEX IF EXISTS {Literal.DoubleQuoted(existing.Name)}")); 245 | } 246 | 247 | // lets create index... 248 | 249 | Run($"CREATE INDEX {name} ON {table.EscapedFullName} ({ newColumns })"); 250 | 251 | 252 | } 253 | } 254 | 255 | public override List GetIndexes(in TableName table) 256 | { 257 | List list = new List(); 258 | using (var reader = Read(Scripts.SqlServerGetIndexes, new Dictionary { 259 | { "@TableName", table.Name } 260 | })) { 261 | 262 | while (reader.Read()) { 263 | var index = new SqlIndex(); 264 | 265 | index.Name = reader.GetValue("IndexName"); 266 | index.Columns = new string[] { 267 | reader.GetValue("ColumnName") 268 | }; 269 | 270 | list.Add(index); 271 | } 272 | 273 | list = list.GroupBy(x => x.Name).Select(x => new SqlIndex { 274 | Name = x.Key, 275 | Columns = x.SelectMany( c => c.Columns ).Select( c => Escape(c) ).ToArray() 276 | }).ToList(); 277 | 278 | } 279 | return list; 280 | } 281 | 282 | protected override void SyncIndexes(in TableName table, IEnumerable fkeys) 283 | { 284 | var allIndexes = fkeys.Select(x => new SqlIndex 285 | { 286 | Name = "IX" + x.GetConstraintName(), 287 | Columns = x.Properties.Select(p => p.GetColumnName()).ToArray() 288 | }); 289 | 290 | EnsureIndexes(table, allIndexes); 291 | } 292 | } 293 | } 294 | -------------------------------------------------------------------------------- /PostGreSql/NeuroSpeech.EFCoreAutomaticMigration.PostGreSql/Scripts.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text; 4 | 5 | namespace NeuroSpeech.EFCoreAutomaticMigration.PostGreSql 6 | { 7 | class Scripts 8 | { 9 | public const string SqlServerGetSchema = @"select 10 | ISC.ordinal_position as Ordinal, 11 | ISC.column_name as ColumnName, 12 | ISC.column_default as ColumnDefault, 13 | (ISC.is_nullable <> 'NO') as IsNullable, 14 | ISC.data_type as DataType, 15 | ISC.character_maximum_length as DataLength, 16 | ISC.numeric_precision as NumericPrecision, 17 | ISC.numeric_scale as NumericScale, 18 | (ISC.is_identity = 'YES') as IsIdentity, 19 | (SELECT 1 FROM information_schema.CONSTRAINT_COLUMN_USAGE as CCU 20 | WHERE CCU.column_name = ISC.column_name 21 | AND CCU.table_name = ISC.table_name 22 | AND EXISTS (SELECT 1 FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS as TC 23 | WHERE TC.constraint_name= CCU.constraint_name 24 | AND TC.constraint_type = 'PRIMARY KEY') 25 | ) as IsPrimaryKey 26 | from information_schema.columns as ISC 27 | where ISC.table_name = @TableName"; 28 | 29 | public const string SqlServerGetIndexes = @"select 30 | t.relname as TableName, 31 | i.relname as IndexName, 32 | i.oid as IndexId, 33 | a.attname as ColumnName, 34 | a.atttypid as ColumnId 35 | from 36 | pg_class t, 37 | pg_class i, 38 | pg_index ix, 39 | pg_attribute a 40 | where 41 | t.oid = ix.indrelid 42 | and i.oid = ix.indexrelid 43 | and a.attrelid = t.oid 44 | and a.attnum = ANY(ix.indkey) 45 | and t.relkind = 'r' 46 | and t.relname = @TableName 47 | order by 48 | t.relname, 49 | i.relname;"; 50 | 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /PostGreSql/PostGreSqlMigrationConsole/Models/Account.cs: -------------------------------------------------------------------------------- 1 | using NeuroSpeech.EFCoreAutomaticMigration; 2 | using System.ComponentModel.DataAnnotations; 3 | using System.ComponentModel.DataAnnotations.Schema; 4 | 5 | namespace LiveMigrationConsole.Models 6 | { 7 | [Table("Accounts")] 8 | public class Account 9 | { 10 | 11 | [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] 12 | public long AccountID { get; set; } 13 | 14 | [MaxLength(200)] 15 | [Index] 16 | [OldName("AccountName")] 17 | public string DisplayName { get; set; } 18 | 19 | [MaxLength(10)] 20 | public string AccountType { get; set; } 21 | 22 | [InverseProperty(nameof(Product.Vendor))] 23 | public Product[] VendorProducts { get; set; } 24 | 25 | public decimal Balance { get; set; } 26 | 27 | public decimal? Total { get; set; } 28 | } 29 | 30 | 31 | [Table("Talents")] 32 | public class Talent { 33 | 34 | [Key, DatabaseGenerated(DatabaseGeneratedOption.None)] 35 | public long TalentID { get; set; } 36 | 37 | } 38 | } -------------------------------------------------------------------------------- /PostGreSql/PostGreSqlMigrationConsole/Models/Config.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations; 2 | using System.ComponentModel.DataAnnotations.Schema; 3 | 4 | namespace LiveMigrationConsole.Models 5 | { 6 | [Table("Configs")] 7 | public class Config 8 | { 9 | 10 | [Key, MaxLength(20)] 11 | public string Key { get; set; } 12 | 13 | public string Value { get; set; } 14 | 15 | } 16 | } -------------------------------------------------------------------------------- /PostGreSql/PostGreSqlMigrationConsole/Models/ERPContext.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.EntityFrameworkCore; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text; 5 | 6 | namespace LiveMigrationConsole.Models 7 | { 8 | public class ERPContext : DbContext 9 | { 10 | 11 | public ERPContext(DbContextOptions options):base(options) 12 | { 13 | 14 | } 15 | 16 | public DbSet Products { get; set; } 17 | 18 | public DbSet Accounts { get; set; } 19 | 20 | public DbSet Configs { get; set; } 21 | 22 | public DbSet Talents { get; set; } 23 | 24 | public DbSet Features { get; set; } 25 | 26 | protected override void OnModelCreating(ModelBuilder modelBuilder) 27 | { 28 | modelBuilder.Entity().HasKey("ProductID", "FeatureID"); 29 | } 30 | 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /PostGreSql/PostGreSqlMigrationConsole/Models/Product.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations; 2 | using System.ComponentModel.DataAnnotations.Schema; 3 | 4 | namespace LiveMigrationConsole.Models 5 | { 6 | [Table("Products")] 7 | public class Product 8 | { 9 | 10 | [Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)] 11 | public long ProductID { get; set; } 12 | 13 | 14 | public long VendorID { get; set; } 15 | 16 | [ForeignKey(nameof(VendorID))] 17 | [InverseProperty(nameof(Models.Account.VendorProducts))] 18 | public Account Vendor { get; set; } 19 | 20 | } 21 | 22 | 23 | [Table("ProductFeatures")] 24 | public class ProductFeature { 25 | 26 | //[Key, Column( Order = 1)] 27 | public long ProductID { get; set; } 28 | 29 | [MaxLength(20)] 30 | public string FeatureID { get; set; } 31 | 32 | } 33 | } -------------------------------------------------------------------------------- /PostGreSql/PostGreSqlMigrationConsole/PostGreSqlAutomaticMigrationConsole.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | Exe 5 | netcoreapp3.1 6 | LiveMigrationConsole.Program 7 | false 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /PostGreSql/PostGreSqlMigrationConsole/Program.cs: -------------------------------------------------------------------------------- 1 | using LiveMigrationConsole.Models; 2 | using Microsoft.EntityFrameworkCore; 3 | using NeuroSpeech.EFCoreAutomaticMigration; 4 | 5 | namespace LiveMigrationConsole 6 | { 7 | class Program 8 | { 9 | static void Main(string[] args) 10 | { 11 | DbContextOptionsBuilder options = new DbContextOptionsBuilder(); 12 | 13 | Npgsql.NpgsqlConnectionStringBuilder sb = new Npgsql.NpgsqlConnectionStringBuilder(); 14 | sb.Host = "localhost"; 15 | sb.Database = "castyy"; 16 | sb.Username = "postgres"; 17 | sb.Password = "abcd123"; 18 | 19 | options.UseNpgsql(sb.ConnectionString); 20 | 21 | using (var db = new ERPContext(options.Options)) { 22 | 23 | db.MigrationForPostGreSql().Migrate(); 24 | 25 | } 26 | 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![.NET](https://github.com/neurospeech/ef-core-automatic-migration/actions/workflows/main.yml/badge.svg)](https://github.com/neurospeech/ef-core-automatic-migration/actions/workflows/main.yml) 2 | # Automatic Migration support for EF Core 3 | Entity Framework Core does not support automatic migration like earlier EntityFramework 6.x; Though it is useful feature, looks like it may not be implemented due to other priorities. 4 | 5 | This library helps in running automatic migration against database with following features. 6 | 7 | # Features 8 | 9 | 1. Creates Missing Table 10 | 2. Creates Missing Columns 11 | 3. Renames old column with same name, creates new column and migrates data if no loss occurs, you can customize this. 12 | 4. Renames old indexes and creates new ones 13 | 5. Creates indexes based on foreign keys 14 | 6. Entire migration runs in a single transaction, so if changes are large, they will fail and rolled back to previous state. 15 | 16 | # Warning 17 | 18 | 1. Please test this toughly before using it in production, always take backup of your database before running migrations on production. 19 | 2. It is also recommended to run migrations separately with some conditions, so you are aware of what changes are being taken. 20 | 3. This library is essential in earlier stages of development, and should only be used for small changes like adding non existing tables/columns. 21 | 22 | # Installation 23 | ## NuGet 24 | | Name | Package | 25 | |------------------------------------|----------------------------------------------------------------------------------------------------------------------------------------------------------------| 26 | | NeuroSpeech.EFCoreAutomaticMigration | [![NuGet](https://img.shields.io/nuget/v/NeuroSpeech.EFCoreAutomaticMigration.svg?label=NuGet)](https://www.nuget.org/packages/NeuroSpeech.EFCoreAutomaticMigration) | 27 | | NeuroSpeech.EFCoreAutomaticMigration.PostGreSql | [![NuGet](https://img.shields.io/nuget/v/NeuroSpeech.EFCoreAutomaticMigration.PostGreSql.svg?label=NuGet)](https://www.nuget.org/packages/NeuroSpeech.EFCoreAutomaticMigration.PostGreSql) | 28 | 29 | 30 | # Run 31 | ```c# 32 | 33 | public void Configure(IApplicationBuilder app, IHostingEnvironment env){ 34 | 35 | if(env.IsDevelopment()){ 36 | app.UseDeveloperExceptionPage(); 37 | 38 | using (var scope = app.ApplicationServices.GetService().CreateScope()) 39 | { 40 | using (var db = scope.ServiceProvider.GetRequiredService()) 41 | { 42 | db.MigrationForSqlServer() 43 | .Migrate(preventChangeColumn: true); 44 | } 45 | } 46 | } 47 | 48 | } 49 | 50 | ``` 51 | 52 | # Change in existing Columns 53 | 54 | Many times even slight change in model may result in changing of column schema, though there are two options in such case. 55 | 56 | ## Prevent Change 57 | 58 | This is the default option, in this case, the migration will fail. 59 | 60 | ## Manually Apply the Change 61 | 62 | Lets assume we have made BrokerID optional and it needs to be updated as nullable in the schema. In this case we can run a simple alert statement manually to make migration smooth. This will happen only if the target schema is old. The condition will not execute if target contains exact same schema. 63 | 64 | ```c# 65 | db.MigrationForSqlServer() 66 | .OnColumnChange((migration, existingColumn, currentColumn, tempName) => { 67 | // you can use properties to run some migrations manually ... 68 | if(existingColumn.TableNameAndColumnName == "[Product].[BrokerID]" 69 | && existingColumn.IsNullable != currentColumn.IsNullable) { 70 | // since we just changed column to nullable.. we can simply run alter by ourselves... 71 | migration.Run("ALTER TABLE [Product] ALTER COLUMN [BrokerID] INT NULL"); 72 | } 73 | }) 74 | .Migrate(); 75 | 76 | ``` 77 | 78 | 79 | -------------------------------------------------------------------------------- /version.json: -------------------------------------------------------------------------------- 1 | { 2 | "$schema": "https://raw.githubusercontent.com/dotnet/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json", 3 | "version": "1.4", 4 | "cloudBuild": { 5 | "buildNumber": { 6 | "enabled": true 7 | }, 8 | "setVersionVariables": true, 9 | "setAllVariables": true 10 | }, 11 | "publicReleaseRefSpec": [ 12 | "^refs/heads/main", 13 | "^refs/tags/v\\d+\\.\\d+" 14 | ] 15 | } 16 | --------------------------------------------------------------------------------