Skip to content

Commit

Permalink
Merge pull request #1 from gust42/add-year-based-squash
Browse files Browse the repository at this point in the history
Add year-based selection for squashing migrations
  • Loading branch information
gust42 authored Jan 22, 2025
2 parents 73cbed8 + 118a435 commit 0ceb501
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ Squashes EF migrations into the first migration in the specified directory.
#### **Usage:**

```bash
steward squash -d path/to/migrations
steward squash -d path/to/migrations [-y year]
```
##### Options

- `-d (or [MigrationsDirectory])`: Path to the directory containing your EF migrations. If omitted, you'll be prompted to enter it interactively.
- `-y (or [Year])`: Optional. Specify the year up to which migrations should be squashed. If omitted, all migrations will be squashed.

#### **How It Works**

Expand All @@ -44,4 +45,4 @@ The squash command combines all existing migrations into a single, consolidated
Depending on how your migrations were structured, you might need to make some manual adjustments after running squash. Some things to watch for:

- Private Fields: If any private readonly fields or other member data are in your migrations, ensure they're properly defined in the combined migration file.
- Variable Conflicts: Sometimes migrations contain variables with the same name across different files. Double-check the final file for duplicate variables and resolve them accordingly.
- Variable Conflicts: Sometimes migrations contain variables with the same name across different files. Double-check the final file for duplicate variables and resolve them accordingly.
23 changes: 20 additions & 3 deletions StewardEF/Commands/SquashMigrations.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ public class Settings : CommandSettings
{
[CommandArgument(0, "[MigrationsDirectory]")]
public string? MigrationsDirectory { get; set; }

[CommandOption("-y|--year")]
public int? Year { get; set; }
}

public override int Execute(CommandContext context, Settings settings)
Expand All @@ -30,11 +33,11 @@ public override int Execute(CommandContext context, Settings settings)
return 1;
}

SquashMigrations(directory);
SquashMigrations(directory, settings.Year);
return 0;
}

static void SquashMigrations(string directory)
static void SquashMigrations(string directory, int? year)
{
// Get all .cs files including Designer.cs files
var files = Directory.GetFiles(directory, "*.cs", SearchOption.TopDirectoryOnly)
Expand All @@ -46,6 +49,14 @@ static void SquashMigrations(string directory)
.Where(f => !Path.GetFileName(f).EndsWith("ModelSnapshot.cs", StringComparison.OrdinalIgnoreCase))
.OrderBy(f => f)
.ToList();

if (year.HasValue)
{
migrationFiles = migrationFiles
.Where(f => ParseYearFromFileName(f) <= year.Value)
.ToList();
}

if (migrationFiles.Count == 0)
{
AnsiConsole.MarkupLine("[red]No migration files found to squash.[/]");
Expand Down Expand Up @@ -334,4 +345,10 @@ private static List<string> IndentContent(string content, string indentation)
var indentedLines = lines.Select(line => indentation + line).ToList();
return indentedLines;
}
}

private static int ParseYearFromFileName(string fileName)
{
var match = Regex.Match(Path.GetFileName(fileName), @"^\d{4}");
return match.Success ? int.Parse(match.Value) : int.MaxValue;
}
}

0 comments on commit 0ceb501

Please sign in to comment.