public static void AddColumnForForeignKey <T>(this AlterTableCommand self, Action <AddColumnCommand> column = null, string columnNameOverride = null) { var recordType = typeof(T); var columnName = columnNameOverride.HasValue() ? columnNameOverride : recordType.Name + "_Id"; self.AddColumn <int>(columnName, column); }
public static void AddColumnFor <TColumn, TRecord>(this AlterTableCommand self, Expression <Func <TRecord, object> > expression, Action <AddColumnCommand> column = null) { #pragma warning disable CS0618 // We need enum support here var property = ObjectExtensions.PropertyName(expression); #pragma warning restore CS0618 // Type or member is obsolete self.AddColumn <TColumn>(property, column); }
public static AlterTableCommand AddColumn <TModel, TProperty>( this AlterTableCommand command, Expression <Func <TModel, TProperty> > propertySelector, Action <CreateColumnCommand> column = null, bool isPrefixWithModelType = false) { var columnName = GetColumnName(propertySelector, isPrefixWithModelType); var propertyType = GetPropertyType <TProperty>(); var dbType = SchemaUtils.ToDbType(propertyType); command.AddColumn(columnName, dbType, column); return(command); }
public void HandleAlterTables() { var originalTable = "Create Table TEST_TestTable (id INTEGER primary key autoincrement, name TEXT(40) NULL, last TEXT(256), " + "CONSTRAINT TEST_FK FOREIGN KEY (id) REFERENCES FAKE_TABLE (id) DEFERRABLE INITIALLY DEFERRED," + "CONSTRAINT KEEP_FK FOREIGN KEY (temp, id) REFERENCES FAKE_TABLE (temp, id) DEFERRABLE INITIALLY DEFERRED);"; var originalIndices = new[] { "CREATE INDEX some_index ON TEST_TestTable (id)", "CREATE INDEX name_index ON TEST_TestTable (name)", "CREATE INDEX this_should_be_gone ON TEST_TestTable (name, last)", }; var input = new AlterTableCommand("TEST_TestTable"); input.CreateIndex("funny_index", "id", "name"); input.DropIndex("some_index"); input.AddColumn("add_column", "TINYINT"); input.DropColumn("last"); //no, this doesn't make sense; no, I don't care input.AlterColumn("name", c => c.WithDefault(0).WithType("INTEGER")); input.DropForeignKey("TEST_FK"); input.CreateForeignKey("TEST_FK2", new [] { "id", "last" }, "SOME_OTHERTABLE", new [] { "something", "else" }); var expectedFinal = new[] { //this line expects the guid to be added "CREATE TEMPORARY TABLE TEST_TestTable_ AS SELECT * FROM TEST_TestTable;", "DROP TABLE TEST_TestTable;", "CREATE TABLE TEST_TestTable (id INTEGER primary key autoincrement, name INTEGER default 0, add_column TINYINT default NULL, " + "CONSTRAINT KEEP_FK FOREIGN KEY (temp, id) REFERENCES FAKE_TABLE (temp, id) DEFERRABLE INITIALLY DEFERRED, " + "CONSTRAINT TEST_FK2 FOREIGN KEY (id, last) REFERENCES SOME_OTHERTABLE (something, else) DEFERRABLE INITIALLY DEFERRED);", //this line expects the guid to be added "INSERT INTO TEST_TestTable (id, name) SELECT id, name FROM TEST_TestTable_;", "CREATE INDEX name_index ON TEST_TestTable (name);", "CREATE INDEX funny_index ON TEST_TestTable (id, name);", //this line expects the guid to be added "DROP TABLE TEST_TestTable_;", }.Select(LowerAndWhitespaceFreeString).ToArray(); var adapter = new AlterTableAdapter(SQLiteParseVisitor.ParseString <CreateTableNode>(originalTable), originalIndices.Select(SQLiteParseVisitor.ParseString <CreateIndexNode>)); var output = adapter.AlterTableStatements(input).Select(LowerAndWhitespaceFreeString).ToArray(); VerifyYourStatementsAreValid(expectedFinal, output); }
public void FirstAlterTableTest() { var originalTable = "Create Table TEST_TestTable (id INTEGER primary key autoincrement, name TEXT(40) NULL, last TEXT(256))"; var originalIndices = new[] { "CREATE INDEX some_index ON TEST_TestTable (id)", "CREATE INDEX name_index ON TEST_TestTable (name)", "CREATE INDEX this_should_be_gone ON TEST_TestTable (name, last)", }; var input = new AlterTableCommand("TEST_TestTable"); input.CreateIndex("funny_index", "id", "name"); input.DropIndex("some_index"); input.AddColumn("add_column", "TINYINT", command => command.WithType("TINYINT")); input.DropColumn("last"); //no, this doesn't make sense; no, I don't care input.AlterColumn("name", c => c.WithDefault(0).WithType("INTEGER")); var expectedFinal = new[] { //this line expects the guid to be added "CREATE TEMPORARY TABLE TEST_TestTable_ AS SELECT * FROM TEST_TestTable;", "DROP TABLE TEST_TestTable;", "CREATE TABLE TEST_TestTable (id INTEGER primary key autoincrement, name INTEGER default 0, add_column TINYINT default NULL);", //this line expects the guid to be added "INSERT INTO TEST_TestTable (id, name) SELECT id, name FROM TEST_TestTable_;", "CREATE INDEX name_index ON TEST_TestTable (name);", "CREATE INDEX funny_index ON TEST_TestTable (id, name);", //this line expects the guid to be added "DROP TABLE TEST_TestTable_;", }; var adapter = new AlterTableAdapter(originalTable, originalIndices); var output = adapter.AlterTableStatements(input).Select(LowerAndWhitespaceFreeString).ToArray(); VerifyYourStatementsAreValid(expectedFinal, output); }