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/945b467c Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/945b467c Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/945b467c Branch: refs/heads/branch-2.5 Commit: 945b467c871d9fa746ff23bb4bd4eebfde24c36f Parents: 91ab3c3 Author: Attila Doroszlai <[email protected]> Authored: Thu May 4 08:15:28 2017 +0200 Committer: Attila Doroszlai <[email protected]> Committed: Thu May 4 08:23:08 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/945b467c/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)
