Deploy the data models to the database¶
In this section you will find you to implement the data models in the database.
The database objects for the technical staging, logical validation, and central facts layer are installed through scripts (DDL). The generic data access layer objects (views, procedures, triggers) will be generated during metadata import.
Warning
When deploying changes, make sure there are no deliveries (dataloads, or CRUD actions). This might lead to changes on the database that cannot be reverted. It's a good practice to enddate the delivery agreement before running a script.
Full deployment¶
When you generate the DDL and metadata, this will create a full deployment script.
Full deployment through windows explorer¶
Edit the config\config.bat file to represent your settings
- Change the database names
- Set the connection string
- -E connect to the default instance
- -S HOSTNAME\INSTANCENAME connects to a named instance
- Set the connection string
Run the install.ps1 file by right clicking on the file and choosing run with powershell. In the window that opens one can see the progress and errors. The path is automatically determined by the folder the install.ps1 resided in.
In the folder log you will find the log file of the installation.
Full deployment with SQL Server Management Studio¶
Open the install.sql file in SQL Server Management Studio.
- Change the folder variables:
:setvar path "<path_name>":<path_name>should be the folder in which theinstall.sqlresides in.:setvar generated_path "<path_name>":<path_name>should be the folder in which the generated scripts are in.- Change the database variables according to your database.
- Enable SQLCMD mode (
Menu > Query > SQLCMD mode). - Run the script in the
masterdatabase.
Delta deployment¶
When a model is updated, the database should change accordingly. In most cases, the database objects already exist and the database contains data. In this case, you need to create scripts to upgrade the existing database to match to the new model release. In some cases you also need to migrate data. How to do this is customer specific. In this section we will give a general advise on how to proceed.
Compare the current model and the new model¶
Some layers can be deleted and rebuilt, while other layers need an upgrade script. These are the rules for each layer:
- Technical staging layer (TSL):
- The entities can be deleted and rebuilt with the generated script. The data in this layer is temporary; it is deleted before a new delivery.
- In case of small changes a delta deployment is advisable. Pay attention to the order of the columns if the loading software is based on column order instead of column names.
- Logical validation layer (LVL):
- In case of full loads: entities can be deleted and rebuilt.
- In case of delta loads or context based loads: one has to take the current record into account. This is needed for delete detection.
- Scenario 1: Delete and rebuilt: when doing this the first load will not be able to detect deletes.
- Scenario 2: Create a delta deployment that keeps the current records and also migrates the data accordingly.
- In case of small changes a delta deployment is advisable.
- Central facts layer (CFPL):
- The CFPL should never be deleted. The entities should always be upgraded to correspond to the latest model version.
- Generic data access layer (GDAL):
- A good practice for the GDAL is to delete all entities (current views, historical views, triggers, procedures, etc.) and have them regenerated when importing the metadata. This ensures that the GDAL always represents the current metadata model.
Info
When importing metadata, deleted objects are not automatically removed from the database.
To create a delta script:
- Determine the delta between the current model and the new model. At least the following items should be compared:
- TSTGIN
- Table & View definitions
- HSTGIN
- Table & View definitions
- Keys
- Constraints
- Indexes
- CFPL
- Table & View definitions
- Keys, Alternate Keys, Foreign Keys
- Constraints
- Indexes
- Extended properties
- Create a delta deployment script for each updated layer.
- In this script new objects are created, objects are changed, or even deleted.
- A good practice is to implement the current and new model in different databases in a test environment and then compare them. For example, you have database A and B that correspond to the current model version. Remove the database objects from database A that match your model. Then run a full install script of the new model release on database A. Finally, compare database A with database B to generate a delta script.
Warning
Be aware that the database model should be upgraded before the metadata is imported. This is because during metadata import, there are checks on the database. If a table or attribute doesn't exists in the database, it will throw an error.
Data migration¶
In some cases, you also need to migrate data. For example, if you create new objects in the central facts layer and you need the history of an existing object in that new object.
When migrating data it is important to keep the following in mind:
- When initially populating data for new tables / columns you have to ask the question if the data should be initialized historically or from a certain point in time.
- When merging objects: Keep in mind that the timelines also need to be merged. (A nice article on packing timelines can be found here: http://sqlmag.com/blog/solutions-packing-date-and-time-intervals-puzzle, for a Point in Time query, where one merges multiple timelines one can basically take our hist views as an example.)
- A rule of thumb is that no data should be removed from the CFPL.
- Backup the i-refactory database and the data in the to be migrated object.
- Deploy the delta scripts and the migration scripts on the database according to the client specifications.
Warning
Be aware that when dropping and recreating objects, the security settings for this object can be impacted. One should always redeploy the security settings after DDL changes.
Example of a Delta Deployment and Data Migration¶
Suppose we would received a requirement for adding a property to one of the entities and the data delivered onwards will be enriched going forward.
This is a minor change and therefore a partial deployment should be considered.
- What we need to do is to change the TSTGIN model adding the property
- We need to do the same with the HSTGIN model and map the new column from TSTGIN to HSTGIN
- Do the same for the CFPL and the GDAL and make sure that the new column is mapped across all 4 layers
After these changes take place on the models, the next step is to migrate the data into temporary tables and recreate the objects that are being affected and repopulate with data while deciding what the value would be for the newly added property
The cleanest way should be to deploy the newly created database objects to a new database and then do a comparison between the databases to determine what are the changes, after that we can migrate the data from the old db definitions to the new. It is common to use various tools for these procedures for that such as Redgate. Alternatively the data migration that need to take place might look like this:
SELECT {[old column1], [old column2]...}
,NULL as [new column] --Or any other default value for the records that are currently in the Database
INTO [...].[...].[..._migrate]
FROM [...].[...].[...]
The old database object can be dropped and the new definition should be executed
/* There might be a need to disable Identity_insert */
SET IDENTITY_INSERT [...].[...].[...] ON;
INSERT INTO [...].[...].[...] ({[old columns1], [old columns2]...}
, [new column])
SELECT {[old columns1], [old columns2]...}
,[new column]
FROM [...].[...].[..._migrate]
/* There might be a need to re-enable Identity_insert */
SET IDENTITY_INSERT [...].[...].[...] OFF;
Lastly we need to import the new metadata into i-refactory so that constraints, views as well as the GDAL layer will also updated.