forked from kenjiuno/Npgsql
-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathNpgsqlDataStoreCreator.cs
More file actions
181 lines (153 loc) · 6.87 KB
/
NpgsqlDataStoreCreator.cs
File metadata and controls
181 lines (153 loc) · 6.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using JetBrains.Annotations;
using Microsoft.Data.Entity.Metadata;
using Microsoft.Data.Entity.Relational;
using Microsoft.Data.Entity.Relational.Migrations.Operations;
using Npgsql.EntityFramework7.Migrations;
using Microsoft.Data.Entity.Utilities;
namespace Npgsql.EntityFramework7
{
public class NpgsqlDataStoreCreator : RelationalDataStoreCreator, INpgsqlDataStoreCreator
{
private readonly INpgsqlEFConnection _connection;
private readonly INpgsqlModelDiffer _modelDiffer;
private readonly INpgsqlMigrationSqlGenerator _sqlGenerator;
private readonly ISqlStatementExecutor _statementExecutor;
public NpgsqlDataStoreCreator(
[NotNull] INpgsqlEFConnection connection,
[NotNull] INpgsqlModelDiffer modelDiffer,
[NotNull] INpgsqlMigrationSqlGenerator sqlGenerator,
[NotNull] ISqlStatementExecutor statementExecutor)
{
Check.NotNull(connection, nameof(connection));
Check.NotNull(modelDiffer, nameof(modelDiffer));
Check.NotNull(sqlGenerator, nameof(sqlGenerator));
Check.NotNull(statementExecutor, nameof(statementExecutor));
_connection = connection;
_modelDiffer = modelDiffer;
_sqlGenerator = sqlGenerator;
_statementExecutor = statementExecutor;
}
public override void Create()
{
using (var masterConnection = _connection.CreateMasterConnection())
{
_statementExecutor.ExecuteNonQuery(masterConnection, null, CreateCreateOperations());
ClearPool();
}
}
public override async Task CreateAsync(CancellationToken cancellationToken = default(CancellationToken))
{
using (var masterConnection = _connection.CreateMasterConnection())
{
await _statementExecutor
.ExecuteNonQueryAsync(masterConnection, null, CreateCreateOperations(), cancellationToken)
.WithCurrentCulture();
ClearPool();
}
}
public override void CreateTables(IModel model)
{
Check.NotNull(model, nameof(model));
_statementExecutor.ExecuteNonQuery(_connection, _connection.DbTransaction, CreateSchemaCommands(model));
}
public override async Task CreateTablesAsync(IModel model, CancellationToken cancellationToken = default(CancellationToken))
{
Check.NotNull(model, nameof(model));
await _statementExecutor
.ExecuteNonQueryAsync(_connection, _connection.DbTransaction, CreateSchemaCommands(model), cancellationToken)
.WithCurrentCulture();
}
public override bool HasTables()
=> (int)_statementExecutor.ExecuteScalar(_connection, _connection.DbTransaction, CreateHasTablesCommand()) != 0;
public override async Task<bool> HasTablesAsync(CancellationToken cancellationToken = default(CancellationToken))
=> (int)(await _statementExecutor
.ExecuteScalarAsync(_connection, _connection.DbTransaction, CreateHasTablesCommand(), cancellationToken)
.WithCurrentCulture()) != 0;
private IEnumerable<SqlBatch> CreateSchemaCommands(IModel model)
=> _sqlGenerator.Generate(_modelDiffer.GetDifferences(null, model), model);
private string CreateHasTablesCommand()
=> @"
SELECT CASE WHEN COUNT(*) = 0 THEN 0 ELSE 1 END
FROM information_schema.tables
WHERE table_type = 'BASE TABLE' AND table_schema NOT IN ('pg_catalog', 'information_schema')
";
private IEnumerable<SqlBatch> CreateCreateOperations()
=> _sqlGenerator.Generate(new[] { new CreateDatabaseOperation { Name = _connection.DbConnection.Database } });
public override bool Exists()
{
try
{
_connection.Open();
_connection.Close();
return true;
}
catch (NpgsqlException e)
{
if (IsDoesNotExist(e))
{
return false;
}
throw;
}
}
public override async Task<bool> ExistsAsync(CancellationToken cancellationToken = default(CancellationToken))
{
try
{
await _connection.OpenAsync(cancellationToken).WithCurrentCulture();
_connection.Close();
return true;
}
catch (NpgsqlException e)
{
if (IsDoesNotExist(e))
{
return false;
}
throw;
}
}
// Login failed is thrown when database does not exist (See Issue #776)
private static bool IsDoesNotExist(NpgsqlException exception) => exception.Code == "3D000";
public override void Delete()
{
ClearAllPools();
using (var masterConnection = _connection.CreateMasterConnection())
{
_statementExecutor.ExecuteNonQuery(masterConnection, null, CreateDropCommands());
}
}
public override async Task DeleteAsync(CancellationToken cancellationToken = default(CancellationToken))
{
ClearAllPools();
using (var masterConnection = _connection.CreateMasterConnection())
{
await _statementExecutor
.ExecuteNonQueryAsync(masterConnection, null, CreateDropCommands(), cancellationToken)
.WithCurrentCulture();
}
}
private IEnumerable<SqlBatch> CreateDropCommands()
{
var operations = new MigrationOperation[]
{
// TODO Check DbConnection.Database always gives us what we want
// Issue #775
new DropDatabaseOperation { Name = _connection.DbConnection.Database }
};
var masterCommands = _sqlGenerator.Generate(operations);
return masterCommands;
}
// Clear connection pools in case there are active connections that are pooled
private static void ClearAllPools() => NpgsqlConnection.ClearAllPools();
// Clear connection pool for the database connection since after the 'create database' call, a previously
// invalid connection may now be valid.
private void ClearPool() => NpgsqlConnection.ClearPool((NpgsqlConnection)_connection.DbConnection);
}
}