Telerik OpenAccess ORM

Telerik OpenAccess ORM Send comments on this topic.
Schema Generation
Programmers > Development Environment > Command Line Tools > Schema Generation

Glossary Item Box

A database schema is simply a repository that contains information about the structure and content of the database. It has to be generated before you can store any data.

The VSchema Tool

OpenAccess ORM provides you with a schema generation tool named VSchema. The VSchema tool is used for creating or migrating a relational schema. During the schema creation phase, VSchema reads the assembly file and uses the information it contains to determine which classes have related tables. The information as to how the schema is to be built is taken from the OpenAccess ORM attributes in the assembly (Persistent, ItemType, etc.) and from the application's config file (App.config), which contains the complete mapping definition.

VSchema needs an existing database, for creating or migrating a relational schema.

To generate the database schema, you first compile your .NET source files as you normally would do for any application and enhance the assembly using VEnhance (refer to The OpenAccess ORM Enhancer for more information). The next step is to call the VSchema tool in order to generate the schema files from the assembly files that you have compiled. The following shows a typical migration command line call to VSchema:

Copy Code
VSchema -assembly:AssemblyFileName -migrate

Command Line Switches for VSchema

Here is the command line syntax for the VSchema tool:

Copy Code
VSchema [options]

General options:

Copy Code
-assembly:filename
-create
-migrate
-executeSql
-databaseCreate
-help
-reference:filelist

Schema options:

Copy Code
-config:filename
-connectionId:connectionId
-outputDir:directory_name
-direct[+|-]

Options for '-create':

Copy Code
-dropTables[+|-]
-validate[+|-]

Options for '-migrate':

Copy Code
-checkConstraint[+|-]
-checkExtraColumns[+|-]
-checkIndex[+|-]
-checkLength[+|-]
-checkNulls[+|-]
-checkPK[+|-]
-checkScale[+|-]
-checkType[+|-]

Options for '-databaseCreate':

Copy Code
-checkOnly:[+|-]

Options for '-executeSql':

Copy Code
-SQLScriptFile:filename

The switches are described below:

General options

-assembly
 

Required. ITaskItem[] parameter.

Specifies the assembly that contains the persistent classes. This switch is mandatory.

-create
 

Boolean parameter.

Creates a new schema for your model, i.e., the schema of the database is rebuilt completely. This implies that the content of the database could be potentially removed.

-migrate
 

Boolean parameter.

Updates the database schema to match the current model and mappings.

-executeSql
 

Boolean parameter.

Executes a referenced SQL statement script file against an existing database. This is useful when the script contains modifications that need to be applied to the database.

-databaseCreate
 

Boolean parameter.

Generates a new, empty relational database.

-help
 

Optional. Boolean parameter.

Prints the usage information to the console.

-reference
 

Required. ITaskItem[] parameter.

A comma separated list of assemblies that are referenced by the assembly with persistent classes. Only assemblies that cannot be found in the global assembly cache must be specified with this switch. Usually assemblies that are passed to the C# compiler with the /reference switch must also be passed to the VSchema tool.

Schema Options

-config
 

Required. String parameter.

Specifies the application configuration filename (App.config), which contains all the configuration information, such as the database connection settings etc (-config:App.config).

Refer to Configuration File for more information about the configuration file settings.

-connectionId
 

Required. String parameter.

Specifies the logical name, which is used to create the database connection and to obtain the object-relational mapping information. This name refers to an entry (for e.g., <connection id="MssqlConnection1">), which is specified in the in the application configuration file. Therefore, for the above example the connectionId will appear as -connectionId:MssqlConnection1.

-outputDir
 

Optional. String parameter.

Specifies the name of the output directory, which stores the generated SQL script file. For e.g -outputDir:sqlscripts will create a directory name "sqlscripts", within which the sql script files will be generated.

-direct[+|-]
 

Optional. Boolean parameter.

If set to True, then the generated migration script is run directly against the database. Defaults to false. It is necessary to use this option if you are using the -droptables option. The reason for this, is that you cannot drop tables if you are not working directly with the database (i.e. you cannot drop tables if you are just creating a script).

To maintain the database schema either -create or -migrate must be selected.

Schema Creation

Use the -create switch to create and/or validate the database tables for your model. The VSchema tool will create an SQL script and also optionally drop and optionally recreate all the tables in each datastore, using the connection properties specified in your config file. This will destroy any data in those tables.

Parameters for schema creation:

-droptables[+|-]
 

Optional. Boolean parameter.

If set to True, then all tables with the same names (case insensitive) as new tables in the schema will be dropped, before creation. Defaults to false. If this option is used, it is necessary to use it with the -direct option. The reason for this, is that you cannot drop tables if you are not working directly with the database (i.e. you cannot drop tables if you are just creating a script).

-validate[+|-]
 

Optional. Boolean parameter.

If set to True, then it is checked whether all the tables and columns, which are specified in the schema exist, i.e., additional (slow) checks are made to validate consistency. Defaults to false.

Schema Migration

-checkConstraints[+|-]
 

Optional. Boolean parameter.

If set to True, then the database constraints are checked. Defaults to true.

-checkExtraColumns[+|-]
 

Optional. Boolean parameter.

If set to True, then all the existing columns are checked, to see if they are necessary for the new schema, and if any extra columns are found, they will be dropped. Due to this, there is a possibility of loss of data while migrating. Defaults to true.

-checkIndex[+|-]
 

Optional. Boolean parameter.

If set to True, then the database indexes are checked. Defaults to true.

-checkLength[+|-]
 

Optional. Boolean parameter.

If set to True, then column lengths are checked. Defaults to true.

-checkNulls[+|-]
 

Optional. Boolean parameter.

If set to True, then the column's null or not null status is checked. Defaults to true.

-checkPK[+|-]
 

Optional. Boolean parameter.

If set to True, then the tables primary keys are checked. Defaults to true.

-checkScale[+|-]
 

Optional. Boolean parameter.

If set to True, then the column scales are checked. Defaults to true.

-checkType[+|-]
 

Optional. Boolean parameter.

If set to True, then the column data types are checked. Defaults to true.

Executing SQL Scripts

Executes a referenced SQL statement script file against an existing database, whose schema in an old state; and the script contains the necessary modifications to that schema like altering/creating/deleting a table or modifying columns.

This can to be useful in case of deployment scenarios, where the user wants some special modifications made to the database on the end customers site using a modified script.

-SQLScriptFile
 

Required. String parameter.

Specifies the SQL Script File that needs to be run against the database.

Database Creation

This option generates a new database, when it is used along with -direct:+ option. The -databaseCreate generates a sql script for creating a database and the -direct:+ option runs the generated script and creates the database. It is necessary to specify the configuration file (-config) and the connectionid (-connectionid) options along with the -databaseCreate option as shown below:

Copy Code
VSchema -assembly:MyApp.exe
-databaseCreate -direct:+
-config:App.config -connectionId:MssqlConnection1

The above command will generate a database with the name specified in the App.config file (refer to Configuration File for more information). A sql script file using the default naming convention, i.e., Database_Creation_Backend_DateTime (for e.g. Database_Creation_mssql_2006-08-30_15-24-28.sql) will also be generated in the runtime directory of your OpenAccess ORM installation. This file contains the SQL DDL command: CREATE DATABASE DatabaseName.

-checkOnly
 

Optional. Boolean input/output parameter.

Checks the existence of a database and can be used, for example, in shell scripts to test for existence. Defaults to false.

If the -checkOnly:+ option is used, the database does not created if it does not exists. Only a message indicating the same is printed.

However, if the -checkOnly:- option is used, a sql script file using the default naming convention, i.e., Database_Creation_Backend_DateTime will be generated in the runtime directory of your OpenAccess ORM installation. This file contains the SQL DDL command: CREATE DATABASE DatabaseName. In case the database exists, a message indicating the same is printed.

Usage Examples

Assume, that some persistence capable classes are located in the assembly MyApp.exe, or are reachable from there.

The following call of VSchema can be used to create a new database schema for all persistence capable classes:

Copy Code
VSchema -assembly:MyApp.exe
-create -direct:+ -outputDir:sqlscripts -dropTables
-config:App.config -connectionId:MssqlConnection1

Existing tables in the database will be dropped and new tables will be created. Since, the -droptables option has been used, in this case it is necessary that -direct option also must be used, as shown in the example above. The reason for this, is that you cannot drop tables if you are not working directly with the database (i.e. you cannot drop tables if you are just creating a script).

Additionally, the SQL script file, with its default naming convention of Schema_Action_Backend_DateTime, for e.g. (Schema_Creation_mssql_2006-04-05_15-05-05.sql), will be created in the "sqlscripts" directory. The sql script file, contains DDL statements, which can be used for table creation. The connection settings are taken from the application configuration file, App.config, which must contain a connection element, whose "id" is equal to MssqlConnection1.

Dropping all tables is not desirable if there is already data stored for an existing schema. The following call can be used to migrate an existing schema:

Copy Code
VSchema -assembly:MyApp.exe
-migrate
-config:App.config -connectionId:MssqlConnection1

This call of VSchema performs all checks on the schema and migrates the existing schema for the database specified in the application configuration file App.config as MssqlConnection1. Any alterations in the schema are written to a SQL script file, containing the SQL DDL commands. The script file in this case will be named using the default naming convention, i.e. Schema_Action_Backend_DateTime (for e.g., Schema_Migration_mssql_2006-04-05_15-05-05.sql).

The create or update sql script file is created in the assembly output directory (bin\Debug) of your project, during Building / Rebuilding your project, if the update database property is set to "True". The script file name will start with Schema_Creation or Schema_Migration followed by the backend and the timestamp (for e.g., Schema_Migration_mssql_2006-04-10_12-41-39.sql).

If it is the first time that you are Building / Rebuilding your project, with the database property set to "True", then another .sql file using the default naming convention, i.e, Database_Creation_Backend_DateTime (for e.g., Database_Creation_mssql_2006-05-31_16-45-46.sql) will also be generated, which will contain the SQL DDL command: CREATE DATABASE DatabaseName.