prettify

baseline

Copy SQL Server Diagram

Problem:

A team is developing an application. Each developer works on a copy of the DB (as should)

When the DB Schema is changed a script is used to roll-out the changes.

But what about the Database Diagram? How can each developer have an updated Schema?

Solution

USE DATABASE_NAME
Replace 'Diagram_Name' with the name of the diagram we want to copy.
Query => Results To => Results to text
Run the script:
DECLARE @line NVARCHAR(MAX)=''
,@index INT=1
,@chunk INT=32
,@diagram_id INT=(SELECT diagram_id FROM sysdiagrams WHERE name='Diagram_Name')
,@newId INT=0;
DECLARE @size INT=(SELECT DATALENGTH(definition) FROM sysdiagrams WHERE diagram_id=@diagram_id);

SELECT @line = 'INSERT INTO sysdiagrams ([name], [principal_id], [version], [definition])'
            + ' VALUES (''' + [name] + ''', '+ CAST (principal_id AS VARCHAR(100))+', '+CAST (version AS VARCHAR(100))+', 0x);'
                    FROM sysdiagrams WHERE diagram_id = @diagram_id
PRINT 'SET NOCOUNT ON'
PRINT 'DECLARE @id INT;'
PRINT @line
PRINT 'SET @id=SCOPE_IDENTITY();'
WHILE @index < @size BEGIN
 SELECT @line =  
  'UPDATE sysdiagrams SET [definition] .Write ('
  + ' ' + UPPER(sys.fn_varbintohexstr (SUBSTRING (definition, @index, @chunk)))
  + ', null, 0) WHERE diagram_id = @id;' -- index:' + CAST(@index AS VARCHAR(100))
 FROM    sysdiagrams 
 WHERE    diagram_id = @diagram_id
 PRINT @line
 SET @index = @index + @chunk
END
  • In the results windows an UPDATE_SCRIPT is output that can recreate the Database Diagram.
  • Send the UPDATE_SCRIPT to target computer.
  • Open SQL Server Management Studio.
  • New Query
  • Select target Database
  • If there is no diagram click on Database_Diagrams and confirm to let Management Studio create sysdiagrams table.
  • If an older version of the diagram exists, delete it.
  • Paste UPDATE_SCRIPT
  • RUN UPDATE_SCRIPT
  • Refresh Database to see the created Diagram.
  • You are done.
Credits: http://www.conceptdevelopment.net/Database/ScriptDiagram2008/

Blogger code prettifier

Problem:

When posting code in blogger looks awful.

Solution:

There is a nice open source project that prettifies the posted code.

Download code

  • http://code.google.com/p/google-code-prettify/downloads/list
  • download prettify-small-21-Jul-2010.zip (or latest) (I includee the date so I remember when to update)
  • Extract to any folder you like on your disk.
  • open folder "prettify" and leave it open (will be needed soon)
  • Use Notepad++ for opening and copying minimized file content. Most editors mess long lines.

Create a Gadget in Blogger:

  • http://www.blogger.com
  • Layout
  • Add a Gadget (anywhere)
  • Basics
  • HTML/Javascript
  • Title: prettify (choose any title you like)
  • Content:
<style type="text/css">
===>insert contents of  prettify.css <===
</style>

<script type="text/javascript">
 ===> insert contents of prettify.js (do not use minified-does NOT work!) <===
</script>
I customize adding:
(Seems that blogger encloses post in a div with class="post" so that we can style it safely without changing the rest of the page.)
<style type="text/css">
.post .prettyprint { 
 border:1px dotted #666; 
 background-color:#ffc; 
 overflow:auto; 
}
.post .prettyprint ol.linenums li {
 list-style-type:decimal-leading-zero;
}
.post .nocode { 
 background-color:#fff; 
 border-right:1px dotted #666; 
} 
</style>

<script type="text/javascript>
 ===> insert contents of lang-css.js <===
 ===> insert contents of lang-sql.js <===
 ===> if needed insert contents of lang-*.js where * is apollo,hs,lisp,lua,ml,proto,vb,vhdl,wiki... <===
</script>

Modify Blogger Template:

  • Layout
  • Edit HTML
  • Click in template TextArea
  • find (^F) <body>
  • change to
<body onload="prettyPrint()" >

How to use:

Browsers display Tabs as 8 characters and not 4. So:
If you use Tabs in your source and Visual Studio 2010 then:
  1. ^A (Select All)
  2. Edit > Advanced > Untabify Selected Lines
  3. ^C (Copy)
  4. ^Z (Undo Untabify)
Clipboard now contains untabified source.
Edit the code sample you want to post
changeto
<&lt;
>&gt;
&&amp;
In the post enter:
<pre class="prettyprint lang-html">
Paste edited (as above) code here
</pre>
We can replace html in lang-html with:
"bsh", "c", "cc", "cpp", "cs", "csh", "cyc", "cv", "htm", "html", "java", "js", "m", "mxml", "perl", "pl", "pm", "py", "rb", "sh", "xhtml", "xml", "xsl".
The above languages are supported by default. You can add more if you include the corresponding js from src folder.

Numbering lines:

<pre class="prettyprint linenums:1">/* This is line 1 of my code
 * and here's line 2 */
print("I'm line number 3");
</pre>
gives (no, it doen't work- You have to go into prettify.css and delete everything about classes L0-L9:
li.L0,li.L1,li.L2,li.L3,li.L5,li.L6,li.L7,li.L8{list-style:none} li.L1,li.L3,li.L5,li.L7,li.L9{background:#eee})
/* This is line 1 of my code
 * and here's line 2 */
print("I'm line number 3");