public Schema CreateAvroSchema(string projectId, string schemaId, string pathToDefinition)
    {
        SchemaServiceClient schemaService = SchemaServiceClient.Create();
        var    schemaName       = SchemaName.FromProjectSchema(projectId, schemaId);
        string schemaDefinition = File.ReadAllText(pathToDefinition);
        Schema schema           = new Schema
        {
            Name       = schemaName.ToString(),
            Type       = Schema.Types.Type.Avro,
            Definition = schemaDefinition
        };
        CreateSchemaRequest createSchemaRequest = new CreateSchemaRequest
        {
            Parent   = "projects/" + projectId,
            SchemaId = schemaId,
            Schema   = schema
        };

        try
        {
            schema = schemaService.CreateSchema(createSchemaRequest);
            Console.WriteLine($"Schema {schema.Name} created.");
        }
        catch (RpcException e) when(e.Status.StatusCode == StatusCode.AlreadyExists)
        {
            Console.WriteLine($"Schema {schemaName} already exists.");
        }
        return(schema);
    }
    public void DeleteSchema(string projectId, string schemaId)
    {
        SchemaServiceClient schemaService = SchemaServiceClient.Create();
        SchemaName          schemaName    = SchemaName.FromProjectSchema(projectId, schemaId);

        schemaService.DeleteSchema(schemaName);
    }
Пример #3
0
    public IEnumerable <Schema> ListSchemas(string projectId)
    {
        SchemaServiceClient schemaService = SchemaServiceClient.Create();
        ProjectName         projectName   = ProjectName.FromProject(projectId);
        var schemas = schemaService.ListSchemas(projectName);

        return(schemas);
    }
Пример #4
0
 /// <summary>Snippet for GetSchema</summary>
 public void GetSchemaResourceNames()
 {
     // Snippet: GetSchema(SchemaName, CallSettings)
     // Create client
     SchemaServiceClient schemaServiceClient = SchemaServiceClient.Create();
     // Initialize request argument(s)
     SchemaName name = SchemaName.FromProjectSchema("[PROJECT]", "[SCHEMA]");
     // Make the request
     Schema response = schemaServiceClient.GetSchema(name);
     // End snippet
 }
Пример #5
0
 /// <summary>Snippet for GetSchema</summary>
 public void GetSchema()
 {
     // Snippet: GetSchema(string, CallSettings)
     // Create client
     SchemaServiceClient schemaServiceClient = SchemaServiceClient.Create();
     // Initialize request argument(s)
     string name = "projects/[PROJECT]/schemas/[SCHEMA]";
     // Make the request
     Schema response = schemaServiceClient.GetSchema(name);
     // End snippet
 }
    public Schema GetSchema(string projectId, string schemaId)
    {
        SchemaServiceClient schemaService = SchemaServiceClient.Create();
        SchemaName          schemaName    = SchemaName.FromProjectSchema(projectId, schemaId);
        GetSchemaRequest    request       = new GetSchemaRequest
        {
            Name = schemaName.ToString(),
            View = SchemaView.Full
        };

        return(schemaService.GetSchema(request));
    }
Пример #7
0
 /// <summary>Snippet for ValidateSchema</summary>
 public void ValidateSchema()
 {
     // Snippet: ValidateSchema(string, Schema, CallSettings)
     // Create client
     SchemaServiceClient schemaServiceClient = SchemaServiceClient.Create();
     // Initialize request argument(s)
     string parent = "projects/[PROJECT]";
     Schema schema = new Schema();
     // Make the request
     ValidateSchemaResponse response = schemaServiceClient.ValidateSchema(parent, schema);
     // End snippet
 }
Пример #8
0
 /// <summary>Snippet for ValidateSchema</summary>
 public void ValidateSchemaResourceNames()
 {
     // Snippet: ValidateSchema(ProjectName, Schema, CallSettings)
     // Create client
     SchemaServiceClient schemaServiceClient = SchemaServiceClient.Create();
     // Initialize request argument(s)
     ProjectName parent = ProjectName.FromProject("[PROJECT]");
     Schema      schema = new Schema();
     // Make the request
     ValidateSchemaResponse response = schemaServiceClient.ValidateSchema(parent, schema);
     // End snippet
 }
Пример #9
0
        /// <summary>Snippet for DeleteSchemaAsync</summary>
        public async Task DeleteSchemaAsync()
        {
            // Snippet: DeleteSchemaAsync(string, CallSettings)
            // Additional: DeleteSchemaAsync(string, CancellationToken)
            // Create client
            SchemaServiceClient schemaServiceClient = await SchemaServiceClient.CreateAsync();

            // Initialize request argument(s)
            string name = "projects/[PROJECT]/schemas/[SCHEMA]";
            // Make the request
            await schemaServiceClient.DeleteSchemaAsync(name);

            // End snippet
        }
Пример #10
0
        /// <summary>Snippet for DeleteSchemaAsync</summary>
        public async Task DeleteSchemaResourceNamesAsync()
        {
            // Snippet: DeleteSchemaAsync(SchemaName, CallSettings)
            // Additional: DeleteSchemaAsync(SchemaName, CancellationToken)
            // Create client
            SchemaServiceClient schemaServiceClient = await SchemaServiceClient.CreateAsync();

            // Initialize request argument(s)
            SchemaName name = SchemaName.FromProjectSchema("[PROJECT]", "[SCHEMA]");
            // Make the request
            await schemaServiceClient.DeleteSchemaAsync(name);

            // End snippet
        }
Пример #11
0
        /// <summary>Snippet for ListSchemas</summary>
        public async Task ListSchemasRequestObjectAsync()
        {
            // Snippet: ListSchemasAsync(ListSchemasRequest, CallSettings)
            // Create client
            SchemaServiceClient schemaServiceClient = await SchemaServiceClient.CreateAsync();

            // Initialize request argument(s)
            ListSchemasRequest request = new ListSchemasRequest
            {
                ParentAsProjectName = ProjectName.FromProject("[PROJECT]"),
                View = SchemaView.Unspecified,
            };
            // Make the request
            PagedAsyncEnumerable <ListSchemasResponse, Schema> response = schemaServiceClient.ListSchemasAsync(request);

            // Iterate over all response items, lazily performing RPCs as required
            await response.ForEachAsync((Schema item) =>
            {
                // Do something with each item
                Console.WriteLine(item);
            });

            // Or iterate over pages (of server-defined size), performing one RPC per page
            await response.AsRawResponses().ForEachAsync((ListSchemasResponse page) =>
            {
                // Do something with each page of items
                Console.WriteLine("A page of results:");
                foreach (Schema item in page)
                {
                    // Do something with each item
                    Console.WriteLine(item);
                }
            });

            // Or retrieve a single page of known size (unless it's the final page), performing as many RPCs as required
            int           pageSize   = 10;
            Page <Schema> singlePage = await response.ReadPageAsync(pageSize);

            // Do something with the page of items
            Console.WriteLine($"A page of {pageSize} results (unless it's the final page):");
            foreach (Schema item in singlePage)
            {
                // Do something with each item
                Console.WriteLine(item);
            }
            // Store the pageToken, for when the next page is required.
            string nextPageToken = singlePage.NextPageToken;
            // End snippet
        }
Пример #12
0
        /// <summary>Snippet for DeleteSchema</summary>
        public void DeleteSchemaRequestObject()
        {
            // Snippet: DeleteSchema(DeleteSchemaRequest, CallSettings)
            // Create client
            SchemaServiceClient schemaServiceClient = SchemaServiceClient.Create();
            // Initialize request argument(s)
            DeleteSchemaRequest request = new DeleteSchemaRequest
            {
                SchemaName = SchemaName.FromProjectSchema("[PROJECT]", "[SCHEMA]"),
            };

            // Make the request
            schemaServiceClient.DeleteSchema(request);
            // End snippet
        }
Пример #13
0
 /// <summary>Snippet for ValidateSchema</summary>
 public void ValidateSchemaRequestObject()
 {
     // Snippet: ValidateSchema(ValidateSchemaRequest, CallSettings)
     // Create client
     SchemaServiceClient schemaServiceClient = SchemaServiceClient.Create();
     // Initialize request argument(s)
     ValidateSchemaRequest request = new ValidateSchemaRequest
     {
         ParentAsProjectName = ProjectName.FromProject("[PROJECT]"),
         Schema = new Schema(),
     };
     // Make the request
     ValidateSchemaResponse response = schemaServiceClient.ValidateSchema(request);
     // End snippet
 }
Пример #14
0
        /// <summary>Snippet for ValidateSchemaAsync</summary>
        public async Task ValidateSchemaAsync()
        {
            // Snippet: ValidateSchemaAsync(string, Schema, CallSettings)
            // Additional: ValidateSchemaAsync(string, Schema, CancellationToken)
            // Create client
            SchemaServiceClient schemaServiceClient = await SchemaServiceClient.CreateAsync();

            // Initialize request argument(s)
            string parent = "projects/[PROJECT]";
            Schema schema = new Schema();
            // Make the request
            ValidateSchemaResponse response = await schemaServiceClient.ValidateSchemaAsync(parent, schema);

            // End snippet
        }
Пример #15
0
 /// <summary>Snippet for GetSchema</summary>
 public void GetSchemaRequestObject()
 {
     // Snippet: GetSchema(GetSchemaRequest, CallSettings)
     // Create client
     SchemaServiceClient schemaServiceClient = SchemaServiceClient.Create();
     // Initialize request argument(s)
     GetSchemaRequest request = new GetSchemaRequest
     {
         SchemaName = SchemaName.FromProjectSchema("[PROJECT]", "[SCHEMA]"),
         View       = SchemaView.Unspecified,
     };
     // Make the request
     Schema response = schemaServiceClient.GetSchema(request);
     // End snippet
 }
Пример #16
0
        /// <summary>Snippet for ValidateSchemaAsync</summary>
        public async Task ValidateSchemaResourceNamesAsync()
        {
            // Snippet: ValidateSchemaAsync(ProjectName, Schema, CallSettings)
            // Additional: ValidateSchemaAsync(ProjectName, Schema, CancellationToken)
            // Create client
            SchemaServiceClient schemaServiceClient = await SchemaServiceClient.CreateAsync();

            // Initialize request argument(s)
            ProjectName parent = ProjectName.FromProject("[PROJECT]");
            Schema      schema = new Schema();
            // Make the request
            ValidateSchemaResponse response = await schemaServiceClient.ValidateSchemaAsync(parent, schema);

            // End snippet
        }
Пример #17
0
 /// <summary>Snippet for ValidateMessage</summary>
 public void ValidateMessageRequestObject()
 {
     // Snippet: ValidateMessage(ValidateMessageRequest, CallSettings)
     // Create client
     SchemaServiceClient schemaServiceClient = SchemaServiceClient.Create();
     // Initialize request argument(s)
     ValidateMessageRequest request = new ValidateMessageRequest
     {
         ParentAsProjectName = ProjectName.FromProject("[PROJECT]"),
         SchemaName          = SchemaName.FromProjectSchema("[PROJECT]", "[SCHEMA]"),
         Message             = ByteString.Empty,
         Encoding            = Encoding.Unspecified,
     };
     // Make the request
     ValidateMessageResponse response = schemaServiceClient.ValidateMessage(request);
     // End snippet
 }
Пример #18
0
        /// <summary>Snippet for DeleteSchemaAsync</summary>
        public async Task DeleteSchemaRequestObjectAsync()
        {
            // Snippet: DeleteSchemaAsync(DeleteSchemaRequest, CallSettings)
            // Additional: DeleteSchemaAsync(DeleteSchemaRequest, CancellationToken)
            // Create client
            SchemaServiceClient schemaServiceClient = await SchemaServiceClient.CreateAsync();

            // Initialize request argument(s)
            DeleteSchemaRequest request = new DeleteSchemaRequest
            {
                SchemaName = SchemaName.FromProjectSchema("[PROJECT]", "[SCHEMA]"),
            };
            // Make the request
            await schemaServiceClient.DeleteSchemaAsync(request);

            // End snippet
        }
Пример #19
0
        /// <summary>Snippet for ValidateSchemaAsync</summary>
        public async Task ValidateSchemaRequestObjectAsync()
        {
            // Snippet: ValidateSchemaAsync(ValidateSchemaRequest, CallSettings)
            // Additional: ValidateSchemaAsync(ValidateSchemaRequest, CancellationToken)
            // Create client
            SchemaServiceClient schemaServiceClient = await SchemaServiceClient.CreateAsync();

            // Initialize request argument(s)
            ValidateSchemaRequest request = new ValidateSchemaRequest
            {
                ParentAsProjectName = ProjectName.FromProject("[PROJECT]"),
                Schema = new Schema(),
            };
            // Make the request
            ValidateSchemaResponse response = await schemaServiceClient.ValidateSchemaAsync(request);

            // End snippet
        }
Пример #20
0
        /// <summary>Snippet for GetSchemaAsync</summary>
        public async Task GetSchemaRequestObjectAsync()
        {
            // Snippet: GetSchemaAsync(GetSchemaRequest, CallSettings)
            // Additional: GetSchemaAsync(GetSchemaRequest, CancellationToken)
            // Create client
            SchemaServiceClient schemaServiceClient = await SchemaServiceClient.CreateAsync();

            // Initialize request argument(s)
            GetSchemaRequest request = new GetSchemaRequest
            {
                SchemaName = SchemaName.FromProjectSchema("[PROJECT]", "[SCHEMA]"),
                View       = SchemaView.Unspecified,
            };
            // Make the request
            Schema response = await schemaServiceClient.GetSchemaAsync(request);

            // End snippet
        }
Пример #21
0
        /// <summary>Snippet for ListSchemas</summary>
        public void ListSchemasResourceNames()
        {
            // Snippet: ListSchemas(ProjectName, string, int?, CallSettings)
            // Create client
            SchemaServiceClient schemaServiceClient = SchemaServiceClient.Create();
            // Initialize request argument(s)
            ProjectName parent = ProjectName.FromProject("[PROJECT]");
            // Make the request
            PagedEnumerable <ListSchemasResponse, Schema> response = schemaServiceClient.ListSchemas(parent);

            // Iterate over all response items, lazily performing RPCs as required
            foreach (Schema item in response)
            {
                // Do something with each item
                Console.WriteLine(item);
            }

            // Or iterate over pages (of server-defined size), performing one RPC per page
            foreach (ListSchemasResponse page in response.AsRawResponses())
            {
                // Do something with each page of items
                Console.WriteLine("A page of results:");
                foreach (Schema item in page)
                {
                    // Do something with each item
                    Console.WriteLine(item);
                }
            }

            // Or retrieve a single page of known size (unless it's the final page), performing as many RPCs as required
            int           pageSize   = 10;
            Page <Schema> singlePage = response.ReadPage(pageSize);

            // Do something with the page of items
            Console.WriteLine($"A page of {pageSize} results (unless it's the final page):");
            foreach (Schema item in singlePage)
            {
                // Do something with each item
                Console.WriteLine(item);
            }
            // Store the pageToken, for when the next page is required.
            string nextPageToken = singlePage.NextPageToken;
            // End snippet
        }
Пример #22
0
        /// <summary>Snippet for ValidateMessageAsync</summary>
        public async Task ValidateMessageRequestObjectAsync()
        {
            // Snippet: ValidateMessageAsync(ValidateMessageRequest, CallSettings)
            // Additional: ValidateMessageAsync(ValidateMessageRequest, CancellationToken)
            // Create client
            SchemaServiceClient schemaServiceClient = await SchemaServiceClient.CreateAsync();

            // Initialize request argument(s)
            ValidateMessageRequest request = new ValidateMessageRequest
            {
                ParentAsProjectName = ProjectName.FromProject("[PROJECT]"),
                SchemaName          = SchemaName.FromProjectSchema("[PROJECT]", "[SCHEMA]"),
                Message             = ByteString.Empty,
                Encoding            = Encoding.Unspecified,
            };
            // Make the request
            ValidateMessageResponse response = await schemaServiceClient.ValidateMessageAsync(request);

            // End snippet
        }
Пример #23
0
        public static void Synchronize()
        {
            ConnectionProperties
                remote = new ConnectionProperties(Settings.Default.RemoteServer, Settings.Default.RemoteDb),
                local = new ConnectionProperties(Settings.Default.LocalServer, Settings.Default.LocalDb);

            using (var service = new SchemaServiceClient()) {

                //check for schema update
                byte version = service.GetSchemaVersion();
                if (version > Settings.Default.SchemaVersion) {
                    //synchronise schema
                    StructureProject structure = new StructureProject(remote, local);

                    structure.ComparisonOptions.IgnoreIdentitySeedAndIncrement = true;
                    structure.MappedObjects.ExcludeAllFromComparison();

                    //select the desired tables and triggers
                    var tables = Settings.Default.Tables.Split(',');
                    structure.MappedTables.IncludeInComparison(tables.Select(t => '^' + t + '$').ToArray());
                    structure.MappedTriggers.IncludeInComparison(tables.Select(t => '^' + t + "_TimeStamp$").ToArray());

                    structure.ComparedObjects.IncludeAllInSynchronization();
                    Synchronise(structure);

                    Settings.Default.SchemaVersion = version;
                    Settings.Default.Save();
                }

                //server to client
                DataProject down = new DataProject(remote, local);
            #pragma warning disable 612
                down.ComparisonOptions.CompareAdditionalRows = false;
            #pragma warning restore 612

                //only retrieve rows which have changed since last sync
                string timestamp = "TimeStamp > '" + Settings.Default.LastSync.ToString("yyyy-MM-dd HH:mm:ss") + "'";
                foreach (MappedDataObject<Table> table in down.MappedTables) {
                    table.WhereClause.SourceFilter = timestamp;
                    table.WhereClause.UseSourceFilterForDestination = false;
                }

                down.ComparedObjects.IncludeAllInSynchronization();
                Synchronise(down);

                //client to server
                DataProject up = new DataProject(local, remote);
            #pragma warning disable 612
                up.ComparisonOptions.CompareAdditionalRows = false;
            #pragma warning restore 612

                using (var con = new SqlConnection(Settings.Default.Local))
                    foreach (MappedDataObject<Table> table in up.MappedTables) {
                        StringBuilder ids = new StringBuilder("ID in (");
                        using (var com = new SqlCommand("select ID from " + table.SourceName + " where " + timestamp, con)) {
                            con.Open();
                            using (var results = com.ExecuteReader(CommandBehavior.CloseConnection))
                                while (results.Read())
                                    ids.Append(results[0]).Append(',');
                        }
                        if (ids[ids.Length - 1] == '(')
                            table.IncludeInComparison = false;
                        else
                            table.WhereClause.SourceFilter = ids.Remove(ids.Length - 1, 1).Append(')').ToString();
                    }

                try {
                    up.ComparedObjects.IncludeAllInSynchronization();
                    Synchronise(up);
                }
                catch (NoSelectedForOperationObjectsException) { }

                Settings.Default.LastSync = DateTime.Now;
                Settings.Default.Save();

                //do we have an ID range?
                if (Settings.Default.MaxID == 0) {
                    var range = service.GetIdRange(Environment.MachineName);
                    Settings.Default.MinID = range.Min;
                    Settings.Default.MaxID = range.Max;
                    Settings.Default.Save();
                }
            }

            //reseed the client
            using (var db = new SqlConnection(Settings.Default.Local))
                Repository.Repository.Reseed(Settings.Default.MinID, Settings.Default.MaxID, db);
        }
Пример #24
0
        public static void Synchronize()
        {
            ConnectionProperties
                remote = new ConnectionProperties(Settings.Default.RemoteServer, Settings.Default.RemoteDb),
                local  = new ConnectionProperties(Settings.Default.LocalServer, Settings.Default.LocalDb);

            using (var service = new SchemaServiceClient()) {
                //check for schema update
                byte version = service.GetSchemaVersion();
                if (version > Settings.Default.SchemaVersion)
                {
                    //synchronise schema
                    StructureProject structure = new StructureProject(remote, local);

                    structure.ComparisonOptions.IgnoreIdentitySeedAndIncrement = true;
                    structure.MappedObjects.ExcludeAllFromComparison();

                    //select the desired tables and triggers
                    var tables = Settings.Default.Tables.Split(',');
                    structure.MappedTables.IncludeInComparison(tables.Select(t => '^' + t + '$').ToArray());
                    structure.MappedTriggers.IncludeInComparison(tables.Select(t => '^' + t + "_TimeStamp$").ToArray());

                    structure.ComparedObjects.IncludeAllInSynchronization();
                    Synchronise(structure);

                    Settings.Default.SchemaVersion = version;
                    Settings.Default.Save();
                }

                //server to client
                DataProject down = new DataProject(remote, local);
#pragma warning disable 612
                down.ComparisonOptions.CompareAdditionalRows = false;
#pragma warning restore 612

                //only retrieve rows which have changed since last sync
                string timestamp = "TimeStamp > '" + Settings.Default.LastSync.ToString("yyyy-MM-dd HH:mm:ss") + "'";
                foreach (MappedDataObject <Table> table in down.MappedTables)
                {
                    table.WhereClause.SourceFilter = timestamp;
                    table.WhereClause.UseSourceFilterForDestination = false;
                }

                down.ComparedObjects.IncludeAllInSynchronization();
                Synchronise(down);

                //client to server
                DataProject up = new DataProject(local, remote);
#pragma warning disable 612
                up.ComparisonOptions.CompareAdditionalRows = false;
#pragma warning restore 612

                using (var con = new SqlConnection(Settings.Default.Local))
                    foreach (MappedDataObject <Table> table in up.MappedTables)
                    {
                        StringBuilder ids = new StringBuilder("ID in (");
                        using (var com = new SqlCommand("select ID from " + table.SourceName + " where " + timestamp, con)) {
                            con.Open();
                            using (var results = com.ExecuteReader(CommandBehavior.CloseConnection))
                                while (results.Read())
                                {
                                    ids.Append(results[0]).Append(',');
                                }
                        }
                        if (ids[ids.Length - 1] == '(')
                        {
                            table.IncludeInComparison = false;
                        }
                        else
                        {
                            table.WhereClause.SourceFilter = ids.Remove(ids.Length - 1, 1).Append(')').ToString();
                        }
                    }

                try {
                    up.ComparedObjects.IncludeAllInSynchronization();
                    Synchronise(up);
                }
                catch (NoSelectedForOperationObjectsException) { }

                Settings.Default.LastSync = DateTime.Now;
                Settings.Default.Save();

                //do we have an ID range?
                if (Settings.Default.MaxID == 0)
                {
                    var range = service.GetIdRange(Environment.MachineName);
                    Settings.Default.MinID = range.Min;
                    Settings.Default.MaxID = range.Max;
                    Settings.Default.Save();
                }
            }

            //reseed the client
            using (var db = new SqlConnection(Settings.Default.Local))
                Repository.Repository.Reseed(Settings.Default.MinID, Settings.Default.MaxID, db);
        }