Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate/Create schema from C#? #120

Open
srad opened this issue Nov 3, 2024 · 3 comments
Open

Migrate/Create schema from C#? #120

srad opened this issue Nov 3, 2024 · 3 comments

Comments

@srad
Copy link

srad commented Nov 3, 2024

Is there a way to call the dotnet mysql-cache create ... from C# code, like with DbContext and EnsureMigrated()? Otherwise I need to roll out a launch script for every container image to run dotnet mysql-cache create before each application launches.

@ChaosEngine
Copy link
Collaborator

ChaosEngine commented Nov 4, 2024

How does this approach sounds:

  1. dotnet tool install --global Pomelo.Extensions.Caching.MySqlConfig.Tools --version 2.2.0
  2. In Your project add this project reference:
  <ItemGroup>
    <Reference Include="dotnet-mysql-cache">
      <HintPath>..\..\..\..\.dotnet\tools\.store\pomelo.extensions.caching.mysqlconfig.tools\2.2.0\pomelo.extensions.caching.mysqlconfig.tools\2.2.0\tools\net8.0\any\dotnet-mysql-cache.dll</HintPath>
    </Reference>
  </ItemGroup>

meaning, reference the internal installed tool library in Your project

  1. Now You can execute following code passinjg any input parameters as one would do for CLI tool:
void MySQLCacheToolTest()
{
	var program = new Pomelo.Extensions.Caching.MySqlConfig.Tools.Program();
	//program.Run(["-h"]);
	//program.Run(["script", "tabName1"]);
	program.Run([
		"create",
		"server=127.0.0.1;user id=SessionTest;password=SessionTest;persistsecurityinfo=True;port=3306;database=SessionTest;Allow User Variables=True",
		"tabNameXXX"
	]);
}

@srad, is this feasible ?

@srad
Copy link
Author

srad commented Nov 25, 2024

Thank you, that of course would probably work, I can also just execute a command to the shell, but I thought this is a more general case which concerns deployment. Why not just run a migration when the cache is added in Program.cs? Like Hangfire for example does, it just is added and runs internally the migration and just works.

@ChaosEngine
Copy link
Collaborator

Thank you, that of course would probably work, I can also just execute a command to the shell, but I thought this is a more general case which concerns deployment. Why not just run a migration when the cache is added in Program.cs? Like Hangfire for example does, it just is added and runs internally the migration and just works.

Because that would mix up the separated responsibilities rule:

  • Pomelo.Extensions.Caching.MySql is a caching library very much based on the SqlServer equivalent
  • Pomelo.Extensions.Caching.MySqlConfig.Tools is a tool for creating the structures that the above operates on.
  • I don't want the same table creation code (DDL) in both. DRY rule also suffers here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants