Quantcast
Channel: Microsoft Dynamics AX
Viewing all articles
Browse latest Browse all 130240

Blog Post: Remove old modelstore from an upgraded AX2012 R2 database

$
0
0
One of the benefits of AX2012 R2 compared to AX2012 RTM is the fact that they decided to split the database in two parts, one part for the business data and one part for the application data. This makes it a lot easier to copy business data from production to test without overwriting the application data in test. Sure, there were ways around this in RTM, but you can't deny it is a lot easier to do this now. Another benefit is that you no longer have the application (modelstore) residing in the business database. This means you don't need to have a full backup with point-in-time recovery for the production application. The application in RTM would be aroud 2.5 GB exported, but inside the database you would have huge indexes and also the compiled assemblies stored. All of these data are now stored in the application database in R2. I would argue that there is no need to have the recovery mode set to "Full" on these databases containing the application. I mean, each time you import a modelstore, have a look at the database log. It will grow huge.

Inspired by a response from Kevin Kidder at the Community Site, I decided to try make a Stored Procedure that removes the modelstore elements inside the database. Getting rid of that old modelstore from RTM that now sits in the business database after successfully upgrading to R2 will reduce the size of the database with a couple of GB, so why not.

I found this nice script to remove all schema elements, and used is as a base for my script. I also initialized a new modelstore for the purpose of testing my script. Actually, the SQL scripts being used for creating a modelstore is available as resources in the AxUtilLib assembly. You can study them by using any tool that can revese engineer a .Net dll.

This procedure might not be flawless, so use it with care. Obviosuly; take a backup and test. By default the procedure will print out the SQL statements you need to run in order to remove all the modelstore elements in the database. You will have to explicitly pass "w" for it to execute the statements for you. You will have to decide if you want to evalute the output and run it manually, or let the procedure do it all.

Please report feedback and/or errors back to me. :-)

Here it is:


CREATE PROCEDURE [dbo].[CleanUpModelStore]
(
@WorkTest char(1) = 't' -- use 'w' to work and 't' to print
)
AS
/*-----------------------------------------------------------------------------------------

Author : Tommy Skaue
Date: 06.04.2013

Description: Drops all modelstore elements.
Usage and License: Free and at own risk
Parameter: t = test/print out
w = work and execute statements

Report any errors or feedback to tommy@skaue.com.

Inspired by the script made by Ranjith Kumar S
http://ranjithk.com/2010/01/31/script-to-drop-all-objects-of-a-schema/

-------------------------------------------------------------------------------------------*/
BEGIN

declare @SQL varchar(5000)
declare @ModelStoreElementQuery varchar(2500)
declare @msg varchar(500)
declare @schemaname varchar (50) = 'dbo'

IF OBJECT_ID('tempdb..#dropmodelstore') IS NOT NULL DROP TABLE #dropmodelstore
CREATE TABLE #dropmodelstore
(
ID int identity(1,1)
,SQLstatement varchar(5000)
)

-- Common filter for ModelElements
SELECT @ModelStoreElementQuery =
'and (t.name like ''Model%'' or
t.name in (
''axIdAllocsTable'',
''AxIdAsk'',
''existingPaths'',
''Sources'',
''newPaths'',
''SourceMerge'',
''SYSXPPASSEMBLY'',
''ElementTypes'',
''SchemaVersion'',
''Layer'',
''LayerVersioning'',
''GlobalFieldIdPool''))'

-- removes all the foreign keys that reference a PK in the target schema
SELECT @SQL =
'select
''ALTER TABLE ''+SCHEMA_NAME(fk.schema_id)+''.''+OBJECT_NAME(fk.parent_object_id)+'' DROP CONSTRAINT ''+ fk.name
FROM sys.foreign_keys fk
join sys.tables t on t.object_id = fk.referenced_object_id
where t.schema_id = schema_id(''' + @schemaname + ''')
and fk.schema_id <> t.schema_id
' + @ModelStoreElementQuery + '
order by fk.name desc'

--IF @WorkTest = 't' PRINT (@SQL )
INSERT INTO #dropmodelstore
EXEC (@SQL)

-- drop all default constraints, check constraints and Foreign Keys
SELECT @SQL =
'SELECT
''ALTER TABLE ''+schema_name(t.schema_id)+''.''+OBJECT_NAME(fk.parent_object_id)+'' DROP CONSTRAINT ''+ fk.[Name]
FROM sys.objects fk
join sys.tables t on t.object_id = fk.parent_object_id
where t.schema_id = schema_id(''' + @schemaname + ''')
and fk.type IN (''D'', ''C'', ''F'')' + @ModelStoreElementQuery

--IF @WorkTest = 't' PRINT (@SQL )
INSERT INTO #dropmodelstore
EXEC (@SQL)

-- Common filter for ModelElements
SELECT @ModelStoreElementQuery =
'SELECT object_id FROM SYS.OBJECTS SO2 WHERE
SO2.parent_object_id IN
(
SELECT SO3.object_id FROM SYS.OBJECTS SO3 WHERE SO3.type IN (''U'') and
(
SO3.name like ''Model%'' or
SO3.name in (
''axIdAllocsTable'',
''AxIdAsk'',
''existingPaths'',
''Sources'',
''newPaths'',
''SourceMerge'',
''SYSXPPASSEMBLY'',
''ElementTypes'',
''SchemaVersion'',
''Layer'',
''LayerVersioning'',
''GlobalFieldIdPool'')
)
)
OR
(
SO2.type IN (''U'') and
(
SO2.name like ''Model%'' or
SO2.name in (
''axIdAllocsTable'',
''AxIdAsk'',
''existingPaths'',
''Sources'',
''newPaths'',
''SourceMerge'',
''SYSXPPASSEMBLY'',
''ElementTypes'',
''SchemaVersion'',
''Layer'',
''LayerVersioning'',
''GlobalFieldIdPool'')
)
)
OR
(
SO2.type IN (''V'') and
(
SO2.name like ''SysModel%'' or
SO2.name like ''Util%'' or
SO2.name like ''Model%'' or
SO2.name in (''ConfigurationKeys'', ''LicenseCodes'', ''Origins'',''SECURABLEOBJECT'')
SO2.name in (
''SECURITYENTRYPOINTLINK'',
''SECURITYPERMISSION'',
''SECURITYROLE'',
''SECURITYROLEEXPLODEDGRAPH'',
''SECURITYROLEPERMISSIONOVERRIDE'',
''SECURITYROLETASKGRANT'',
''SECURITYSUBROLE'',
''SECURITYSUBTASK'',
''SECURITYTASK'',
''SECURITYTASKENTRYPOINT'',
''SECURITYTASKEXPLODEDGRAPH'',
''SECURITYTASKPERMISSIONOVERRIDE''
)
)
)
OR
(
SO2.type IN (''TR'') and
SO2.name IN (
''ModelSecurityPermission_CreateSecurable'',
''UpdateChangedBy'',
''SetInstalledAndChangedBy'',
''UpdateModelFromManifestChangedBy'',
''IO_Trig_INS_ModelElement'',
''IO_Trig_INS_ModelElements'',
''IO_Trig_Del_ModelElements'',
''ModelSecurityCommon_Insert''
)
)
OR
(
SO2.type IN (''P'') and
(
SO2.name like ''XI_%'' or
SO2.name like ''XU_%''
)
)
OR
(
SO2.type IN (''FN'', ''TF'',''IF'',''FS'',''FT'') and
SO2.name IN (
''CreateTemplateName'',
''GetLayerMask'',
''GetNextAvailableFieldOrIndexAxId'',
''GetNextAvailableAxId'',
''GetAxIdHole'',
''IsAxIdExcluded'',
''SECURITYROLE_FUNC'',
''SECURITYTASK_FUNC''
)
)'
-- drop all other objects in order
SELECT @SQL =
'SELECT
CASE WHEN SO.type=''PK'' THEN ''ALTER TABLE ''+SCHEMA_NAME(SO.schema_id)+''.''+OBJECT_NAME(SO.parent_object_id)+'' DROP CONSTRAINT ''+ SO.name
WHEN SO.type=''U'' THEN ''DROP TABLE ''+SCHEMA_NAME(SO.schema_id)+''.''+ SO.[Name]
WHEN SO.type=''V'' THEN ''DROP VIEW ''+SCHEMA_NAME(SO.schema_id)+''.''+ SO.[Name]
WHEN SO.type=''P'' THEN ''DROP PROCEDURE ''+SCHEMA_NAME(SO.schema_id)+''.''+ SO.[Name]
WHEN SO.type=''TR'' THEN ''DROP TRIGGER ''+SCHEMA_NAME(SO.schema_id)+''.''+ SO.[Name]
WHEN SO.type IN (''FN'', ''TF'',''IF'',''FS'',''FT'') THEN ''DROP FUNCTION ''+SCHEMA_NAME(SO.schema_id)+''.''+ SO.[Name]
END
FROM SYS.OBJECTS SO
WHERE SO.schema_id = schema_id(''' + @schemaname + ''')
AND SO.type IN (''PK'', ''FN'', ''TF'', ''TR'', ''V'', ''U'', ''P'')
AND SO.object_id IN (' + @ModelStoreElementQuery + ')
ORDER BY CASE WHEN type = ''PK'' THEN 1
WHEN type in (''FN'', ''TF'', ''P'',''IF'',''FS'',''FT'') THEN 2
WHEN type = ''TR'' THEN 3
WHEN type = ''V'' THEN 4
WHEN type = ''U'' THEN 5
ELSE 6
END'

IF @WorkTest = 't' PRINT (@SQL )
INSERT INTO #dropmodelstore
EXEC (@SQL)

DECLARE @ID int, @statement varchar(2000)
DECLARE statement_cursor CURSOR
FOR SELECT SQLStatement
FROM #dropmodelstore
ORDER BY ID ASC

OPEN statement_cursor
FETCH statement_cursor INTO @statement
WHILE (@@FETCH_STATUS = 0)
BEGIN

PRINT (@statement)
FETCH statement_cursor INTO @statement
END

CLOSE statement_cursor
DEALLOCATE statement_cursor

PRINT '------- ALL - DONE -------'
END

GO
Hope this helps someone.

Viewing all articles
Browse latest Browse all 130240

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>