Exemplo n.º 1
0
        /// <summary>
        /// Ensure configuration is correct on both server and client side
        /// </summary>
        public virtual async Task <(SyncContext, DmSet)> EnsureSchemaAsync(SyncContext context, MessageEnsureSchema message)
        {
            DbConnection  connection  = null;
            DbTransaction transaction = null;

            try
            {
                context.SyncStage = SyncStage.SchemaReading;

                using (connection = this.CreateConnection())
                {
                    await connection.OpenAsync();

                    await this.InterceptAsync(new ConnectionOpenArgs(context, connection));

                    using (transaction = connection.BeginTransaction())
                    {
                        await this.InterceptAsync(new TransactionOpenArgs(context, connection, transaction));

                        // if we dont have already read the tables || we want to overwrite the current config
                        if (message.Schema.HasTables && !message.Schema.HasColumns)
                        {
                            this.ReadSchema(message.Schema, connection, transaction);
                        }

                        // Progress & Interceptor
                        context.SyncStage = SyncStage.SchemaRead;
                        var schemaArgs = new SchemaArgs(context, message.Schema, connection, transaction);
                        this.ReportProgress(context, schemaArgs);
                        await this.InterceptAsync(schemaArgs);

                        await this.InterceptAsync(new TransactionCommitArgs(context, connection, transaction));

                        transaction.Commit();
                    }

                    connection.Close();
                }

                return(context, message.Schema);
            }
            catch (SyncException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SyncException(ex, SyncStage.SchemaReading);
            }
            finally
            {
                if (connection != null && connection.State != ConnectionState.Closed)
                {
                    connection.Close();
                }

                await this.InterceptAsync(new ConnectionCloseArgs(context, connection, transaction));
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Ensure configuration is correct on both server and client side
        /// </summary>
        public virtual async Task <(SyncContext, SyncSet)> EnsureSchemaAsync(SyncContext context, SyncSetup setup,
                                                                             DbConnection connection, DbTransaction transaction,
                                                                             CancellationToken cancellationToken, IProgress <ProgressArgs> progress = null)
        {
            context.SyncStage = SyncStage.SchemaReading;

            var schema = this.ReadSchema(setup, connection, transaction);

            // Progress & Interceptor
            context.SyncStage = SyncStage.SchemaRead;
            var schemaArgs = new SchemaArgs(context, schema, connection, transaction);

            this.ReportProgress(context, progress, schemaArgs);
            await this.InterceptAsync(schemaArgs).ConfigureAwait(false);

            return(context, schema);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Ensure configuration is correct on both server and client side
        /// </summary>
        public virtual async Task <(SyncContext, DmSet)> EnsureSchemaAsync(SyncContext context, MessageEnsureSchema message)
        {
            try
            {
                context.SyncStage = SyncStage.SchemaApplying;

                using (var connection = this.CreateConnection())
                {
                    await connection.OpenAsync();

                    using (var transaction = connection.BeginTransaction())
                    {
                        // Progress
                        context.SyncStage = SyncStage.SchemaApplying;

                        // if we dont have already read the tables || we want to overwrite the current config
                        if (message.Schema.HasTables && !message.Schema.HasColumns)
                        {
                            await this.ReadSchemaAsync(message.Schema, connection, transaction);
                        }

                        // Progress & Interceptor
                        context.SyncStage = SyncStage.SchemaApplied;
                        var schemaArgs = new SchemaArgs(context, message.Schema, connection, transaction);
                        this.ReportProgress(context, schemaArgs);
                        await this.InterceptAsync(schemaArgs);

                        transaction.Commit();
                    }

                    connection.Close();
                }

                return(context, message.Schema);
            }
            catch (SyncException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new SyncException(ex, SyncStage.SchemaApplying);
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Read the schema stored from the orchestrator database, through the provider.
        /// </summary>
        /// <returns>Schema containing tables, columns, relations, primary keys</returns>
        public virtual async Task <SyncSet> GetSchemaAsync(CancellationToken cancellationToken = default, IProgress <ProgressArgs> progress = null)
        {
            if (!this.StartTime.HasValue)
            {
                this.StartTime = DateTime.UtcNow;
            }

            // Get context or create a new one
            var ctx = this.GetContext();


            SyncSet schema = null;

            using (var connection = this.Provider.CreateConnection())
            {
                // Encapsulate in a try catch for a better exception handling
                // Especially whew called from web proxy
                try
                {
                    this.logger.LogInformation(SyncEventsId.GetSchema, this.Setup);

                    ctx.SyncStage = SyncStage.SchemaReading;

                    if (this.Setup.Tables.Count <= 0)
                    {
                        throw new MissingTablesException();
                    }

                    // Open connection
                    await this.OpenConnectionAsync(connection, cancellationToken).ConfigureAwait(false);

                    // Create a transaction
                    using (var transaction = connection.BeginTransaction())
                    {
                        await this.InterceptAsync(new TransactionOpenedArgs(ctx, connection, transaction), cancellationToken).ConfigureAwait(false);

                        (ctx, schema) = await this.Provider.GetSchemaAsync(ctx, this.Setup, connection, transaction, cancellationToken, progress).ConfigureAwait(false);

                        await this.InterceptAsync(new TransactionCommitArgs(ctx, connection, transaction), cancellationToken).ConfigureAwait(false);

                        transaction.Commit();
                    }

                    ctx.SyncStage = SyncStage.SchemaRead;

                    await this.CloseConnectionAsync(connection, cancellationToken).ConfigureAwait(false);

                    var schemaArgs = new SchemaArgs(ctx, schema, connection);
                    await this.InterceptAsync(schemaArgs, cancellationToken).ConfigureAwait(false);

                    this.ReportProgress(ctx, progress, schemaArgs);
                }
                catch (Exception ex)
                {
                    RaiseError(ex);
                }
                finally
                {
                    if (connection != null && connection.State != ConnectionState.Closed)
                    {
                        connection.Close();
                    }
                }

                return(schema);
            }
        }