AMBARI-20886. Create idempotent Ambari DB Schema SQL script for AzureDB - addendum for Python 2.6
Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/fa832299 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/fa832299 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/fa832299 Branch: refs/heads/branch-feature-AMBARI-12556 Commit: fa8322994385081187e285e43fe36d4575743ed4 Parents: 2549963 Author: Attila Doroszlai <adorosz...@hortonworks.com> Authored: Thu May 4 08:15:28 2017 +0200 Committer: Attila Doroszlai <adorosz...@hortonworks.com> Committed: Thu May 4 08:22:47 2017 +0200 ---------------------------------------------------------------------- .../src/main/python/azuredb_create_generator.py | 22 ++++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/fa832299/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 index 5972c56..6ceaa90 100755 --- a/ambari-server/src/main/python/azuredb_create_generator.py +++ b/ambari-server/src/main/python/azuredb_create_generator.py @@ -25,43 +25,47 @@ import fileinput import re from textwrap import dedent +flags = re.DOTALL | re.IGNORECASE +create_table_re = re.compile("CREATE TABLE ([^\s(]+).*", flags = flags) +create_index_re = re.compile("CREATE(?: NONCLUSTERED)? INDEX ([^ (]+).*", flags = flags) +add_fk_const_re = re.compile("ALTER TABLE \S+ ADD CONSTRAINT (\S+) FOREIGN KEY.*", flags = flags) + 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(]+).*", + create_table_re, 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) + statement) # wrap "CREATE INDEX" in IF for existence check - statement = re.sub("CREATE(?: NONCLUSTERED)? INDEX ([^ (]+).*", + statement = re.sub( + create_index_re, dedent('''\ IF NOT EXISTS (SELECT 1 FROM sys.indexes WHERE name = '\g<1>') BEGIN \g<0> END '''), - statement, - flags = re.DOTALL | re.IGNORECASE) + statement) # wrap "ALTER TABLE ... ADD CONSTRAINT ... FOREIGN KEY" in IF for existence check - statement = re.sub("ALTER TABLE \S+ ADD CONSTRAINT (\S+) FOREIGN KEY.*", + statement = re.sub( + add_fk_const_re, 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) + statement) statements.append(statement)