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

SQLite RevEng: Fix some issues #20063

Merged
merged 2 commits into from
Feb 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,16 @@
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Scaffolding;
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata;
using Microsoft.EntityFrameworkCore.Sqlite.Internal;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.EntityFrameworkCore.Utilities;

using static SQLitePCL.raw;

#nullable enable

namespace Microsoft.EntityFrameworkCore.Sqlite.Scaffolding.Internal
Expand Down Expand Up @@ -231,10 +234,31 @@ private void GetColumns(DbConnection connection, DatabaseTable table)

_logger.ColumnFound(table.Name, columnName, dataType, notNull, defaultValue);

var autoIncrement = 0;
if (connection is SqliteConnection sqliteConnection
&& !(table is DatabaseView))
{
var db = sqliteConnection.Handle;
var rc = sqlite3_table_column_metadata(
db,
connection.Database,
table.Name,
columnName,
out var _,
out var _,
out var _,
out var _,
out autoIncrement);
SqliteException.ThrowExceptionForRC(rc, db);
}

table.Columns.Add(new DatabaseColumn(table, columnName, dataType)
{
IsNullable = !notNull,
DefaultValueSql = defaultValue
DefaultValueSql = defaultValue,
ValueGenerated = autoIncrement != 0
? ValueGenerated.OnAdd
: default(ValueGenerated?)
});
}
}
Expand Down Expand Up @@ -490,7 +514,7 @@ private void GetForeignKeys(DbConnection connection, DatabaseTable table, IList<

using var command2 = connection.CreateCommand();
command2.CommandText = new StringBuilder()
.AppendLine("SELECT \"from\", \"to\"")
.AppendLine("SELECT \"seq\", \"from\", \"to\"")
.AppendLine("FROM pragma_foreign_key_list(@table)")
.AppendLine("WHERE \"id\" = @id")
.AppendLine("ORDER BY \"seq\";")
Expand All @@ -512,17 +536,27 @@ private void GetForeignKeys(DbConnection connection, DatabaseTable table, IList<
{
while (reader2.Read())
{
var columnName = reader2.GetString(0);
var columnName = reader2.GetString(1);
var column = table.Columns.FirstOrDefault(c => c.Name == columnName)
?? table.Columns.FirstOrDefault(
c => c.Name.Equals(columnName, StringComparison.OrdinalIgnoreCase));
Check.DebugAssert(column != null, "column is null.");

var principalColumnName = reader2.GetString(1);
var principalColumn =
foreignKey.PrincipalTable.Columns.FirstOrDefault(c => c.Name == principalColumnName)
?? foreignKey.PrincipalTable.Columns.FirstOrDefault(
c => c.Name.Equals(principalColumnName, StringComparison.OrdinalIgnoreCase));
var principalColumnName = reader2.IsDBNull(2) ? null : reader2.GetString(2);
DatabaseColumn? principalColumn = null;
if (principalColumnName != null)
{
principalColumn =
foreignKey.PrincipalTable.Columns.FirstOrDefault(c => c.Name == principalColumnName)
?? foreignKey.PrincipalTable.Columns.FirstOrDefault(
c => c.Name.Equals(principalColumnName, StringComparison.OrdinalIgnoreCase));
}
else if (principalTable.PrimaryKey != null)
{
var seq = reader2.GetInt32(0);
principalColumn = principalTable.PrimaryKey.Columns[seq];
}

if (principalColumn == null)
{
invalid = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Microsoft.EntityFrameworkCore.Diagnostics;
using Microsoft.EntityFrameworkCore.Diagnostics.Internal;
using Microsoft.EntityFrameworkCore.Internal;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using Microsoft.EntityFrameworkCore.Scaffolding.Metadata;
using Microsoft.EntityFrameworkCore.Sqlite.Design.Internal;
Expand Down Expand Up @@ -319,6 +320,45 @@ FOREIGN KEY (Id) REFERENCES PrincipalTable(Id) ON DELETE NO ACTION
DROP TABLE PrincipalTable;");
}

[ConditionalFact]
public void Create_composite_foreign_key_with_default_columns()
{
Test(
@"
CREATE TABLE MinimalFKTest1 (
Id1 INTEGER,
Id2 INTEGER,
Id3 INTEGER,
PRIMARY KEY (Id2, Id3, Id1)
);

CREATE TABLE MinimalFKTest2 (
Id3 INTEGER,
Id2 INTEGER,
Id1 INTEGER,
FOREIGN KEY (Id3, Id1, Id2) REFERENCES MinimalFKTest1
)
",
Enumerable.Empty<string>(),
Enumerable.Empty<string>(),
dbModel =>
{
Assert.Equal(2, dbModel.Tables.Count);

var table = dbModel.Tables.Single(t => t.Name == "MinimalFKTest2");

var foreignKey = Assert.Single(table.ForeignKeys);
Assert.Equal(new[] { "Id3", "Id1", "Id2" }, foreignKey.Columns.Select(c => c.Name));
Assert.Equal("MinimalFKTest1", foreignKey.PrincipalTable.Name);
Assert.Equal(new[] { "Id2", "Id3", "Id1" }, foreignKey.PrincipalColumns.Select(c => c.Name));

},
@"
DROP TABLE MinimalFKTest2;
DROP TABLE MinimalFKTest1;
");
}

#endregion

#region ColumnFacets
Expand Down Expand Up @@ -418,6 +458,35 @@ public void Column_default_value_is_ignored_when_clr_default(string columnSql)
"DROP TABLE DefaultValueClr");
}

[ConditionalTheory]
[InlineData(false)]
[InlineData(true)]
public void Column_ValueGenerated_is_set(bool autoIncrement)
{
Test(
$@"
CREATE TABLE AutoIncTest (
Id INTEGER PRIMARY KEY {(autoIncrement ? "AUTOINCREMENT" : null)}
)
",
Enumerable.Empty<string>(),
Enumerable.Empty<string>(),
dbModel =>
{
var table = Assert.Single(dbModel.Tables);
Assert.Equal("AutoIncTest", table.Name);

var column = Assert.Single(table.Columns);
Assert.Equal("Id", column.Name);
Assert.Equal(
autoIncrement
? ValueGenerated.OnAdd
: default(ValueGenerated?),
column.ValueGenerated);
},
"DROP TABLE AutoIncTest");
}

#endregion

#region PrimaryKeyFacets
Expand Down