Esempio n. 1
0
        public void Visit_with_alter_column_does_not_reset_indexes_if_same_type_but_smaller_max_length()
        {
            var database = new DatabaseModel();
            var column
                = new Column("Id", typeof(string))
                {
                MaxLength = 10
                };
            var table = new Table("A", new[] { column });
            var index = new Index("IX", new[] { column });

            database.AddTable(table);
            table.AddIndex(index);

            var alterColumnOperation
                = new AlterColumnOperation(
                      "A",
                      new Column("Id", typeof(string))
            {
                MaxLength = 9
            }, true);

            var operations = PreProcess(database, alterColumnOperation);

            Assert.Equal(1, operations.Count);

            Assert.Same(alterColumnOperation, operations[0]);
        }
Esempio n. 2
0
        public void Can_set_name()
        {
            var index = new Index("IX", new[] { new Column("Foo", typeof(int)) });

            Assert.Equal("IX", index.Name);

            index.Name = "RenamedIX";

            Assert.Equal("RenamedIX", index.Name);
        }
Esempio n. 3
0
        protected virtual bool MatchIndexes(
            [NotNull] Index sourceIndex,
            [NotNull] Index targetIndex,
            [NotNull] IDictionary <Column, Column> columnMap)
        {
            Check.NotNull(sourceIndex, "sourceIndex");
            Check.NotNull(targetIndex, "targetIndex");
            Check.NotNull(columnMap, "columnMap");

            return
                (sourceIndex.IsUnique == targetIndex.IsUnique &&
                 sourceIndex.IsClustered == targetIndex.IsClustered &&
                 MatchColumnReferences(sourceIndex.Columns, targetIndex.Columns, columnMap));
        }
Esempio n. 4
0
        public void Create_and_initialize_index()
        {
            var column0 = new Column("Foo", "int");
            var column1 = new Column("Bar", "int");
            var table = new Table("dbo.MyTable", new[] { column0, column1 });
            var index = new Index("MyIndex", new[] { column1 }, isUnique: true, isClustered: true);

            Assert.Equal("MyIndex", index.Name);
            Assert.IsAssignableFrom<IReadOnlyList<Column>>(table.Columns);
            Assert.Equal(1, index.Columns.Count);
            Assert.Same(column1, index.Columns[0]);
            Assert.True(index.IsUnique);
            Assert.True(index.IsClustered);
        }
Esempio n. 5
0
        private static Index BuildIndex(DatabaseModel database, IIndex index)
        {
            Check.NotNull(index, "index");

            var table   = database.GetTable(index.EntityType.SchemaQualifiedName());
            var columns = index.Properties.Select(
                p => table.GetColumn(p.ColumnName())).ToArray();

            var storeIndex = new Index(
                IndexName(index), columns, index.IsUnique, index.IsClustered());

            table.AddIndex(storeIndex);

            return(storeIndex);
        }
Esempio n. 6
0
        public void Visit_with_rename_index_operation()
        {
            var model     = new DatabaseModel();
            var column    = new Column("Foo", typeof(int));
            var table     = new Table("dbo.MyTable", new[] { column });
            var index     = new Index("IX", new[] { column }, isUnique: true, isClustered: true);
            var operation = new RenameIndexOperation("dbo.MyTable", "IX", "RenamedIndex");

            model.AddTable(table);
            table.AddIndex(index);

            operation.Accept(new DatabaseModelModifier(), model);

            Assert.Equal(1, table.Indexes.Count);
            Assert.Equal("RenamedIndex", table.Indexes[0].Name);
        }
Esempio n. 7
0
        public void Clone_gets_column_clones_from_cache()
        {
            var column0 = new Column("Foo", typeof(int));
            var column1 = new Column("Bar", typeof(int));
            var index = new Index("IX", new[] { column0, column1 }, isUnique: true, isClustered: true);

            var cloneContext = new CloneContext();
            var columnClone0 = column0.Clone(cloneContext);
            var columnClone1 = column1.Clone(cloneContext);
            var clone = index.Clone(cloneContext);

            Assert.NotSame(index, clone);
            Assert.Equal(2, clone.Columns.Count);
            Assert.Same(columnClone0, clone.Columns[0]);
            Assert.Same(columnClone1, clone.Columns[1]);
        }
Esempio n. 8
0
        private Index BuildIndex(DatabaseModel database, IIndex index)
        {
            Check.NotNull(index, "index");

            var table   = database.GetTable(GetSchemaQualifiedName(index.EntityType));
            var columns = index.Properties.Select(
                p => table.GetColumn(GetColumnName(p))).ToArray();

            // TODO: Clustered is SQL Server-specific elsewhere in the stack
            // Issue #879
            var storeIndex = new Index(
                IndexName(index), columns, index.IsUnique, IsIndexClustered(index));

            table.AddIndex(storeIndex);

            return(storeIndex);
        }
Esempio n. 9
0
        public void Clone_replicates_instance_and_adds_column_clones_to_cache()
        {
            var column0 = new Column("Foo", typeof(int));
            var column1 = new Column("Bar", typeof(int));
            var index = new Index("IX", new[] { column0, column1 }, isUnique: true, isClustered: true);

            var cloneContext = new CloneContext();
            var clone = index.Clone(cloneContext);

            Assert.NotSame(index, clone);
            Assert.Equal("IX", clone.Name);
            Assert.Equal(2, clone.Columns.Count);
            Assert.NotSame(column0, clone.Columns[0]);
            Assert.NotSame(column1, clone.Columns[1]);
            Assert.Equal("Foo", clone.Columns[0].Name);
            Assert.Equal("Bar", clone.Columns[1].Name);
            Assert.True(clone.IsUnique);
            Assert.True(clone.IsClustered);

            Assert.Same(clone.Columns[0], cloneContext.GetOrAdd(column0, () => null));
            Assert.Same(clone.Columns[1], cloneContext.GetOrAdd(column1, () => null));
        }
Esempio n. 10
0
        private static Index BuildIndex(DatabaseModel database, IIndex index)
        {
            Check.NotNull(index, "index");

            var table = database.GetTable(GetSchemaQualifiedName(index.EntityType));
            var columns = index.Properties.Select(
                p => table.GetColumn(p.ColumnName())).ToArray();

            var storeIndex = new Index(
                IndexName(index), columns, index.IsUnique, index.IsClustered());

            table.AddIndex(storeIndex);

            return storeIndex;
        }
Esempio n. 11
0
        public void Clone_replicates_instance_and_uses_cache()
        {
            var column00 = new Column("C00", typeof(int));
            var column01 = new Column("C01", typeof(string));
            var column10 = new Column("C10", typeof(int));
            var column11 = new Column("C11", typeof(string));
            var table0 = new Table("dbo.T0", new[] { column00, column01 });
            var table1 = new Table("dbo.T1", new[] { column10, column11 });
            var primaryKey0 = new PrimaryKey("PK0", new[] { column00 });
            var primaryKey1 = new PrimaryKey("PK1", new[] { column10 });
            var foreignKey0 = new ForeignKey("FK0", new[] { column01 }, new[] { column10 });
            var foreignKey1 = new ForeignKey("FK1", new[] { column11 }, new[] { column00 });
            var index0 = new Index("IX0", new[] { column00 });
            var index1 = new Index("IX1", new[] { column10 });

            table0.PrimaryKey = primaryKey0;
            table1.PrimaryKey = primaryKey1;
            table0.AddForeignKey(foreignKey0);
            table1.AddForeignKey(foreignKey1);
            table0.AddIndex(index0);
            table1.AddIndex(index1);

            var cloneContext = new CloneContext();
            var clone0 = table0.Clone(cloneContext);

            Assert.NotSame(table0, clone0);
            Assert.Equal("dbo.T0", clone0.Name);

            Assert.Equal(2, clone0.Columns.Count);
            Assert.NotSame(column00, clone0.Columns[0]);
            Assert.NotSame(column01, clone0.Columns[1]);
            Assert.Equal("C00", clone0.Columns[0].Name);
            Assert.Equal("C01", clone0.Columns[1].Name);
            Assert.Equal(typeof(int), clone0.Columns[0].ClrType);
            Assert.Equal(typeof(string), clone0.Columns[1].ClrType);

            Assert.NotNull(clone0.PrimaryKey);
            Assert.NotSame(primaryKey0, clone0.PrimaryKey);
            Assert.Equal("PK0", clone0.PrimaryKey.Name);
            Assert.Equal(1, clone0.PrimaryKey.Columns.Count);
            Assert.Same(clone0.Columns[0], clone0.PrimaryKey.Columns[0]);

            Assert.Equal(1, clone0.ForeignKeys.Count);
            Assert.NotSame(foreignKey0, clone0.ForeignKeys[0]);
            Assert.Equal("FK0", clone0.ForeignKeys[0].Name);
            Assert.Equal(1, clone0.ForeignKeys[0].Columns.Count);
            Assert.Equal(1, clone0.ForeignKeys[0].ReferencedColumns.Count);
            Assert.Same(clone0.Columns[1], clone0.ForeignKeys[0].Columns[0]);
            Assert.Same(cloneContext.GetOrAdd(column10, () => null), clone0.ForeignKeys[0].ReferencedColumns[0]);

            Assert.Equal(1, clone0.Indexes.Count);
            Assert.NotSame(index0, clone0.Indexes[0]);
            Assert.Equal("IX0", clone0.Indexes[0].Name);
            Assert.Equal(1, clone0.Indexes[0].Columns.Count);
            Assert.Same(clone0.Columns[0], clone0.Indexes[0].Columns[0]);

            Assert.Equal(3, cloneContext.ItemCount);
            Assert.Same(clone0.Columns[0], cloneContext.GetOrAdd(column00, () => null));
            Assert.Same(clone0.Columns[1], cloneContext.GetOrAdd(column01, () => null));
            Assert.NotNull(cloneContext.GetOrAdd(column10, () => null));

            var clone1 = table1.Clone(cloneContext);

            Assert.NotSame(table1, clone1);
            Assert.Equal("dbo.T1", clone1.Name);

            Assert.Equal(2, clone1.Columns.Count);
            Assert.NotSame(column10, clone1.Columns[0]);
            Assert.NotSame(column11, clone1.Columns[1]);
            Assert.Equal("C10", clone1.Columns[0].Name);
            Assert.Equal("C11", clone1.Columns[1].Name);
            Assert.Equal(typeof(int), clone1.Columns[0].ClrType);
            Assert.Equal(typeof(string), clone1.Columns[1].ClrType);

            Assert.NotNull(clone1.PrimaryKey);
            Assert.NotSame(primaryKey1, clone1.PrimaryKey);
            Assert.Equal("PK1", clone1.PrimaryKey.Name);
            Assert.Equal(1, clone1.PrimaryKey.Columns.Count);
            Assert.Same(clone1.Columns[0], clone1.PrimaryKey.Columns[0]);

            Assert.Equal(1, clone1.ForeignKeys.Count);
            Assert.NotSame(foreignKey1, clone1.ForeignKeys[0]);
            Assert.Equal("FK1", clone1.ForeignKeys[0].Name);
            Assert.Equal(1, clone1.ForeignKeys[0].Columns.Count);
            Assert.Equal(1, clone1.ForeignKeys[0].ReferencedColumns.Count);
            Assert.Same(clone1.Columns[1], clone1.ForeignKeys[0].Columns[0]);
            Assert.Same(clone0.Columns[0], clone1.ForeignKeys[0].ReferencedColumns[0]);
            Assert.Same(clone1.Columns[0], clone0.ForeignKeys[0].ReferencedColumns[0]);

            Assert.Equal(1, clone1.Indexes.Count);
            Assert.NotSame(index1, clone1.Indexes[0]);
            Assert.Equal("IX1", clone1.Indexes[0].Name);
            Assert.Equal(1, clone1.Indexes[0].Columns.Count);
            Assert.Same(clone1.Columns[0], clone1.Indexes[0].Columns[0]);

            Assert.Equal(4, cloneContext.ItemCount);
            Assert.Same(clone0.Columns[0], cloneContext.GetOrAdd(column00, () => null));
            Assert.Same(clone0.Columns[1], cloneContext.GetOrAdd(column01, () => null));
            Assert.Same(clone1.Columns[0], cloneContext.GetOrAdd(column10, () => null));
            Assert.Same(clone1.Columns[1], cloneContext.GetOrAdd(column11, () => null));
        }
Esempio n. 12
0
        public void GetIndex_finds_index_by_name()
        {
            var column0 = new Column("Foo", "int");
            var column1 = new Column("Bar", "int");
            var table = new Table("dbo.MyTable", new[] { column0, column1 });
            var index0 = new Index("MyIndex0", new[] { column0 });
            var index1 = new Index("MyIndex1", new[] { column1 });

            table.AddIndex(index0);
            table.AddIndex(index1);

            Assert.Same(index0, table.GetIndex("MyIndex0"));
            Assert.Same(index1, table.GetIndex("MyIndex1"));
        }
Esempio n. 13
0
        public void RemoveIndex_removes_specified_index()
        {
            var column0 = new Column("Foo", "int");
            var column1 = new Column("Bar", "int");
            var table = new Table("dbo.MyTable", new[] { column0, column1 });
            var index0 = new Index("MyIndex0", new[] { column0 });
            var index1 = new Index("MyIndex1", new[] { column1 });

            table.AddIndex(index0);
            table.AddIndex(index1);

            Assert.Equal(2, table.Indexes.Count);

            table.RemoveIndex("MyIndex1");

            Assert.Equal(1, table.Indexes.Count);
            Assert.Same(index0, table.Indexes[0]);
        }
Esempio n. 14
0
        public void AddIndex_adds_specified_column()
        {
            var column0 = new Column("Foo", "int");
            var column1 = new Column("Bar", "int");
            var table = new Table("dbo.MyTable", new[] { column0, column1 });

            Assert.Equal(0, table.Indexes.Count);

            var index = new Index("MyIndex", new[] { column1 });
            table.AddIndex(index);

            Assert.Equal(1, table.Indexes.Count);
            Assert.Same(column1, index.Columns[0]);
            Assert.Same(table, index.Table);
        }
Esempio n. 15
0
        public void Indexes_gets_read_only_list_of_indexes()
        {
            var column0 = new Column("Foo", "int");
            var column1 = new Column("Bar", "int");
            var table = new Table("dbo.MyTable", new[] { column0, column1 });
            var index0 = new Index("MyIndex0", new[] { column0 });
            var index1 = new Index("MyIndex1", new[] { column1 });

            table.AddIndex(index0);
            table.AddIndex(index1);

            Assert.IsAssignableFrom<IReadOnlyList<Index>>(table.Indexes);
            Assert.Equal(2, table.Indexes.Count);
            Assert.Same(index0, table.Indexes[0]);
            Assert.Same(index1, table.Indexes[1]);
        }
Esempio n. 16
0
        public virtual void AddIndex([NotNull] Index index)
        {
            Check.NotNull(index, "index");

            _indexes.Add(index);
        }