<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[15654] CalendarServer/trunk</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.calendarserver.org//changeset/15654">15654</a></dd>
<dt>Author</dt> <dd>cdaboo@apple.com</dd>
<dt>Date</dt> <dd>2016-06-03 12:49:01 -0700 (Fri, 03 Jun 2016)</dd>
</dl>

<h3>Log Message</h3>
<pre>psql-based schema check now does all the same checks that the internal txn based schema check does.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#CalendarServertrunkcalendarservertoolscheckdatabaseschemapy">CalendarServer/trunk/calendarserver/tools/checkdatabaseschema.py</a></li>
<li><a href="#CalendarServertrunkrequirementscstxt">CalendarServer/trunk/requirements-cs.txt</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="CalendarServertrunkcalendarservertoolscheckdatabaseschemapy"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/calendarserver/tools/checkdatabaseschema.py (15653 => 15654)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/calendarserver/tools/checkdatabaseschema.py        2016-06-03 19:47:17 UTC (rev 15653)
+++ CalendarServer/trunk/calendarserver/tools/checkdatabaseschema.py        2016-06-03 19:49:01 UTC (rev 15654)
</span><span class="lines">@@ -16,13 +16,20 @@
</span><span class="cx"> from __future__ import print_function
</span><span class="cx"> 
</span><span class="cx"> from getopt import getopt, GetoptError
</span><ins>+
+from twext.enterprise.dal.model import Schema, Table, Column, Sequence, SQLType, \
+    Constraint, Function
+from twext.enterprise.dal.parseschema import addSQLToSchema, schemaFromPath
+
+from twisted.python.filepath import FilePath
+
+from txdav.common.datastore.sql_dump import DTYPE_MAP_POSTGRES, \
+    DEFAULTVALUE_MAP_POSTGRES
+
</ins><span class="cx"> import os
</span><span class="cx"> import re
</span><span class="cx"> import subprocess
</span><span class="cx"> import sys
</span><del>-from twext.enterprise.dal.model import Schema, Table, Column, Sequence
-from twext.enterprise.dal.parseschema import addSQLToSchema, schemaFromPath
-from twisted.python.filepath import FilePath
</del><span class="cx"> 
</span><span class="cx"> USERNAME = &quot;caldav&quot;
</span><span class="cx"> DATABASENAME = &quot;caldav&quot;
</span><span class="lines">@@ -86,7 +93,7 @@
</span><span class="cx">             (PSQL, e.output, e.returncode)
</span><span class="cx">         )
</span><span class="cx"> 
</span><del>-    return [s.strip() for s in out.splitlines()[:-1]]
</del><ins>+    return [map(lambda x: x.strip(), s.split(&quot;|&quot;)) for s in out.splitlines()[:-1]]
</ins><span class="cx"> 
</span><span class="cx"> 
</span><span class="cx"> 
</span><span class="lines">@@ -103,7 +110,7 @@
</span><span class="cx">     )
</span><span class="cx"> 
</span><span class="cx">     try:
</span><del>-        version = int(out[0])
</del><ins>+        version = int(out[0][0])
</ins><span class="cx">     except ValueError, e:
</span><span class="cx">         raise CheckSchemaError(
</span><span class="cx">             &quot;Failed to parse schema version: %s&quot; % (e,)
</span><span class="lines">@@ -114,51 +121,109 @@
</span><span class="cx"> 
</span><span class="cx"> def dumpCurrentSchema(verbose=False):
</span><span class="cx"> 
</span><ins>+    schemaname = &quot;public&quot;
+
</ins><span class="cx">     schema = Schema(&quot;Dumped schema&quot;)
</span><del>-    tables = {}
</del><span class="cx"> 
</span><ins>+    # Sequences
+    seqs = {}
+    rows = execSQL(
+        &quot;Schema sequences...&quot;,
+        &quot;select sequence_name from information_schema.sequences where sequence_schema = '%s';&quot; % (schemaname,),
+        verbose
+    )
+    for row in rows:
+        name = row[0]
+        seqs[name.upper()] = Sequence(schema, name.upper())
+
</ins><span class="cx">     # Tables
</span><ins>+    tables = {}
</ins><span class="cx">     rows = execSQL(
</span><span class="cx">         &quot;Schema tables...&quot;,
</span><del>-        &quot;select table_name from information_schema.tables where table_schema = 'public';&quot;,
</del><ins>+        &quot;select table_name from information_schema.tables where table_schema = '%s';&quot; % (schemaname,),
</ins><span class="cx">         verbose
</span><span class="cx">     )
</span><span class="cx">     for row in rows:
</span><del>-        name = row
-        table = Table(schema, name)
-        tables[name] = table
</del><ins>+        name = row[0]
+        table = Table(schema, name.upper())
+        tables[name.upper()] = table
</ins><span class="cx"> 
</span><span class="cx">         # Columns
</span><span class="cx">         rows = execSQL(
</span><span class="cx">             &quot;Reading table '{}' columns...&quot;.format(name),
</span><del>-            &quot;select column_name from information_schema.columns where table_schema = 'public' and table_name = '{}';&quot;.format(name),
-            verbose
</del><ins>+            &quot;select column_name, data_type, is_nullable, character_maximum_length, column_default from information_schema.columns where table_schema = '%s' and table_name = '%s';&quot; % (schemaname, name,),
+            verbose,
</ins><span class="cx">         )
</span><del>-        for row in rows:
-            name = row
</del><ins>+        for name, datatype, is_nullable, charlen, default in rows:
</ins><span class="cx">             # TODO: figure out the type
</span><del>-            column = Column(table, name, None)
</del><ins>+            column = Column(table, name.upper(), SQLType(DTYPE_MAP_POSTGRES.get(datatype, datatype), int(charlen) if charlen else 0))
</ins><span class="cx">             table.columns.append(column)
</span><ins>+            if default:
+                if default.startswith(&quot;nextval(&quot;):
+                    dname = default.split(&quot;'&quot;)[1].split(&quot;.&quot;)[-1]
+                    column.default = seqs[dname.upper()]
+                elif default in DEFAULTVALUE_MAP_POSTGRES:
+                    column.default = DEFAULTVALUE_MAP_POSTGRES[default]
+                else:
+                    try:
+                        column.default = int(default)
+                    except ValueError:
+                        column.default = default
+            if is_nullable == &quot;NO&quot;:
+                table.tableConstraint(Constraint.NOT_NULL, [column.name, ])
</ins><span class="cx"> 
</span><ins>+    # Key columns
+    keys = {}
+    rows = execSQL(
+        &quot;Schema key columns...&quot;,
+        &quot;select constraint_name, table_name, column_name from information_schema.key_column_usage where constraint_schema = '%s';&quot; % (schemaname,),
+        verbose
+    )
+    for conname, tname, cname in rows:
+        keys[conname] = (tname, cname)
+
+    # Constraints
+    constraints = {}
+    rows = execSQL(
+        &quot;SChema constraints...&quot;,
+        &quot;select constraint_name, table_name, column_name from information_schema.constraint_column_usage where constraint_schema = '%s';&quot; % (schemaname,),
+        verbose
+    )
+    for conname, tname, cname in rows:
+        constraints[conname] = (tname, cname)
+
+    # References - referential_constraints
+    rows = execSQL(
+        &quot;Schema referential constraints...&quot;,
+        &quot;select constraint_name, unique_constraint_name, delete_rule from information_schema.referential_constraints where constraint_schema = '%s';&quot; % (schemaname,),
+        verbose
+    )
+    for conname, uconname, delete in rows:
+        table = tables[keys[conname][0].upper()]
+        column = table.columnNamed(keys[conname][1].upper())
+        column.doesReferenceName(constraints[uconname][0].upper())
+        if delete != &quot;NO ACTION&quot;:
+            column.deleteAction = delete.lower()
+
</ins><span class="cx">     # Indexes
</span><span class="cx">     # TODO: handle implicit indexes created via primary key() and unique() statements within CREATE TABLE
</span><span class="cx">     rows = execSQL(
</span><span class="cx">         &quot;Schema indexes...&quot;,
</span><del>-        &quot;select indexdef from pg_indexes where schemaname = 'public';&quot;,
</del><ins>+        &quot;select indexdef from pg_indexes where schemaname = '%s';&quot; % (schemaname,),
</ins><span class="cx">         verbose
</span><span class="cx">     )
</span><span class="cx">     for indexdef in rows:
</span><del>-        addSQLToSchema(schema, indexdef.replace(&quot;public.&quot;, &quot;&quot;))
</del><ins>+        addSQLToSchema(schema, indexdef[0].replace(&quot;%s.&quot; % (schemaname,), &quot;&quot;).upper())
</ins><span class="cx"> 
</span><del>-    # Sequences
</del><ins>+    # Functions
</ins><span class="cx">     rows = execSQL(
</span><del>-        &quot;Schema sequences...&quot;,
-        &quot;select sequence_name from information_schema.sequences where sequence_schema = 'public';&quot;,
</del><ins>+        &quot;Schema functions&quot;,
+        &quot;select routine_name from information_schema.routines where routine_schema = '%s';&quot; % (schemaname,),
</ins><span class="cx">         verbose
</span><span class="cx">     )
</span><span class="cx">     for row in rows:
</span><del>-        name = row
-        Sequence(schema, name)
</del><ins>+        name = row[0]
+        Function(schema, name)
</ins><span class="cx"> 
</span><span class="cx">     return schema
</span><span class="cx"> 
</span><span class="lines">@@ -227,7 +292,7 @@
</span><span class="cx">                 if skt.basename().startswith(&quot;ccs_postgres_&quot;):
</span><span class="cx">                     PGSOCKETDIR = skt.path
</span><span class="cx">             PSQL = &quot;/Applications/Server.app/Contents/ServerRoot/usr/bin/psql&quot;
</span><del>-            SCHEMADIR = &quot;/Applications/Server.app/Contents/ServerRoot/usr/share/caldavd/lib/python/txdav/common/datastore/sql_schema/&quot;
</del><ins>+            SCHEMADIR = &quot;/Applications/Server.app/Contents/ServerRoot/Library/CalendarServer/lib/python2.7/site-packages/txdav/common/datastore/sql_schema/&quot;
</ins><span class="cx">         elif opt in (&quot;-v&quot;, &quot;--verbose&quot;):
</span><span class="cx">             verbose = True
</span><span class="cx">         else:
</span></span></pre></div>
<a id="CalendarServertrunkrequirementscstxt"></a>
<div class="modfile"><h4>Modified: CalendarServer/trunk/requirements-cs.txt (15653 => 15654)</h4>
<pre class="diff"><span>
<span class="info">--- CalendarServer/trunk/requirements-cs.txt        2016-06-03 19:47:17 UTC (rev 15653)
+++ CalendarServer/trunk/requirements-cs.txt        2016-06-03 19:49:01 UTC (rev 15654)
</span><span class="lines">@@ -7,7 +7,7 @@
</span><span class="cx">     zope.interface==4.1.3
</span><span class="cx">             setuptools==18.5
</span><span class="cx"> 
</span><del>-    --editable svn+http://svn.calendarserver.org/repository/calendarserver/twext/trunk@15633#egg=twextpy
</del><ins>+    --editable svn+http://svn.calendarserver.org/repository/calendarserver/twext/trunk@15653#egg=twextpy
</ins><span class="cx">         cffi==1.3.0
</span><span class="cx">             pycparser==2.14
</span><span class="cx">         #twisted
</span></span></pre>
</div>
</div>

</body>
</html>