Skip to content

Commit

Permalink
修复自动建表的时候mysql探测表是否存在的时候一个bug
Browse files Browse the repository at this point in the history
  • Loading branch information
kklldog committed Mar 14, 2022
1 parent ff21d40 commit 2ed545f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 9 deletions.
4 changes: 2 additions & 2 deletions AgileConfig.Server.Apisite/AgileConfig.Server.Apisite.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<AspNetCoreHostingModel>InProcess</AspNetCoreHostingModel>
<AssemblyVersion>1.5.7.8</AssemblyVersion>
<AssemblyVersion>1.5.7.9</AssemblyVersion>
<Version>1.5.2</Version>
<PackageVersion>1.5.3.2</PackageVersion>
<DockerDefaultTargetOS>Linux</DockerDefaultTargetOS>
<FileVersion>1.5.7.8</FileVersion>
<FileVersion>1.5.7.9</FileVersion>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand Down
19 changes: 14 additions & 5 deletions AgileConfig.Server.Data.Freesql/EnsureTables.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,23 +9,31 @@ namespace AgileConfig.Server.Data.Freesql
{
public class EnsureTables
{
private const string Sqlite_ExistTableSql = "SELECT count(1) FROM sqlite_master WHERE type='table' AND name = 'agc_app'";
private const string Mysql_ExistTableSql = " SELECT count(1) FROM information_schema.TABLES WHERE table_name ='agc_app'";
private const string SqlServer_ExistTableSql = "SELECT COUNT(1) FROM dbo.SYSOBJECTS WHERE ID = object_id(N'[dbo].[agc_app]') and OBJECTPROPERTY(id, N'IsUserTable') = 1";
private const string Sqlite_ExistTableSql =
"SELECT count(1) FROM sqlite_master WHERE type='table' AND name = 'agc_app'";

private const string Mysql_ExistTableSql =
" SELECT count(1) FROM information_schema.TABLES WHERE table_schema=?schema AND table_name ='agc_app'";

private const string SqlServer_ExistTableSql =
"SELECT COUNT(1) FROM dbo.SYSOBJECTS WHERE ID = object_id(N'[dbo].[agc_app]') and OBJECTPROPERTY(id, N'IsUserTable') = 1";

private const string Oracle_ExistTableSql = "select count(1) from user_tables where table_name = 'agc_app'";
private const string PostgreSql_ExistTableSql = "select count(1) from pg_class where relname = 'agc_app'";

public static bool ExistTable(IFreeSql instance)
{
//sqlite exist table?
string sql = "";
string schema = "";
switch (instance.Ado.DataType)
{
case FreeSql.DataType.Sqlite:
sql = Sqlite_ExistTableSql;
break;
case FreeSql.DataType.MySql:
sql = Mysql_ExistTableSql;
schema = instance.Ado.MasterPool.Get().Value.Database;
break;
case FreeSql.DataType.SqlServer:
sql = SqlServer_ExistTableSql;
Expand All @@ -41,7 +49,7 @@ public static bool ExistTable(IFreeSql instance)
break;
}

dynamic count = instance.Ado.ExecuteScalar(sql);
dynamic count = instance.Ado.ExecuteScalar(sql, new { schema });

return count > 0;
}
Expand All @@ -58,6 +66,7 @@ public static void Ensure(IFreeSql instance)
{
instance.CodeFirst.IsSyncStructureToUpper = true;
}

try
{
instance.CodeFirst.SyncStructure<App>();
Expand All @@ -81,4 +90,4 @@ public static void Ensure(IFreeSql instance)
}
}
}
}
}
11 changes: 9 additions & 2 deletions AgileConfig.Server.ServiceTests/EnsureTablesTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,19 @@ public void ExistTableSqlServerTest()
public void ExistTableMysqlTest()
{
//SqlServer
string conn = "Database=agile_config_test;Data Source=localhost;User Id=root;Password=dev@123;port=3306";
string conn = "Database=agile_config_test;Data Source=192.168.0.125;User Id=root;Password=mimi756310;port=13306";
var mysql_fsq = new FreeSqlBuilder()
.UseConnectionString(FreeSql.DataType.MySql, conn)
.Build();
FluentApi.Config(mysql_fsq);
mysql_fsq.Ado.ExecuteNonQuery("drop table agc_app");
try
{
mysql_fsq.Ado.ExecuteNonQuery("drop table agc_app");
}
catch (Exception e)
{
Console.WriteLine(e);
}
var ex = EnsureTables.ExistTable(mysql_fsq);
Assert.IsFalse(ex);
mysql_fsq.CodeFirst.SyncStructure<App>();
Expand Down

0 comments on commit 2ed545f

Please sign in to comment.