public void ReverseSetsTableNameAndColumnNameOnGeneratedExpression() { var expression = new CreateColumnExpression { TableName = "Bacon", Column = { Name = "BaconId" } }; var reverse = expression.Reverse() as DeleteColumnExpression; reverse.TableName.ShouldBe("Bacon"); reverse.ColumnName.ShouldBe("BaconId"); }
public void WhenDefaultSchemaConventionIsAppliedAndSchemaIsNotSetThenSchemaShouldBeNull() { var expression = new CreateColumnExpression { TableName = "Bacon", Column = { Name = "BaconId", Type = DbType.Int32 } }; expression.ApplyConventions(new MigrationConventions()); Assert.That(expression.SchemaName, Is.Null); }
public void WhenDefaultSchemaConventionIsChangedAndSchemaIsNotSetThenSetSchema() { var expression = new CreateColumnExpression { TableName = "Bacon", Column = { Name = "BaconId", Type = DbType.Int32 } }; var processed = expression.Apply(ConventionSets.WithSchemaName); Assert.That(processed.SchemaName, Is.EqualTo("testdefault")); }
public void WhenDefaultSchemaConventionIsAppliedAndSchemaIsSetThenSchemaShouldNotBeChanged() { var expression = new CreateColumnExpression { SchemaName = "testschema", TableName = "Bacon", Column = { Name = "BaconId", Type = DbType.Int32 } }; expression.ApplyConventions(new MigrationConventions()); Assert.That(expression.SchemaName, Is.EqualTo("testschema")); }
public void WhenDefaultSchemaConventionIsAppliedAndSchemaIsNotSetThenSchemaShouldBeNull() { var expression = new CreateColumnExpression { TableName = "Bacon", Column = { Name = "BaconId", Type = DbType.Int32 } }; var processed = expression.Apply(ConventionSets.NoSchemaName); Assert.That(processed.SchemaName, Is.Null); }
public ICreateColumnOnTableSyntax Column(string columnName) { var expression = new CreateColumnExpression(_context.CurrentDatabaseProvider, _databaseProviders, _sqlSyntax) { Column = { Name = columnName } }; _context.Expressions.Add(expression); return(new CreateColumnBuilder(_context, _databaseProviders, expression)); }
public void CanDumpCreateColumnExpression() { var expression = new CreateColumnExpression { TableName = _tableName, Column = { Name = _columnName, IsIdentity = true, IsPrimaryKey = true, Type = DbType.String } }; string expectedSql = _generator.Generate(expression); _fileDumpProcessor.Process(expression); Lines.ShouldContain(expectedSql); }
public override string Generate(CreateColumnExpression expression) { var errors = ValidateAdditionalFeatureCompatibility(expression.Column.AdditionalFeatures); if (!string.IsNullOrEmpty(errors)) { return(errors); } return(string.Format(AddColumn, Quoter.QuoteTableName(expression.TableName, expression.SchemaName), Column.Generate(expression.Column))); }
protected void SetupUndoDeleteColumn(DeleteColumnExpression expression) { FirebirdSchemaProvider schema = new FirebirdSchemaProvider(Processor); FirebirdTableSchema table = schema.GetTableSchema(expression.TableName); bool hasPrimary = table.HasPrimaryKey; CanUndo = true; using (IDataReader ds = Processor.ReadTableData(String.Empty, expression.TableName)) { //Create columns foreach (string columnName in expression.ColumnNames) { CreateColumnExpression createColumn = new CreateColumnExpression() { SchemaName = String.Empty, TableName = expression.TableName, Column = table.Definition.Columns.First(x => x.Name.ToUpper() == columnName.ToUpper()) }; UndoExpressions.Add(createColumn); //NB: No need to recreate indices? Drop should fail if detects existing user created indices } //Update data while (ds.Read()) { if (!hasPrimary) { CanUndo = false; return; } UpdateDataExpression update = new UpdateDataExpression() { TableName = expression.TableName, IsAllRows = false, Set = new List <KeyValuePair <string, object> >(), Where = new List <KeyValuePair <string, object> >() }; foreach (string columnName in expression.ColumnNames) { update.Set.Add(new KeyValuePair <string, object>(columnName, ds[columnName])); } table.Definition.Columns.ToList().ForEach(col => { if (col.IsPrimaryKey) { update.Where.Add(new KeyValuePair <string, object>(col.Name, ds[col.Name])); } }); UndoExpressions.Add(update); } } }
public override string Generate(CreateColumnExpression expression) { var alterTableStatement = base.Generate(expression); var descriptionStatement = DescriptionGenerator.GenerateDescriptionStatement(expression); if (string.IsNullOrEmpty(descriptionStatement)) { return(alterTableStatement); } return(ComposeStatements(alterTableStatement, new[] { descriptionStatement })); }
public void CanCreateColumnWithBinaryIntMax() { var column = new ColumnDefinition { Name = GeneratorTestHelper.TestColumnName1, Type = DbType.Binary, Size = int.MaxValue }; var expression = new CreateColumnExpression { TableName = GeneratorTestHelper.TestTableName1, Column = column }; var result = Generator.Generate(expression); result.ShouldBe("ALTER TABLE `TestTable1` ADD COLUMN `TestColumn1` LONGBLOB NOT NULL"); }
public override string Generate(CreateColumnExpression expression) { var alterTableStatement = string.Format("ALTER TABLE {0}.{1}", Quoter.QuoteSchemaName(expression.SchemaName), base.Generate(expression)); var descriptionStatement = DescriptionGenerator.GenerateDescriptionStatement(expression); if (string.IsNullOrEmpty(descriptionStatement)) { return(alterTableStatement); } return(ComposeStatements(alterTableStatement, new[] { descriptionStatement })); }
/// <inheritdoc /> public ICreateColumnOnTableSyntax Column(string columnName) { var expression = new CreateColumnExpression { Column = { Name = columnName } }; _context.Expressions.Add(expression); return(new CreateColumnExpressionBuilder(expression, _context)); }
public void CanUseSystemMethodCurrentDateTimeAsADefaultValueForAColumn() { var columnDefinition = new ColumnDefinition { Name = "NewColumn", Size = 15, Type = null, CustomType = "TIMESTAMP", DefaultValue = SystemMethods.CurrentDateTime }; var expression = new CreateColumnExpression { Column = columnDefinition, TableName = "NewTable" }; var result = Generator.Generate(expression); result.ShouldBe("ALTER TABLE `NewTable` ADD COLUMN `NewColumn` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP"); }
public void WhenDefaultSchemaConventionIsChangedAndSchemaIsNotSetThenSetSchema() { var expression = new CreateColumnExpression { TableName = "Bacon", Column = { Name = "BaconId", Type = DbType.Int32 } }; var migrationConventions = new MigrationConventions { GetDefaultSchema = () => "testdefault" }; expression.ApplyConventions(migrationConventions); Assert.That(expression.SchemaName, Is.EqualTo("testdefault")); }
public void CanCreateXmlColumn() { var expression = new CreateColumnExpression { TableName = "TestTable1", Column = new ColumnDefinition { Name = "TestColumn1", Type = DbType.Xml } }; var result = Generator.Generate(expression); result.ShouldBe("ALTER TABLE [TestTable1] ADD [TestColumn1] NTEXT NOT NULL"); }
public void CanCreateXmlColumn() { var expression = new CreateColumnExpression(); expression.TableName = "TestTable1"; expression.Column = new ColumnDefinition(); expression.Column.Name = "TestColumn1"; expression.Column.Type = DbType.Xml; var sql = generator.Generate(expression); sql.ShouldBe("ALTER TABLE [TestTable1] ADD [TestColumn1] NTEXT NOT NULL"); }
public void CanCreateXmlColumn() { var expression = new CreateColumnExpression { TableName = "Table1", Column = new ColumnDefinition { Name = "MyXmlColumn", Type = DbType.Xml } }; var result = Generator.Generate(expression); result.ShouldNotBeNull(); }
public void CanUseSystemMethodCurrentCDateTimeAsADefaultValueForAColumn() { const string tableName = "NewTable"; var columnDefinition = new ColumnDefinition { Name = "NewColumn", Size = 5, Type = DbType.String, DefaultValue = SystemMethods.CurrentDateTime }; var expression = new CreateColumnExpression { Column = columnDefinition, TableName = tableName }; var result = Generator.Generate(expression); result.ShouldBe("ALTER TABLE \"NewTable\" ADD (\"NewColumn\" NVARCHAR(5) DEFAULT CURRENT_TIMESTAMP);"); }
public void CanCreateXmlColumn() { var expression = new CreateColumnExpression(); expression.TableName = "Table1"; expression.Column = new ColumnDefinition(); expression.Column.Name = "MyXmlColumn"; expression.Column.Type = DbType.Xml; var sql = _generator.Generate(expression); sql.ShouldNotBeNull(); }
public override string Generate(CreateColumnExpression expression) { var createStatement = new StringBuilder(); createStatement.AppendFormat("ALTER TABLE {0} ADD {1};", Quoter.QuoteTableName(expression.TableName, expression.SchemaName), Column.Generate(expression.Column)); var descriptionStatement = DescriptionGenerator.GenerateDescriptionStatement(expression); if (!string.IsNullOrEmpty(descriptionStatement)) { createStatement.Append(";"); createStatement.Append(descriptionStatement); } return(createStatement.ToString()); }
public static CreateColumnExpression GetCreateColumnExpressionWithNullableCustomType() { var expression = new CreateColumnExpression(); expression.TableName = TestTableName1; expression.Column = new ColumnDefinition(); expression.Column.Name = TestColumnName1; expression.Column.IsNullable = true; expression.Column.CustomType = "MyDomainType"; expression.Column.ModificationType = ColumnModificationType.Create; return(expression); }
public void CanCreateJsonColumnWithDefaultSchema() { var column = new ColumnDefinition { Name = "TestColumn1", DefaultValue = "{}", CustomType = "json" }; var expression = new CreateColumnExpression { TableName = "TestTable1", Column = column }; var result = Generator.Generate(expression); result.ShouldBe("ALTER TABLE \"public\".\"TestTable1\" ADD \"TestColumn1\" json NOT NULL DEFAULT '{}';"); }
public void CanUseSystemMethodCurrentUTCDateTimeAsADefaultValueForAColumn() { const string tableName = "NewTable"; var columnDefinition = new ColumnDefinition { Name = "NewColumn", Size = 5, Type = DbType.String, DefaultValue = SystemMethods.CurrentUTCDateTime }; var expression = new CreateColumnExpression { Column = columnDefinition, TableName = tableName }; var result = Generator.Generate(expression); result.ShouldBe("ALTER TABLE NewTable ADD COLUMN NewColumn VARGRAPHIC(5) CCSID 1200 NOT NULL DEFAULT (CURRENT_TIMESTAMP - CURRENT_TIMEZONE)"); }
public static CreateColumnExpression GetCreateColumnExpressionWithDateTimeOffsetType() { var expression = new CreateColumnExpression(); expression.TableName = TestTableName1; expression.Column = new ColumnDefinition(); expression.Column.Name = TestColumnName1; expression.Column.IsNullable = true; expression.Column.Type = DbType.DateTimeOffset; expression.Column.ModificationType = ColumnModificationType.Create; return(expression); }
public void CanUseSystemMethodCurrentUserAsADefaultValueForAColumn() { const string tableName = "NewTable"; var columnDefinition = new ColumnDefinition { Name = "NewColumn", Size = 18, Type = DbType.AnsiString, DefaultValue = SystemMethods.CurrentUser }; var expression = new CreateColumnExpression { Column = columnDefinition, TableName = tableName }; var result = Generator.Generate(expression); result.ShouldBe("ALTER TABLE NewTable ADD COLUMN NewColumn VARCHAR(18) NOT NULL DEFAULT USER"); }
public void CanUseSystemMethodCurrentDateTimeOffsetAsADefaultValueForAColumn() { const string tableName = "NewTable"; var columnDefinition = new ColumnDefinition { Name = "NewColumn", Size = 5, Type = DbType.String, DefaultValue = SystemMethods.CurrentDateTimeOffset }; var expression = new CreateColumnExpression { Column = columnDefinition, TableName = tableName }; var result = Generator.Generate(expression); result.ShouldBe("ALTER TABLE \"public\".\"NewTable\" ADD \"NewColumn\" varchar(5) NOT NULL DEFAULT current_timestamp;"); }
public void CanCreateXmlColumnWithDefaultSchema() { var expression = new CreateColumnExpression(); expression.TableName = "TestTable1"; expression.Column = new ColumnDefinition(); expression.Column.Name = "TestColumn1"; expression.Column.Type = DbType.Xml; var result = Generator.Generate(expression); result.ShouldBe("ALTER TABLE [dbo].[TestTable1] ADD [TestColumn1] XML NOT NULL"); }
public void GetPrimaryKeyNamePrefixesTableNameWithPKAndUnderscore() { var expr = new CreateColumnExpression() { Column = { TableName = "Foo", IsPrimaryKey = true, } }; var processed = expr.Apply(ConventionSets.NoSchemaName); processed.Column.PrimaryKeyName.ShouldBe("PK_Foo"); }
public void CanCreateXmlColumnWithCustomSchema() { var expression = new CreateColumnExpression(); expression.TableName = "TestTable1"; expression.Column = new ColumnDefinition(); expression.Column.Name = "TestColumn1"; expression.Column.Type = DbType.Xml; expression.SchemaName = "TestSchema"; var sql = generator.Generate(expression); sql.ShouldBe("ALTER TABLE [TestSchema].[TestTable1] ADD [TestColumn1] XML NOT NULL"); }