This repository was archived by the owner on Jun 5, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Adding EF core, database project, and some usage. #3674
Merged
dhoehna
merged 16 commits into
user/dhoehna/RepositoryManagementFeature
from
user/dhoehna/RepositoryManagementDatabase
Aug 26, 2024
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e983d99
WIP
dhoehna 7379f6f
EF works. Can save data.
dhoehna bdc4019
Can read and write. :)
dhoehna a988d21
Repos are added when cloned.
dhoehna ee2c767
Putting save/load into a different class
dhoehna 86a3982
Adding default values
dhoehna 37ac6f6
Cleaning up names and using statements
dhoehna d3dab7e
More comments
dhoehna 66bdbb2
Removing refrence to the public nuget
dhoehna 82d6085
Restoring the nuget config
dhoehna 2629d85
Adding a test. Better defining dates
dhoehna 2b1438b
Adding some tests
dhoehna a14822e
More comments.
dhoehna 4685640
Better path comparison.
dhoehna 9299f5d
Removing unused equals code
dhoehna d01eed6
Adding more comments. Making new migrations
dhoehna File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
database/DevHome.Database/DatabaseModels/RepositoryManagement/Repository.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.EntityFrameworkCore; | ||
|
|
||
| namespace DevHome.Database.DatabaseModels.RepositoryManagement; | ||
|
|
||
| /// <summary> | ||
| /// This represents the SQLite model EF has in the database. | ||
| /// Any change here needs a corresponding migration. | ||
| /// Use FluentAPI in the database context to further customize each column. | ||
| /// </summary> | ||
| [Index(nameof(RepositoryName), nameof(RepositoryClonePath), IsUnique = true)] | ||
| public class Repository | ||
| { | ||
| public int RepositoryId { get; set; } | ||
|
|
||
| public string? RepositoryName { get; set; } | ||
|
|
||
| public string? RepositoryClonePath { get; set; } | ||
|
|
||
dhoehna marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| public DateTime? CreatedUTCDate { get; set; } | ||
|
|
||
| public DateTime? UpdatedUTCDate { get; set; } | ||
|
|
||
| // 1:1 relationship. Repository is the parent and needs only | ||
| // the object of the dependant. | ||
| public RepositoryMetadata? RepositoryMetadata { get; set; } | ||
dkbennett marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
27 changes: 27 additions & 0 deletions
27
database/DevHome.Database/DatabaseModels/RepositoryManagement/RepositoryMetadata.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| namespace DevHome.Database.DatabaseModels.RepositoryManagement; | ||
|
|
||
| /// <summary> | ||
| /// This represents the SQLite model EF has in the database. | ||
| /// Any change here needs a corresponding migration. | ||
| /// Use FluentAPI in the database context to further customize each column. | ||
| /// </summary> | ||
| public class RepositoryMetadata | ||
| { | ||
| // EF uses [ClassName]Id as the primary key | ||
| public int RepositoryMetadataId { get; set; } | ||
|
|
||
| public bool IsHiddenFromPage { get; set; } | ||
|
|
||
| public DateTime UtcDateHidden { get; set; } | ||
|
|
||
| public DateTime? CreatedUTCDate { get; set; } | ||
|
|
||
| public DateTime? UpdatedUTCDate { get; set; } | ||
|
|
||
| public int RepositoryId { get; set; } | ||
|
|
||
| public Repository Repository { get; set; } = null!; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <Project Sdk="Microsoft.NET.Sdk"> | ||
|
|
||
| <PropertyGroup> | ||
| <TargetFramework>net8.0</TargetFramework> | ||
| <ImplicitUsings>enable</ImplicitUsings> | ||
| <Nullable>enable</Nullable> | ||
| </PropertyGroup> | ||
|
|
||
| <ItemGroup> | ||
| <PackageReference Include="Microsoft.EntityFrameworkCore.Sqlite" Version="8.0.8" /> | ||
| <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="8.0.8"> | ||
| <PrivateAssets>all</PrivateAssets> | ||
| <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> | ||
| </PackageReference> | ||
| <PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="8.0.0" /> | ||
| </ItemGroup> | ||
|
|
||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.ComponentModel.DataAnnotations.Schema; | ||
| using DevHome.Database.DatabaseModels.RepositoryManagement; | ||
| using Microsoft.EntityFrameworkCore; | ||
|
|
||
| namespace DevHome.Database; | ||
|
|
||
| public class DevHomeDatabaseContext : DbContext | ||
| { | ||
| public DbSet<Repository> Repositories { get; set; } | ||
|
|
||
| public DbSet<RepositoryMetadata> RepositoryMetadatas { get; set; } | ||
|
|
||
| public string DbPath { get; } | ||
|
|
||
| public DevHomeDatabaseContext() | ||
| { | ||
| // Not the final path. It will change before going into main. | ||
| // Needs a configurable location for testing. | ||
| var folder = Environment.SpecialFolder.LocalApplicationData; | ||
dhoehna marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| var path = Environment.GetFolderPath(folder); | ||
| DbPath = Path.Join(path, "DevHome.db"); | ||
| } | ||
|
|
||
| protected override void OnModelCreating(ModelBuilder modelBuilder) | ||
| { | ||
| // If I have time, this should be split like service extensions. | ||
| // As more entities are added, the longer this method will get. | ||
| var repositoryEntity = modelBuilder.Entity<Repository>(); | ||
| if (repositoryEntity != null) | ||
| { | ||
| repositoryEntity.Property(x => x.RepositoryClonePath).HasDefaultValue(string.Empty).IsRequired(true); | ||
| repositoryEntity.Property(x => x.RepositoryName).HasDefaultValue(string.Empty).IsRequired(true); | ||
| repositoryEntity.Property(x => x.CreatedUTCDate).HasDefaultValueSql("datetime()"); | ||
| repositoryEntity.Property(x => x.UpdatedUTCDate).HasDefaultValue(new DateTime(1, 1, 1, 0, 0, 0, DateTimeKind.Utc)); | ||
| repositoryEntity.ToTable("Repository"); | ||
| } | ||
|
|
||
| var repositoryMetadataEntity = modelBuilder.Entity<RepositoryMetadata>(); | ||
| if (repositoryMetadataEntity != null) | ||
| { | ||
| repositoryMetadataEntity.Property(x => x.IsHiddenFromPage).HasDefaultValue(false).IsRequired(true); | ||
| repositoryMetadataEntity.Property(x => x.UtcDateHidden).HasDefaultValue(new DateTime(1, 1, 1, 0, 0, 0, DateTimeKind.Utc)).IsRequired(true); | ||
| repositoryMetadataEntity.Property(x => x.CreatedUTCDate).HasDefaultValueSql("datetime()"); | ||
| repositoryMetadataEntity.Property(x => x.UpdatedUTCDate).HasDefaultValue(new DateTime(1, 1, 1, 0, 0, 0, DateTimeKind.Utc)); | ||
| repositoryMetadataEntity.ToTable("RepositoryMetadata"); | ||
| } | ||
| } | ||
|
|
||
| protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) | ||
| => optionsBuilder.UseSqlite($"Data Source={DbPath}"); | ||
dhoehna marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.Extensions.DependencyInjection; | ||
| using Microsoft.Extensions.Hosting; | ||
|
|
||
| namespace DevHome.Database.Extensions; | ||
|
|
||
| public static class ServiceExtensions | ||
| { | ||
| public static IServiceCollection AddDatabaseContext(this IServiceCollection services, HostBuilderContext context) | ||
| { | ||
| services.AddTransient<DevHomeDatabaseContext>(); | ||
|
|
||
| return services; | ||
| } | ||
| } |
114 changes: 114 additions & 0 deletions
114
database/DevHome.Database/Migrations/20240826220057_InitialMigration.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
79 changes: 79 additions & 0 deletions
79
database/DevHome.Database/Migrations/20240826220057_InitialMigration.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.EntityFrameworkCore.Migrations; | ||
|
|
||
| #nullable disable | ||
|
|
||
| namespace DevHome.Database.Migrations; | ||
|
|
||
| /// <inheritdoc /> | ||
| public partial class InitialMigration : Migration | ||
| { | ||
| private static readonly string[] _columns = new[] { "RepositoryName", "RepositoryClonePath" }; | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override void Up(MigrationBuilder migrationBuilder) | ||
| { | ||
| migrationBuilder.CreateTable( | ||
| name: "Repository", | ||
| columns: table => new | ||
| { | ||
| RepositoryId = table.Column<int>(type: "INTEGER", nullable: false) | ||
| .Annotation("Sqlite:Autoincrement", true), | ||
| RepositoryName = table.Column<string>(type: "TEXT", nullable: false, defaultValue: string.Empty), | ||
| RepositoryClonePath = table.Column<string>(type: "TEXT", nullable: false, defaultValue: string.Empty), | ||
| CreatedUTCDate = table.Column<DateTime>(type: "TEXT", nullable: true, defaultValueSql: "datetime()"), | ||
| UpdatedUTCDate = table.Column<DateTime>(type: "TEXT", nullable: true, defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)), | ||
| }, | ||
| constraints: table => | ||
| { | ||
| table.PrimaryKey("PK_Repository", x => x.RepositoryId); | ||
| }); | ||
|
|
||
| migrationBuilder.CreateTable( | ||
| name: "RepositoryMetadata", | ||
| columns: table => new | ||
| { | ||
| RepositoryMetadataId = table.Column<int>(type: "INTEGER", nullable: false) | ||
| .Annotation("Sqlite:Autoincrement", true), | ||
| IsHiddenFromPage = table.Column<bool>(type: "INTEGER", nullable: false, defaultValue: false), | ||
| UtcDateHidden = table.Column<DateTime>(type: "TEXT", nullable: false, defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)), | ||
| CreatedUTCDate = table.Column<DateTime>(type: "TEXT", nullable: true, defaultValueSql: "datetime()"), | ||
| UpdatedUTCDate = table.Column<DateTime>(type: "TEXT", nullable: true, defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)), | ||
| RepositoryId = table.Column<int>(type: "INTEGER", nullable: false), | ||
| }, | ||
| constraints: table => | ||
| { | ||
| table.PrimaryKey("PK_RepositoryMetadata", x => x.RepositoryMetadataId); | ||
| table.ForeignKey( | ||
| name: "FK_RepositoryMetadata_Repository_RepositoryId", | ||
| column: x => x.RepositoryId, | ||
| principalTable: "Repository", | ||
| principalColumn: "RepositoryId", | ||
| onDelete: ReferentialAction.Cascade); | ||
| }); | ||
|
|
||
| migrationBuilder.CreateIndex( | ||
| name: "IX_Repository_RepositoryName_RepositoryClonePath", | ||
| table: "Repository", | ||
| columns: _columns, | ||
| unique: true); | ||
|
|
||
| migrationBuilder.CreateIndex( | ||
| name: "IX_RepositoryMetadata_RepositoryId", | ||
| table: "RepositoryMetadata", | ||
| column: "RepositoryId", | ||
| unique: true); | ||
| } | ||
|
|
||
| /// <inheritdoc /> | ||
| protected override void Down(MigrationBuilder migrationBuilder) | ||
| { | ||
| migrationBuilder.DropTable( | ||
| name: "RepositoryMetadata"); | ||
|
|
||
| migrationBuilder.DropTable( | ||
| name: "Repository"); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.