Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
jobily
Efcore.Pg
提交
7408237c
E
Efcore.Pg
项目概览
jobily
/
Efcore.Pg
12 个月 前同步成功
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
E
Efcore.Pg
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
7408237c
编写于
4月 27, 2020
作者:
S
Shay Rojansky
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Require computed columns to be explicitly specified as stored
Closes #1336
上级
e7357132
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
72 addition
and
8 deletion
+72
-8
src/EFCore.PG/Migrations/NpgsqlMigrationsSqlGenerator.cs
src/EFCore.PG/Migrations/NpgsqlMigrationsSqlGenerator.cs
+7
-0
src/EFCore.PG/Scaffolding/Internal/NpgsqlDatabaseModelFactory.cs
...ore.PG/Scaffolding/Internal/NpgsqlDatabaseModelFactory.cs
+3
-0
test/EFCore.PG.FunctionalTests/ComputedColumnTest.cs
test/EFCore.PG.FunctionalTests/ComputedColumnTest.cs
+3
-3
test/EFCore.PG.FunctionalTests/MigrationsNpgsqlTest.cs
test/EFCore.PG.FunctionalTests/MigrationsNpgsqlTest.cs
+55
-3
test/EFCore.PG.FunctionalTests/Scaffolding/NpgsqlDatabaseModelFactoryTest.cs
...tionalTests/Scaffolding/NpgsqlDatabaseModelFactoryTest.cs
+4
-2
未找到文件。
src/EFCore.PG/Migrations/NpgsqlMigrationsSqlGenerator.cs
浏览文件 @
7408237c
...
...
@@ -338,6 +338,7 @@ protected override void Generate(AlterColumnOperation operation, IModel model, M
DefaultValue
=
operation
.
DefaultValue
,
DefaultValueSql
=
operation
.
DefaultValueSql
,
ComputedColumnSql
=
operation
.
ComputedColumnSql
,
ComputedColumnIsStored
=
operation
.
ComputedColumnIsStored
,
IsFixedLength
=
operation
.
IsFixedLength
};
addColumnOperation
.
AddAnnotations
(
operation
.
GetAnnotations
());
...
...
@@ -1278,6 +1279,7 @@ protected override void Generate(CreateSequenceOperation operation, IModel model
$"
{
nameof
(
NpgsqlAnnotationNames
.
TsVectorProperties
)}
is absent or empty"
);
operation
.
ComputedColumnSql
=
ColumnsToTsVector
(
tsVectorIncludedColumns
,
tsVectorConfig
,
model
,
schema
,
table
);
operation
.
ComputedColumnIsStored
=
true
;
}
if
(
operation
.
ComputedColumnSql
!=
null
)
...
...
@@ -1421,6 +1423,11 @@ long Max(Type type)
if
(
_postgresVersion
!=
null
&&
_postgresVersion
<
new
Version
(
12
,
0
))
throw
new
NotSupportedException
(
"Computed/generated columns aren't supported in PostgreSQL prior to version 12"
);
if
(
operation
.
ComputedColumnIsStored
!=
true
)
throw
new
NotSupportedException
(
"Generated columns currently must be stored, specify "
+
$"
{
nameof
(
RelationalPropertyBuilderExtensions
.
IsStoredComputedColumn
)}
in your context's OnModelCreating."
);
builder
.
Append
(
DelimitIdentifier
(
name
))
.
Append
(
" "
)
...
...
src/EFCore.PG/Scaffolding/Internal/NpgsqlDatabaseModelFactory.cs
浏览文件 @
7408237c
...
...
@@ -353,7 +353,10 @@ ELSE NULL
// Default values and PostgreSQL 12 generated columns
if
(
record
.
GetValueOrDefault
<
char
>(
"attgenerated"
)
==
's'
)
{
column
.
ComputedColumnSql
=
record
.
GetValueOrDefault
<
string
>(
"default"
);
column
.
ComputedColumnIsStored
=
true
;
}
else
{
column
.
DefaultValueSql
=
record
.
GetValueOrDefault
<
string
>(
"default"
);
...
...
test/EFCore.PG.FunctionalTests/ComputedColumnTest.cs
浏览文件 @
7408237c
...
...
@@ -68,11 +68,11 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder
.
Entity
<
Entity
>()
.
Property
(
e
=>
e
.
P4
)
.
HasComputedColumnSql
(
@"""P1"" + ""P2"""
);
.
HasComputedColumnSql
(
@"""P1"" + ""P2"""
,
stored
:
true
);
modelBuilder
.
Entity
<
Entity
>()
.
Property
(
e
=>
e
.
P5
)
.
HasComputedColumnSql
(
@"""P1"" + ""P3"""
);
.
HasComputedColumnSql
(
@"""P1"" + ""P3"""
,
stored
:
true
);
}
}
...
...
@@ -123,7 +123,7 @@ protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
protected
override
void
OnModelCreating
(
ModelBuilder
modelBuilder
)
=>
modelBuilder
.
Entity
<
EnumItem
>()
.
Property
(
entity
=>
entity
.
CalculatedFlagEnum
)
.
HasComputedColumnSql
(
@"""FlagEnum"" | ""OptionalFlagEnum"""
);
.
HasComputedColumnSql
(
@"""FlagEnum"" | ""OptionalFlagEnum"""
,
stored
:
true
);
}
[
ConditionalFact
]
...
...
test/EFCore.PG.FunctionalTests/MigrationsNpgsqlTest.cs
浏览文件 @
7408237c
...
...
@@ -478,7 +478,14 @@ public override async Task Add_column_with_computedSql(bool? computedColumnStore
return
;
}
await
base
.
Add_column_with_computedSql
(
computedColumnStored
);
if
(
computedColumnStored
!=
true
)
{
// Non-stored generated columns aren't yet supported (PG12)
await
Assert
.
ThrowsAsync
<
NotSupportedException
>(()
=>
base
.
Add_column_with_computedSql
(
computedColumnStored
));
return
;
}
await
base
.
Add_column_with_computedSql
(
computedColumnStored
:
true
);
AssertSql
(
@"ALTER TABLE ""People"" ADD ""Sum"" text GENERATED ALWAYS AS (""X"" + ""Y"") STORED;"
);
...
...
@@ -550,7 +557,21 @@ public override async Task Add_column_computed_with_collation()
return
;
}
await
base
.
Add_column_computed_with_collation
();
// Non-stored generated columns aren't yet supported (PG12), so we override to used stored
await
Test
(
builder
=>
builder
.
Entity
(
"People"
).
Property
<
int
>(
"Id"
),
builder
=>
{
},
builder
=>
builder
.
Entity
(
"People"
).
Property
<
string
>(
"Name"
)
.
HasComputedColumnSql
(
"'hello'"
,
stored
:
true
)
.
UseCollation
(
NonDefaultCollation
),
model
=>
{
var
table
=
Assert
.
Single
(
model
.
Tables
);
Assert
.
Equal
(
2
,
table
.
Columns
.
Count
);
var
nameColumn
=
Assert
.
Single
(
table
.
Columns
,
c
=>
c
.
Name
==
"Name"
);
Assert
.
Contains
(
"hello"
,
nameColumn
.
ComputedColumnSql
);
Assert
.
Equal
(
NonDefaultCollation
,
nameColumn
.
Collation
);
});
AssertSql
(
@"ALTER TABLE ""People"" ADD ""Name"" text COLLATE ""POSIX"" GENERATED ALWAYS AS ('hello') STORED;"
);
...
...
@@ -860,6 +881,13 @@ public override async Task Alter_column_make_computed(bool? computedColumnStored
return
;
}
if
(
computedColumnStored
!=
true
)
{
// Non-stored generated columns aren't yet supported (PG12)
await
Assert
.
ThrowsAsync
<
NotSupportedException
>(()
=>
base
.
Add_column_with_computedSql
(
computedColumnStored
));
return
;
}
await
base
.
Alter_column_make_computed
(
computedColumnStored
);
AssertSql
(
...
...
@@ -876,7 +904,28 @@ public override async Task Alter_column_change_computed()
return
;
}
await
base
.
Alter_column_change_computed
();
// Non-stored generated columns aren't yet supported (PG12), so we override to used stored
await
Test
(
builder
=>
builder
.
Entity
(
"People"
,
e
=>
{
e
.
Property
<
int
>(
"Id"
);
e
.
Property
<
int
>(
"X"
);
e
.
Property
<
int
>(
"Y"
);
e
.
Property
<
int
>(
"Sum"
);
}),
builder
=>
builder
.
Entity
(
"People"
).
Property
<
int
>(
"Sum"
)
.
HasComputedColumnSql
(
$"
{
DelimitIdentifier
(
"X"
)}
+
{
DelimitIdentifier
(
"Y"
)}
"
,
stored
:
true
),
builder
=>
builder
.
Entity
(
"People"
).
Property
<
int
>(
"Sum"
)
.
HasComputedColumnSql
(
$"
{
DelimitIdentifier
(
"X"
)}
-
{
DelimitIdentifier
(
"Y"
)}
"
,
stored
:
true
),
model
=>
{
var
table
=
Assert
.
Single
(
model
.
Tables
);
var
sumColumn
=
Assert
.
Single
(
table
.
Columns
,
c
=>
c
.
Name
==
"Sum"
);
Assert
.
Contains
(
"X"
,
sumColumn
.
ComputedColumnSql
);
Assert
.
Contains
(
"Y"
,
sumColumn
.
ComputedColumnSql
);
Assert
.
Contains
(
"-"
,
sumColumn
.
ComputedColumnSql
);
});
AssertSql
(
@"ALTER TABLE ""People"" DROP COLUMN ""Sum"";"
,
...
...
@@ -884,6 +933,9 @@ public override async Task Alter_column_change_computed()
@"ALTER TABLE ""People"" ADD ""Sum"" integer GENERATED ALWAYS AS (""X"" - ""Y"") STORED;"
);
}
public
override
Task
Alter_column_change_computed_type
()
=>
Assert
.
ThrowsAsync
<
NotSupportedException
>(()
=>
base
.
Alter_column_change_computed
());
public
override
async
Task
Alter_column_add_comment
()
{
await
base
.
Alter_column_add_comment
();
...
...
test/EFCore.PG.FunctionalTests/Scaffolding/NpgsqlDatabaseModelFactoryTest.cs
浏览文件 @
7408237c
...
...
@@ -845,8 +845,10 @@ public void Computed_values_are_stored()
var
columns
=
dbModel
.
Tables
.
Single
().
Columns
;
// Note that on-the-fly computed columns aren't (yet) supported by PostgreSQL, only stored/persistedcolumns.
Assert
.
Null
(
columns
.
Single
(
c
=>
c
.
Name
==
"SumOfAAndB"
).
DefaultValueSql
);
Assert
.
Equal
(
@"(""A"" + ""B"")"
,
columns
.
Single
(
c
=>
c
.
Name
==
"SumOfAAndB"
).
ComputedColumnSql
);
var
column
=
columns
.
Single
(
c
=>
c
.
Name
==
"SumOfAAndB"
);
Assert
.
Null
(
column
.
DefaultValueSql
);
Assert
.
Equal
(
@"(""A"" + ""B"")"
,
column
.
ComputedColumnSql
);
Assert
.
True
(
column
.
ComputedColumnIsStored
);
},
@"DROP TABLE ""ComputedValues"""
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录