Customizing Schema Generation
INFO
The Marten team's advice is to keep your Postgresql security usage very simple to reduce friction, but if you can't follow that advice, Marten has some facility to customize the generated DDL for database security rights.
Table Creation Style
By default, Marten generates the raw schema export from IMartenStorage.ToDatabaseScript() (available via store.Storage.ToDatabaseScript()) by writing a CREATE TABLE IF NOT EXISTS statement. If desired, you can direct Marten to do this generation by first writing a DROP TABLE statement then a CREATE TABLE statement. That usage is shown below:
var store = DocumentStore.For(_ =>
{
// This is the default
_.Advanced.Migrator.TableCreation = CreationStyle.CreateIfNotExists;
// or, to drop and recreate the table on each schema migration
_.Advanced.Migrator.TableCreation = CreationStyle.DropThenCreate;
});Database Role
If you want the DDL scripts generated by Marten to run under a specific Postgresql ROLE, you can configure that like so:
var store = DocumentStore.For(_ =>
{
_.Advanced.Migrator.Role = "ROLE1";
});Doing so will wrap any DDL scripts generated or exported by Marten with this pattern:
SET ROLE [ROLE_NAME];
-- the body of the DDL
RESET ROLE;Template Files for GRANT's
One of the primary goals of Marten has been to drastically reduce the mechanical work necessary to apply database schema changes as your system evolved. To that end, we've strived to make the DDL generation and patch generation be as complete as possible. However, if your database administrators require any kind of customized security on the document storage tables, you can optionally use the "template file" feature shown in this section to add GRANT's or other permissions to the document tables generated by Marten.
To create a DDL template for the document storage tables, name your file with the pattern [template name].table that would look like the following:
ALTER TABLE %SCHEMA%.%TABLENAME% OWNER TO "SchemaOwners";
GRANT SELECT (%COLUMNS%) ON TABLE %SCHEMA%.%TABLENAME% TO "ServiceAccountRole", "SchemaOwners";
GRANT INSERT (%COLUMNS%) ON TABLE %SCHEMA%.%TABLENAME% TO "SchemaOwners";
GRANT UPDATE (%NON_ID_COLUMNS%) ON TABLE %SCHEMA%.%TABLENAME% TO "SchemaOwners";
GRANT DELETE ON %SCHEMA%.%TABLENAME% TO "ServiceAccountRole";INFO
Marten writes documents with plain SQL statements against these tables, and has not generated a function per document type since Marten 9. A [template name].function file is still read, but Marten applies no function templates to the objects it generates, so put everything a document type needs into its .table template.
As you probably surmised, the "%SOMETHING%" strings are the inputs coming from Marten itself (it's just a crude string replacement behind the scenes). The available substitutions are:
- %SCHEMA% - the database schema holding the table
- %TABLENAME% - the name of the table
- %COLUMNS% - comma delimited list of all the columns in the table
- %NON_ID_COLUMNS% - comma delimited list of all the columns except for "id" in the table
- %METADATA_COLUMNS% - comma delimited list of all the Marten metadata columns
To apply the templating, first put all your *.table files in a single directory or the base directory of your application and use code like that shown below in your IDocumentStore initialization:
var options = new StoreOptions();
options.Connection("some connection string");
// let's say that you have template files in a
// "templates" directory under the root of your
// application. Reading them is asynchronous, so
// read them before you build the store
await options.Advanced.Migrator.ReadTemplatesAsync("templates");
var store = new DocumentStore(options);Do note that ReadTemplatesAsync() only does a shallow search through the given directory and does not consider child directories.
To establish the default table DDL template, name your file "default.table". If Marten finds this file, it will automatically apply that to all document types as the default.
To overwrite the default template on a document by document type basis, you have a couple options. You can opt to use the fluent interface configuration:
var store = DocumentStore.For(_ =>
{
_.Schema.For<User>().DdlTemplate("readonly");
});You can also decorate document types with the [DdlTemplate("name")] attribute shown below:
[DdlTemplate("readonly")]
public class AuditRecord
{
public Guid Id { get; set; }
}And lastly, a document type can configure its own mapping in a public static ConfigureMarten(DocumentMapping) method, which Marten calls when it builds the mapping for that type:
public class LegacyInvoice
{
public Guid Id { get; set; }
// Marten calls a public static ConfigureMarten(DocumentMapping) method
// on a document type when it builds that type's mapping
public static void ConfigureMarten(DocumentMapping mapping)
{
mapping.DdlTemplate = "readonly";
}
}
