You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by ad...@apache.org on 2017/05/03 13:52:50 UTC

[2/2] ambari git commit: AMBARI-20886. Create idempotent Ambari DB Schema SQL script for AzureDB

AMBARI-20886. Create idempotent Ambari DB Schema SQL script for AzureDB


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/928f8090
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/928f8090
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/928f8090

Branch: refs/heads/branch-2.5
Commit: 928f80908d36992452d06b43a77bc4ebfadef7fa
Parents: 5e4b94b
Author: Attila Doroszlai <ad...@hortonworks.com>
Authored: Wed May 3 15:37:50 2017 +0200
Committer: Attila Doroszlai <ad...@hortonworks.com>
Committed: Wed May 3 15:46:53 2017 +0200

----------------------------------------------------------------------
 ambari-server/pom.xml                           | 13 ++++
 ambari-server/src/main/assemblies/server.xml    |  5 ++
 .../src/main/python/azuredb_create_generator.py | 81 ++++++++++++++++++++
 ambari-server/src/main/resources/.gitignore     |  1 +
 .../src/main/sh/azuredb_create_generator.sh     | 26 +++++++
 5 files changed, 126 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/928f8090/ambari-server/pom.xml
----------------------------------------------------------------------
diff --git a/ambari-server/pom.xml b/ambari-server/pom.xml
index 7128598..231c5af 100644
--- a/ambari-server/pom.xml
+++ b/ambari-server/pom.xml
@@ -669,6 +669,19 @@
               <goal>exec</goal>
             </goals>
           </execution>
+          <execution>
+            <configuration>
+              <executable>${project.basedir}/src/main/sh/azuredb_create_generator.sh</executable>
+              <arguments>
+                <argument>${project.basedir}</argument>
+              </arguments>
+            </configuration>
+            <id>azuredb-gen</id>
+            <phase>generate-resources</phase>
+            <goals>
+              <goal>exec</goal>
+            </goals>
+          </execution>
         </executions>
       </plugin>
       <plugin>

http://git-wip-us.apache.org/repos/asf/ambari/blob/928f8090/ambari-server/src/main/assemblies/server.xml
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/assemblies/server.xml b/ambari-server/src/main/assemblies/server.xml
index a6996f0..74d96a4 100644
--- a/ambari-server/src/main/assemblies/server.xml
+++ b/ambari-server/src/main/assemblies/server.xml
@@ -334,6 +334,11 @@
     </file>
     <file>
       <fileMode>755</fileMode>
+      <source>target/classes/Ambari-DDL-AzureDB-CREATE.sql</source>
+      <outputDirectory>/var/lib/ambari-server/resources</outputDirectory>
+    </file>
+    <file>
+      <fileMode>755</fileMode>
       <source>target/classes/Ambari-DDL-SQLServer-CREATE.sql</source>
       <outputDirectory>/var/lib/ambari-server/resources</outputDirectory>
     </file>

http://git-wip-us.apache.org/repos/asf/ambari/blob/928f8090/ambari-server/src/main/python/azuredb_create_generator.py
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/python/azuredb_create_generator.py b/ambari-server/src/main/python/azuredb_create_generator.py
new file mode 100755
index 0000000..5972c56
--- /dev/null
+++ b/ambari-server/src/main/python/azuredb_create_generator.py
@@ -0,0 +1,81 @@
+#!/usr/bin/env python
+
+'''
+Licensed to the Apache Software Foundation (ASF) under one
+or more contributor license agreements.  See the NOTICE file
+distributed with this work for additional information
+regarding copyright ownership.  The ASF licenses this file
+to you under the Apache License, Version 2.0 (the
+"License"); you may not use this file except in compliance
+with the License.  You may obtain a copy of the License at
+
+    http://www.apache.org/licenses/LICENSE-2.0
+
+Unless required by applicable law or agreed to in writing, software
+distributed under the License is distributed on an "AS IS" BASIS,
+WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+See the License for the specific language governing permissions and
+limitations under the License.
+'''
+
+# This script transforms SQLServer "create" SQL to idempotent SQL for AzureDB.
+# It is a filter, ie. it expects input on stdin, and prints output on stdout.
+
+import fileinput
+import re
+from textwrap import dedent
+
+input_sql = "".join(fileinput.input())
+input_statements = re.split(';', input_sql)
+statements = []
+for statement in input_statements:
+  # wrap "CREATE TABLE" in IF for existence check
+  statement = re.sub(
+    "CREATE TABLE ([^\s(]+).*",
+    dedent('''\
+      IF NOT EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('dbo.\g<1>') AND type = 'U')
+      BEGIN
+      \g<0>
+      END
+      '''),
+    statement,
+    flags = re.DOTALL | re.IGNORECASE)
+
+  # wrap "CREATE INDEX" in IF for existence check
+  statement = re.sub("CREATE(?: NONCLUSTERED)? INDEX ([^ (]+).*",
+    dedent('''\
+      IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = '\g<1>')
+      BEGIN
+      \g<0>
+      END
+      '''),
+    statement,
+    flags = re.DOTALL | re.IGNORECASE)
+
+  # wrap "ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY" in IF for existence check
+  statement = re.sub("ALTER TABLE \S+ ADD CONSTRAINT (\S+) FOREIGN KEY.*",
+    dedent('''\
+      IF NOT EXISTS (SELECT 1 FROM sys.objects WHERE object_id = OBJECT_ID('\g<1>') AND type = 'F')
+      BEGIN
+      \g<0>
+      END
+      '''),
+    statement,
+    flags = re.DOTALL | re.IGNORECASE)
+
+  statements.append(statement)
+
+# find all INSERT statements, create a matching DELETE in reverse order, only one per table
+sql = "".join(statements)
+inserts = re.findall("INSERT INTO ([^ (]+)", sql, flags = re.IGNORECASE)
+tables = set()
+deletes = []
+for table in inserts:
+  if table not in tables:
+    deletes.append("  DELETE {0};".format(table))
+    tables.add(table)
+deletes.reverse()
+delete_sql = "\n".join(deletes)
+sql = re.sub("BEGIN TRANSACTION", "\g<0>\n" + delete_sql, sql, count=1)
+
+print sql

http://git-wip-us.apache.org/repos/asf/ambari/blob/928f8090/ambari-server/src/main/resources/.gitignore
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/resources/.gitignore b/ambari-server/src/main/resources/.gitignore
new file mode 100644
index 0000000..173a09f
--- /dev/null
+++ b/ambari-server/src/main/resources/.gitignore
@@ -0,0 +1 @@
+Ambari-DDL-AzureDB-CREATE.sql

http://git-wip-us.apache.org/repos/asf/ambari/blob/928f8090/ambari-server/src/main/sh/azuredb_create_generator.sh
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/sh/azuredb_create_generator.sh b/ambari-server/src/main/sh/azuredb_create_generator.sh
new file mode 100755
index 0000000..e233cf4
--- /dev/null
+++ b/ambari-server/src/main/sh/azuredb_create_generator.sh
@@ -0,0 +1,26 @@
+#!/usr/bin/env bash
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Creates an idempotent SQL script for AzureDB from the SQLServer "create" script.
+
+sql_dir="$1"/src/main/resources
+script_dir="$1"/src/main/python
+
+[[ -e "$sql_dir"/Ambari-DDL-SQLServer-CREATE.sql ]] || exit 1
+[[ -x "$script_dir"/azuredb_create_generator.py ]] || exit 2
+
+cat "$sql_dir"/Ambari-DDL-SQLServer-CREATE.sql | "$script_dir"/azuredb_create_generator.py > "$sql_dir"/Ambari-DDL-AzureDB-CREATE.sql