public IEnumerable <ObjectState> GetDesiredState(SchemaConnection connection, IOutput output) { var schemaDriver = connection.SchemaDriver; var stateTable = TableType.Create("dbo.nrdo_object"); var stateTypeField = FieldType.Create(stateTable.Identifier, "type", 0, "nvarchar(100)", false); var stateNameField = FieldType.Create(stateTable.Identifier, "name", 1, "nvarchar(500)", false); var statePk = UniqueIndexType.CreatePrimaryKey(stateTable.Identifier, "nrdo_object_pk", new[] { stateTypeField.Identifier, stateNameField.Identifier }, schemaDriver.DefaultPrimaryKeyCustomState); var subTable = TableType.Create("dbo.nrdo_object_sub"); var subParentTypeField = FieldType.Create(subTable.Identifier, "parent_type", 0, "nvarchar(100)", false); var subParentNameField = FieldType.Create(subTable.Identifier, "parent_name", 1, "nvarchar(500)", false); var subTypeField = FieldType.Create(subTable.Identifier, "type", 2, "nvarchar(100)", false); var subNameField = FieldType.Create(subTable.Identifier, "name", 3, "nvarchar(500)", false); var subPk = UniqueIndexType.CreatePrimaryKey(subTable.Identifier, "nrdo_object_sub_pk", new[] { subParentTypeField.Identifier, subParentNameField.Identifier, subTypeField.Identifier, subNameField.Identifier }, schemaDriver.DefaultPrimaryKeyCustomState); var subFk = FkeyType.Create(subTable.Identifier, stateTable.Identifier, "nrdo_object_sub_parent_fk", true, new[] { new FieldPair(subParentTypeField.Name, stateTypeField.Name), new FieldPair(subParentNameField.Name, stateNameField.Name), }); return(new ObjectState[] { stateTable, stateTypeField, stateNameField, statePk, subTable, subParentTypeField, subParentNameField, subTypeField, subNameField, subPk, subFk }); }
public static void UpdateSchema(SchemaDriver schemaDriver, string connectionString, IOutput output, SchemaChangeOptions options, IEnumerable <ISchemaProvider> providers) { var allProviders = essentialProviders.AddRange(providers); var preProviders = allProviders.OfType <IPrerequisiteSchemaProvider>(); var normalProviders = from provider in allProviders let pre = provider as IPrerequisiteSchemaProvider where pre == null || pre.IncludeInNormalRun select provider; using (var progress = output.ProgressBlock()) { var currentState = DatabaseState.Empty; var objectTypes = ImmutableHashSet <ObjectType> .Empty; StateStorage storage = null; var chunks = progress.GetChunks(1, 19, providers == null ? 0 : 80).ToList(); output.Message("Connecting to database..."); using (var connection = SchemaConnection.Create(schemaDriver, connectionString)) { connection.AcquireSchemaUpdateLock(output, options.SchemaUpdateLockTimeout); output.Verbose("Connected to database."); chunks[0].ProgressComplete(); runSteps(connection, chunks[1], options, "Preparation", preProviders, ref currentState, ref objectTypes, ref storage); runSteps(connection, chunks[2], options, "Processing", normalProviders, ref currentState, ref objectTypes, ref storage); } } }
public static void Run() { String host = "your-test-node"; String upn = "*****@*****.**"; String passwd = "Admin!23"; SchemaConnection conn = new SchemaConnection(host, upn, passwd); //String host1 = "test-node-1"; //String host2 = "test-node-2"; //String host3 = "test-node-3"; //String upn = "*****@*****.**"; //String passwd = "Admin!23"; //String testDataFileNameFormat = @"..\..\..\TestData\{0}.xml"; //SchemaConnTestDataLoader testDataLoader = new SchemaConnTestDataLoader(); //testDataLoader.LoadXML(string.Format(testDataFileNameFormat, host1)); //testDataLoader.LoadXML(string.Format(testDataFileNameFormat, host2)); //testDataLoader.LoadXML(string.Format(testDataFileNameFormat, host3)); //MockEntryFetcherFactory factory = new MockEntryFetcherFactory(testDataLoader); //SchemaConnection conn = new SchemaConnection(factory, host1, upn, passwd); listNodeAvailability(conn); listSchemaDefinitionDiff(conn); listSchemaMetadataDiff(conn); return; }
private T QuerySysTable <T>(string col, string table, string where, params string[] args) { var cmd = (SqlCommand)SchemaConnection.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = string.Format("SELECT {0} FROM sys.{1} WHERE {2}", col, table, where); for (var i = 0; i < args.Length; i++) { cmd.Parameters.Add("@p" + (i + 1), SqlDbType.Char, args[i].Length).Value = args[i]; } var openClose = SchemaConnection.State == ConnectionState.Closed; if (openClose) { SchemaConnection.Open(); } try { return((T)cmd.ExecuteScalar()); } finally { if (openClose) { SchemaConnection.Close(); } } }
private string GetCreateStatement(string dbobject) { var cmd = (SqlCommand)SchemaConnection.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "SELECT OBJECT_DEFINITION(OBJECT_ID(@p1));"; cmd.Parameters.Add("@p1", SqlDbType.Char, dbobject.Length).Value = dbobject; bool openClose = SchemaConnection.State == ConnectionState.Closed; if (openClose) { SchemaConnection.Open(); } try { var create = cmd.ExecuteScalar(); if (create is DBNull) { return(string.Format("CREATE [{0}] AS FAILED TO IMPORT", dbobject)); } return((string)create); } finally { if (openClose) { SchemaConnection.Close(); } } }
public static bool Connects(SchemaConnection a, SchemaConnection b) { if (a.Connector != b.Connector) { return(false); } return(a.Symmetric || b.Symmetric || (a.Flipped != b.Flipped)); }
private SchemaChanges(DatabaseState current, DatabaseState desired, SchemaConnection connection, IOutput output, SchemaChangeOptions options, StateStorage storage) { this.current = current; this.desired = desired; this.connection = connection; this.output = output; this.options = options; this.storage = storage; this.isUpgrade = current.Contains(CompletionType.Create()); }
public StateStorage(SchemaConnection connection) { if (connection == null) { throw new ArgumentNullException("connection"); } this.connection = connection; this.identComparer = new IdentComparer(connection.DbDriver.DbStringComparer); this.subComparer = new SubComparer(identComparer); Refresh(); }
private T QuerySysTable <T>(string col, string table, string where, params string[] args) { SqlCommand cmd = (SqlCommand)SchemaConnection.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = string.Format("SELECT {0} FROM sys.{1} WHERE {2}", col, table, where); for (int i = 0; i < args.Length; i++) { cmd.Parameters.Add("@p" + (i + 1), SqlDbType.Char, args[i].Length).Value = args[i]; } return((T)cmd.ExecuteScalar()); }
public static void MyClassInitialize(TestContext testContext) { host1 = "test-node-1"; host2 = "test-node-2"; host3 = "test-node-3"; upn = "*****@*****.**"; passwd = "123$%^"; testDataLoader = new SchemaConnTestDataLoader(); testDataLoader.LoadXML(string.Format(testDataFileNameFormat, host1)); testDataLoader.LoadXML(string.Format(testDataFileNameFormat, host2)); testDataLoader.LoadXML(string.Format(testDataFileNameFormat, host3)); factory = new MockEntryFetcherFactory(testDataLoader); conn = new SchemaConnection(factory, host1, upn, passwd); }
public void SchemaConnectionConstructorServerUnreachableTest() { try { SchemaConnection conn3 = new SchemaConnection(factory, host3, upn, passwd); Assert.Fail(); } catch (SchemaConnectionException) { // pass } catch (Exception) { Assert.Fail(); } }
public override IEnumerable <RootObjectState <OldVersionCacheMigrationType, OldVersionCacheMigrationType.State> > GetExistingObjects(SchemaConnection connection, ObjectTypeHelper helper) { // This runs before the state table exists, so we wait until the Step is run to figure out what exists. yield break; }
public IEnumerable <ObjectState> GetDesiredState(SchemaConnection connection, IOutput output) { yield return(OldVersionCacheMigrationType.Create(findOldVersionNrdoCache)); }
public override IEnumerable <SubObjectState <FkeyType, State> > GetExistingObjects(SchemaConnection connection, ObjectTypeHelper helper) { return(from fkey in connection.GetAllForeignKeys() select Create(TableType.Identifier(fkey.FromTable.QualifiedName), TableType.Identifier(fkey.ToTable.QualifiedName), fkey.Name, fkey.IsCascadeDelete, from j in fkey.Joins select new FieldPair(j.FromFieldName, j.ToFieldName))); }
public override IEnumerable <RootObjectState <ViewType, State> > GetExistingObjects(SchemaConnection connection, ObjectTypeHelper helper) { return(from view in connection.GetAllViews() select Create(view.QualifiedName, view.Body)); }
public IEnumerable <ObjectState> GetDesiredState(SchemaConnection connection, IOutput output) { yield return(CompletionType.Create()); }
private void LoadPrototypes() { var schemas = wfc.SceneLoader.GetComponentsInChildren <WFCSchema>(); var prototypes = new List <WFCPrototype>(); var counter = 0; // Check all directions; foreach (var schema in schemas) { if (schema.gameObject.activeSelf) { for (int i = 0; i < 4; i++) { var prototype = new WFCPrototype(counter++, i, false, schema); prototypes.Add(prototype); if (schema.AllowFlipped) { var flippedPrototype = new WFCPrototype(counter++, i, true, schema); prototypes.Add(flippedPrototype); } } } } // rotation 0 = 0 (forward becomes forward) // rotation 1 = 90 (forward becomes right) // rotation 2 = 180 (forward becomes back) // rotation 3 = 270 (forward becomes left) foreach (var prototype in prototypes) { var directionCollection = new[] { new List <int>(), new List <int>(), new List <int>(), new List <int>(), new List <int>(), new List <int>(), }; void handlePrototype(WFCPrototype me, WFCPrototype other, int direction) { var m = me.GetSchemaConnectors(direction); var o = other.GetSchemaConnectors(SlotDirection.CounterDirections[direction]); if (m.Any(a => o.Any(b => SchemaConnection.Connects(a, b)))) { directionCollection[direction].Add(other.Id); } } foreach (var o_prototype in prototypes) { var mu = prototype.GetSchemaConnectors(SlotDirection.UP); var md = prototype.GetSchemaConnectors(SlotDirection.DOWN); var ou = o_prototype.GetSchemaConnectors(SlotDirection.UP); var od = o_prototype.GetSchemaConnectors(SlotDirection.DOWN); if (mu.Select(x => x.Connector).Intersect(od.Select(x => x.Connector)).Any()) { directionCollection[SlotDirection.UP].Add(o_prototype.Id); } if (md.Select(x => x.Connector).Intersect(ou.Select(x => x.Connector)).Any()) { directionCollection[SlotDirection.DOWN].Add(o_prototype.Id); } handlePrototype(prototype, o_prototype, SlotDirection.LEFT); handlePrototype(prototype, o_prototype, SlotDirection.RIGHT); handlePrototype(prototype, o_prototype, SlotDirection.FORWARD); handlePrototype(prototype, o_prototype, SlotDirection.BACK); } foreach (var direction in SlotDirection.Directions) { prototype.Possible[direction] = directionCollection[direction].ToArray(); } } wfc.prototypes = prototypes.ToArray(); }
public override IEnumerable <SubObjectState <BeforeStatementType, State> > GetExistingObjects(SchemaConnection connection, ObjectTypeHelper helper) { return(GetBasedOnKnownIdentifiers(connection, helper, (parent, name) => CreateAsCompleted(parent, name))); }
public override IEnumerable <RootObjectState <TableType, Stateless> > GetExistingObjects(SchemaConnection connection, ObjectTypeHelper helper) { return(from table in connection.GetAllTables() select Create(table.QualifiedName)); }
public static SchemaChanges Create(DatabaseState current, DatabaseState desired, SchemaConnection connection, IOutput output, SchemaChangeOptions options, StateStorage storage) { return(new SchemaChanges(current, desired, connection, output, options, storage)); }
public override IEnumerable <SubObjectState <UniqueIndexType, UniqueIndexType.State> > GetExistingObjects(SchemaConnection connection, ObjectTypeHelper helper) { return(from index in connection.GetAllUniqueIndexes() join custom in connection.GetAllIndexCustomState() on new { table = TableType.Identifier(index.Table.QualifiedName), index = UniqueIndexType.Identifier(index.Name) } equals new { table = TableType.Identifier(custom.Table.QualifiedName), index = UniqueIndexType.Identifier(custom.IndexName) } into indexCustom select CreateState(TableType.Identifier(index.Table.QualifiedName), index.Name, new State(index.IsPrimaryKey, from field in index.FieldNames select FieldType.Identifier(field), indexCustom.Any() ? indexCustom.Single().CustomState : index.IsPrimaryKey ? connection.SchemaDriver.DefaultPrimaryKeyCustomState : connection.SchemaDriver.DefaultUniqueConstraintCustomState))); }
public override IEnumerable <RootObjectState <QueryType, ProcState> > GetExistingObjects(SchemaConnection connection, ObjectTypeHelper helper) { var existingNames = new HashSet <string>(connection.DbDriver.DbStringComparer); foreach (var proc in connection.GetAllStoredProcsAndFunctions()) { existingNames.Add(proc.QualifiedName); var func = proc as IntrospectedFunction; if (func != null) { yield return(CreateFunction(func.QualifiedName, func.Parameters, func.ReturnType, func.Body)); } else { yield return(CreateProc(proc.QualifiedName, proc.Parameters, proc.Body)); } } // Queries that do not exist as stored procs or functions just get stored as known objects in the state tables. foreach (var known in GetBasedOnKnownIdentifiers(connection, helper, CreateUnstored)) { if (!existingNames.Contains(known.Name)) { yield return(known); } } }
public override IEnumerable <RootObjectState <CompletionType, Stateless> > GetExistingObjects(SchemaConnection connection, ObjectTypeHelper helper) { return(GetBasedOnKnownIdentifiers(connection, helper, name => Create())); }
public override IEnumerable <SubObjectState <TriggerType, State> > GetExistingObjects(SchemaConnection connection, ObjectTypeHelper helper) { return(from trigger in connection.GetAllTriggers() select Create(TableType.Identifier(trigger.Table.QualifiedName), trigger.QualifiedName, trigger.Timing, trigger.Events, trigger.Body)); }
public abstract IEnumerable <ObjectState> GetExistingObjects(SchemaConnection connection, ObjectTypeHelper helper);
public override IEnumerable<SubObjectState<PreUpgradeHookType, Stateless>> GetExistingObjects(SchemaConnection connection, ObjectTypeHelper helper) { return GetBasedOnKnownIdentifiers(connection, helper, (query, name) => Create(query)); }
public override IEnumerable <SubObjectState <PendingReorderTableType, Stateless> > GetExistingObjects(SchemaConnection connection, ObjectTypeHelper helper) { return(GetBasedOnKnownIdentifiers(connection, helper, (parent, name) => Create(parent, name))); }
public override IEnumerable <RootObjectState <TableRenameType, TableRenameType.State> > GetExistingObjects(SchemaConnection connection, ObjectTypeHelper helper) { // Table renames don't get tracked in existing databases at all - no point! yield break; }
private static void runSteps(SchemaConnection connection, IOutput output, SchemaChangeOptions options, string phaseName, IEnumerable <ISchemaProvider> providers, ref DatabaseState currentState, ref ImmutableHashSet <ObjectType> objectTypes, ref StateStorage storage) { using (var progress = output.ProgressBlock()) { output.Message(phaseName + ": Starting..."); // Get object types, get steps, get desired state, get current state, everything else var chunks = progress.GetChunks(1, 1, 5, 5, 1, 50).ToList(); var oldTypes = objectTypes; using (chunks[0].Start()) { objectTypes = (from provider in providers from objectType in provider.GetObjectTypes(connection.SchemaDriver) select objectType).ToImmutableHashSet(); output.Verbose(phaseName + ": Object types: " + string.Join(", ", objectTypes)); } // Get all the steps and organize them into order IImmutableList <StepBase> steps; using (chunks[1].Start()) { steps = sortSteps(from objectType in objectTypes from step in objectType.Steps select step); output.Verbose(phaseName + ": Steps: " + string.Join(", ", steps)); } // Get the desired state DatabaseState desiredState; using (chunks[2].Start()) { output.Verbose(phaseName + ": Determining desired database state..."); desiredState = DatabaseState.Create(from provider in providers from state in provider.GetDesiredState(connection, chunks[2]) select state); foreach (var overrideProvider in providers.OfType <ISchemaOverrideProvider>()) { desiredState = overrideProvider.ApplyOverrides(connection, desiredState); } output.Message(phaseName + ": " + counted(desiredState.Count, "object") + " desired."); } ObjectTypeHelper helper; using (chunks[3].Start()) { // Get the current state output.Verbose(phaseName + ": Determining current database state..."); helper = new ObjectTypeHelper(objectTypes, storage != null, connection.DbDriver); currentState = currentState.With(from objectType in objectTypes where !oldTypes.Contains(objectType) from state in objectType.GetExistingObjects(connection, helper) select state); output.Verbose(phaseName + ": " + counted(currentState.Count, "object") + " existing."); } // Filter to only things that are relevant here using (chunks[4].Start()) { output.Verbose(phaseName + ": Filtering..."); if (storage != null) { var priorKnownRoots = storage.GetKnownRoots(helper); var priorKnownSubs = storage.GetKnownSubs(helper); // Anything that exists in *both* current and desired is treated as known and assumed to be in-scope for consideration var knownRoots = priorKnownRoots.Union(from root in currentState.RootObjects where desiredState.Contains(root) select root.Identifier); foreach (var extra in knownRoots.Except(priorKnownRoots)) { storage.PutRoot(extra); } // FIXME: remove from storage anything that's of a type that's under consideration but is neither current nor desired var knownSubs = priorKnownSubs.Union(from sub in currentState.AllChildren where desiredState.Contains(sub) select Tuple.Create(sub.ParentIdentifier, sub.Identifier)); foreach (var extra in knownSubs.Except(priorKnownSubs)) { storage.PutSub(extra.Item1, extra.Item2); } // FIXME: remove from storage anything that's of a type that's under consideration but is neither current nor desired var _storage = storage; var initialState = currentState; currentState = currentState.WithFilters((root) => desiredState.ContainsRoot(root) || Objects.Tables.TableRenameType.ContainsRenameTo(desiredState, root) || Objects.Tables.PendingReorderTableType.IsTablePendingReorder(initialState, root) || (options.PreserveUnknownObjects ? !initialState.ContainsRoot(root) : _storage.ContainsRoot(root)), null); } else { // FIXME this doesn't allow for any flexibility in whether the nrdo_ tables live in dbo currentState = currentState.WithFilters(root => root.Name.StartsWith("dbo.nrdo_", StringComparison.OrdinalIgnoreCase), null); } output.Message(phaseName + ": " + counted(currentState.Count, "applicable existing object") + "."); } var isFullRun = storage != null; if (isFullRun) { // Sanity check for any before statements that will be skipped due to the associated step not existing var stepSet = new HashSet <string>(from step in steps select step.Identifier, Nstring.DBEquivalentComparer); var current = currentState; // Can't use ref parameter directly inside linq query var unknownBefores = from stmt in BeforeStatementType.AllFrom(desiredState) where !stepSet.Contains(stmt.State.Step) && !current.Contains(stmt) select stmt; foreach (var stmt in unknownBefores) { output.Warning(stmt.Name + " on " + stmt.ParentIdentifier + " will not be executed because there is no such step as " + stmt.State.Step); } } var changes = SchemaChanges.Create(currentState, desiredState, connection, chunks[5], options, storage); changes.PerformSteps(steps, phaseName, isFullRun); currentState = changes.Current.WithoutFilters(); storage = changes.Storage; if (changes.HasFailed) { throw new ApplicationException("Schema update failed"); } output.Message(phaseName + ": Completed."); output.Message(phaseName + ": " + counted(changes.StatementCount, "statement") + " executed."); output.Message(phaseName + ": " + counted(currentState.Count, "object") + " existing."); } }
public override IEnumerable <SubObjectState <FieldType, State> > GetExistingObjects(SchemaConnection connection, ObjectTypeHelper helper) { return(from field in connection.GetAllFields() let table = TableType.Identifier(field.Table.QualifiedName) select field.IsSequencedPkey ? CreateSequencedPkey(table, field.Name, field.OrdinalPosition, field.DataType, field.IsNullable, field.SequenceName) : Create(table, field.Name, field.OrdinalPosition, field.DataType, field.IsNullable)); }