__all__ = [
    'ensure_only_one_row',
    'insert_or_ignore',
]


def ensure_only_one_row(rows):
    row = rows.fetchone()
    if row is None or rows.fetchone() is not None:
        raise KeyError
    return row


def insert_or_ignore(conn, table, values):
    conn.execute(table.insert().prefix_with('OR IGNORE'), values)

__all__ = [
    'add_if_not_exists_clause',
    'ensure_only_one_row',
    'insert_or_ignore',
]

from garage import asserts

from sqlalchemy.schema import CreateIndex


def add_if_not_exists_clause(index, engine):
    # `sqlalchemy.Index.create()` does not take `checkfirst` for reasons
    # that I am unaware of, and here is a hack for sidestep that.
    stmt = str(CreateIndex(index).compile(engine))
    stmt = stmt.replace('CREATE INDEX', 'CREATE INDEX IF NOT EXISTS', 1)
    asserts.postcond('IF NOT EXISTS' in stmt, stmt)
    return stmt


def ensure_only_one_row(rows):
    row = rows.fetchone()
    if row is None or rows.fetchone() is not None:
        raise KeyError
    return row


def insert_or_ignore(conn, table, values):
    conn.execute(table.insert().prefix_with('OR IGNORE'), values)

Add a hack for appending "IF NOT EXISTS" clause to "CREATE INDEX"
Add a hack for appending "IF NOT EXISTS" clause to "CREATE INDEX"

  __all__ = [
+     'add_if_not_exists_clause',
      'ensure_only_one_row',
      'insert_or_ignore',
  ]
+ 
+ from garage import asserts
+ 
+ from sqlalchemy.schema import CreateIndex
+ 
+ 
+ def add_if_not_exists_clause(index, engine):
+     # `sqlalchemy.Index.create()` does not take `checkfirst` for reasons
+     # that I am unaware of, and here is a hack for sidestep that.
+     stmt = str(CreateIndex(index).compile(engine))
+     stmt = stmt.replace('CREATE INDEX', 'CREATE INDEX IF NOT EXISTS', 1)
+     asserts.postcond('IF NOT EXISTS' in stmt, stmt)
+     return stmt
  
  
  def ensure_only_one_row(rows):
      row = rows.fetchone()
      if row is None or rows.fetchone() is not None:
          raise KeyError
      return row
  
  
  def insert_or_ignore(conn, table, values):
      conn.execute(table.insert().prefix_with('OR IGNORE'), values)
  
Add a hack for appending "IF NOT EXISTS" clause to "CREATE INDEX"
## Code Before:
__all__ = [
    'ensure_only_one_row',
    'insert_or_ignore',
]


def ensure_only_one_row(rows):
    row = rows.fetchone()
    if row is None or rows.fetchone() is not None:
        raise KeyError
    return row


def insert_or_ignore(conn, table, values):
    conn.execute(table.insert().prefix_with('OR IGNORE'), values)

## Instruction:
Add a hack for appending "IF NOT EXISTS" clause to "CREATE INDEX"
## Code After:
__all__ = [
    'add_if_not_exists_clause',
    'ensure_only_one_row',
    'insert_or_ignore',
]

from garage import asserts

from sqlalchemy.schema import CreateIndex


def add_if_not_exists_clause(index, engine):
    # `sqlalchemy.Index.create()` does not take `checkfirst` for reasons
    # that I am unaware of, and here is a hack for sidestep that.
    stmt = str(CreateIndex(index).compile(engine))
    stmt = stmt.replace('CREATE INDEX', 'CREATE INDEX IF NOT EXISTS', 1)
    asserts.postcond('IF NOT EXISTS' in stmt, stmt)
    return stmt


def ensure_only_one_row(rows):
    row = rows.fetchone()
    if row is None or rows.fetchone() is not None:
        raise KeyError
    return row


def insert_or_ignore(conn, table, values):
    conn.execute(table.insert().prefix_with('OR IGNORE'), values)
