Skip to content

Commit

Permalink
Merge pull request maxtepkeev#44 from soyking/create_table_if_not_exists
Browse files Browse the repository at this point in the history
Use CREATE TABLE IF NOT EXISTS in Postgres insert trigger
  • Loading branch information
maxtepkeev authored Sep 14, 2017
2 parents 512ea89 + f75a471 commit d4eb81f
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions architect/databases/postgresql/partition.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,18 +45,14 @@ def prepare(self):
{variables}
END IF;
IF NOT EXISTS(
SELECT 1 FROM information_schema.tables WHERE table_name=tablename)
THEN
BEGIN
EXECUTE 'CREATE TABLE ' || tablename || ' (
CHECK (' || checks || '),
LIKE "{{parent_table}}" INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING INDEXES
) INHERITS ("{{parent_table}}");';
EXCEPTION WHEN duplicate_table THEN
-- pass
END;
END IF;
BEGIN
EXECUTE 'CREATE TABLE IF NOT EXISTS ' || tablename || ' (
CHECK (' || checks || '),
LIKE "{{parent_table}}" INCLUDING DEFAULTS INCLUDING CONSTRAINTS INCLUDING INDEXES
) INHERITS ("{{parent_table}}");';
EXCEPTION WHEN duplicate_table THEN
-- pass
END;
EXECUTE 'INSERT INTO ' || tablename || ' VALUES (($1).*);' USING NEW;
RETURN NEW;
Expand Down

0 comments on commit d4eb81f

Please sign in to comment.