November Happy Hour will be moved to Thursday December 5th.

SQL script to change SiteDefinitions

Vote:
 

Hi!

We often refresh our test and development data with a fresh dump from production. I'm writing an sql script to change the site settings automatically after the data migration (manual work is no devops), but I can't find where the settings are store in the database. Could you please give me a hint in the right direction about the table? :)

Table tblSiteConfig has a promising name, but the content doesn't seem to match up with SiteDefinition current values. Is this table used in site configuration anymore?

#86450
May 21, 2014 19:19
Vote:
 

Hi Jouni,

HostDefinition is stored in tblBigTable, and SiteDefinition is stored in tblSystemBigTable

#86451
May 21, 2014 20:15
Vote:
 

Nice question, I am just about to do the same thing!

Jouni, can you please chare the code when you are done?

#86453
May 21, 2014 20:27
Vote:
 

Thanks! I'll see if I can make some sense of this :)

Henrik, certainly! I'll share the script if I get it working.  

#86464
May 22, 2014 10:07
Vote:
 

I forgot to send the queries:

SELECT * FROM tblBigTable where StoreName = 'EPiServer.Web.HostDefinition'

SELECT * FROM tblSystemBigTable WHERE StoreName = 'EPiServer.Web.SiteDefinition'
#86475
May 22, 2014 12:20
Vote:
 

As with other types in DDS there is also database views generated for each store. So an alternative is to do the data access through the views instead of going directly towards the tables.

#86544
May 23, 2014 11:52
Vote:
 

Thanks Johan! The views make the sql scripting super-easy!

use MySite

update dbo.[VW_EPiServer.Web.SiteDefinition]
   set SiteUrl = 'http://test.site.com/'
 where Name = 'MySite';
   
update dbo.[VW_EPiServer.Web.HostDefinition]
   set Name = 'test.site.com'
 where Name = 'www.site.com';

Remember that you can run scripts from command line, e.g. "call sqlcmd -i update_test_conf.sql". So you can automate the whole db restore + configuration, for example, with a batch file like:

set physicalPath=%~dp0
set dumpfolder=D:\temp\datadump
set database=MySite
set dbuser=MySite
 
:: restore db and set correct owner
call sqlcmd -E -Q "Alter Database %database% SET SINGLE_USER With ROLLBACK IMMEDIATE; RESTORE DATABASE %database% FROM DISK='%dumpfolder%\%database%.bak' WITH REPLACE; ALTER AUTHORIZATION ON DATABASE::%database% TO %dbuser%"
:: update correct configuration
call sqlcmd -i %physicalPath%update_test_conf.sql
#87604
Edited, Jun 17, 2014 23:21
Vote:
 

Btw. you can easily automate production data dumps too with command line, for example, run this batch file in production db (just add 7za.exe to the same folder)

:: Set your site specs here
set database=MySite
set vppfolder=D:\Webshare\MySite\appdata
:: Datadump output folder
set dumpfolder=D:\temp\datadump

mkdir %dumpfolder%

:: backup database. replace existing file
call sqlcmd -E -Q "BACKUP DATABASE %database% TO DISK='%dumpfolder%\%database%.bak' WITH FORMAT"

:: zip vpp folder, no compression
call 7za.exe a -mx0 %dumpfolder%\%database%.vpp.zip %vppfolder%\*
:: compress database backup ("fast" compression)
call 7za.exe a -mx3 %dumpfolder%\%database%.bak.zip %dumpfolder%\%database%.bak
:: combine zips to one archive
call 7za.exe a -mx0 %dumpfolder%\%database%.data.zip %dumpfolder%\%database%.bak.zip %dumpfolder%\%database%.vpp.zip

:: cleanup
call del %dumpfolder%\%database%.vpp.zip
call del %dumpfolder%\%database%.bak
call del %dumpfolder%\%database%.bak.zip

So, now you should have one zip archive named MySite.data.zip that contains both the database and vpp files. Let's transfer the file to the test environment db and execute following batch file (add 7za.exe and the previosly mentioned update_test_conf.sql to the same folder)

@echo off
:: better safe than sorry
IF NOT %COMPUTERNAME%==TEST_ENVIRONMENT_NAME (
    echo WRONG ENVIRONMENT!
    pause
    exit
)
@echo on

:: Set your site specs here 
set database=MySite
set dbuser=MySite
set vppfolder=D:\Webshare\MySite\appdata
:: Datadump input folder
set dumpfolder=D:\temp\datadump
set physicalPath=%~dp0

:: unzip packages
call 7za.exe x -o%dumpfolder% %dumpfolder%\%database%.data.zip 
call 7za.exe x -o%dumpfolder% %dumpfolder%\%database%.bak.zip 

:: restore db and set correct owner
call sqlcmd -E -Q "Alter Database %database% SET SINGLE_USER With ROLLBACK IMMEDIATE; RESTORE DATABASE %database% FROM DISK='%dumpfolder%\%database%.bak' WITH REPLACE; ALTER AUTHORIZATION ON DATABASE::%database% TO %dbuser%"
:: update correct configuration
call sqlcmd -i %physicalPath%update_test_conf.sql


:: delete old vpp data
FOR /D %%p IN ("%vppfolder%\*.*") DO rmdir "%%p" /s /q
:: sometimes the first call fails to remove all subdirs 
FOR /D %%p IN ("%vppfolder%\*.*") DO rmdir "%%p" /s /q
:: restore vpp from zip
call 7za.exe x -o%vppfolder% %dumpfolder%\%database%.vpp.zip

:: cleanup
call del %dumpfolder%\%database%.vpp.zip
call del %dumpfolder%\%database%.bak
call del %dumpfolder%\%database%.bak.zip

If all goes well, you should now have freshly updated data dump in your test environment :)

#87605
Edited, Jun 17, 2014 23:33
Vote:
 

Site and host definitions are no longer kept in DDS but instead in own tables. So the previous script no longer works. This one does:

use MySite
 
update dbo.[tblSiteDefinition]
   set SiteUrl = 'http://test.site.com/'
 where Name = 'MySite';
    
update dbo.[tblHostDefinition]
   set Name = 'test.site.com'
 where Name = 'www.site.com';
#132023
Aug 12, 2015 13:38
Vote:
 

Thanks Jouni, I have missed that schema change and I use it a lot so, thanks for saving me some time bugfixing my restore-script! :)

#132025
Aug 12, 2015 13:48
Vote:
 

Thanks! I also missed the switch to separate tables. Strange thing tho in our DB the old views are still there, pointing to big table and returing data that is no longer correct. Shouldn't this data has been migrated/cleaned up during upgrade to some version?

#143896
Feb 02, 2016 23:20
Vote:
 

It might be that they are not used any more, but in that case they should have been removed.

Another question.
I noticed that changing this works great, but if you are using a clould-license the old url is still in the licence definition, are there any way to also change this by SQL-script?

Right now I update the dev/test environment with the sql script but then I need to go manually into the site to update to licence.

#143907
Feb 03, 2016 10:48
This topic was created over six months ago and has been resolved. If you have a similar question, please create a new topic and refer to this one.
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.