Introduction
Administrator class is the main façade for making modifications to the database schema: creating tables, relationships and indexes, or adding some special data, like Enum values or the TypeDN table.
Very often this kind of test can not be executed straight to the database, and is preferable to generate a script, supervise it and send it to production as a *.sql file for example. That's why a lot of methods in Administrator class generate
SqlPreCommand. It's up to you to
execute them.
Finally, Administrator class also has some method overloads to generate multi-column indexes (not available though
field attributes and have to be created manually).
Warning: This class is meant to be used in a Dev/Test environment,
not Production, that's why it's separated from
Database class.
Erasing the Schema
There are some scripts to remove everything in the schema. Useful before generating the schema. Obviously, you will
loose data.
- RemoveAllViewsScript: SqlPreCommand to remove all views in the schema.
- RemoveAllConstraintsScript: SqlPreCommand to remove all constraints (FKs, Indexes...) in the schema.
- RemoveAllTablesScript: SqlPreCommand to remove all tables in the schema.
- RemoveAllScript(): Concatenation of the three above to remove almost everyting in the right order.
Creating the Schema
Scripts that, based on the current
Schema, create the tables, relationships, indices... to be able to use Signum Engine with your entities.
- CreateTablesScript(): Create a big script with all your entities Tables, Relational Tables, Foreign Keys and Indices. The order of the columns depend on the order they are placed in your source code.
- InsertEnumValuesScript(): Create a script with all the necessary inserts to fill up the tables that correspond to enums defined in your entity model.
- InsertTypesScript(): Create a script with all the necessary inserts to fill up the tlTableDN table, necessary if you have any ImplementedByAll relationship in your schema.
There's also a useful method, NewDatabaseBasic() that is enough to clean and re-create the database in a lot of scenarios. It doesn't generate the script, instead it executes all the previous ones. It's defined like this:
public static void NewDatabaseBasic()
{
RemoveAllScript().ExecuteLeaves();
CreateTablesScript().ExecuteLeaves();
InsertEnumValuesScript().ToSimple().ExecuteNonQuery();
if (Schema.Current.Tables.ContainsKey(typeof(TypeDN)))
{
InsertTypesScript().ToSimple().ExecuteNonQuery();
Schema.Current.CacheTypeTable();
}
}
Notice that, if TypeDN is included in the Schema, after inserting TypeDN entities we call
CacheTypeTable to update the Type Id's (necessary for ImplementedByAll associations). See more in
Connection.
Synchronizing the Schema
Synchronize is the process of creating the script that makes your current schema (no matter how different they are), the Schema that should be fully compatible with what Signum Engine needs to deal with your entities.
The synchronization script will create and remove tables, FKs, indexes, columns... also it will insert and remove TypeDN and Enum values from the correct tables.
This are the methods available to do synchronizing:
- SynchronizeSchemaScript(): Generate the script to create and remove tables, relational tables, FKs, indexes and columns.
- SynchronizeTypesScript(): Generate the script to insert, delete or update TypeDN entities in tlTypeDN table.
- SynchronizeEnumsScript(): Generate the script to insert, delete or update Enum values in all the enum tables in the schema.
- SynchronizeAllScript(): Concatenates the tree above, doing all the necessary synchronizations to transform your database schema according to your Schema data structure.
Warning: The algorithm used by the synchronization goes from your current schema to the right one doing as fewer modifications as possible. It has has no compassion for your current data. Take a look at the syncronization script before executing it.