
from .. import utils


__all__ = ('cli_loader',)


@utils.with_cursor
def cli_command(cursor, migrations_directory='', version=None, **kwargs):
    cursor.execute("""\
        CREATE TABLE IF NOT EXISTS schema_migrations (
            version TEXT NOT NULL,
            applied TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
        )""")
    cursor.execute("""\
        DELETE FROM schema_migrations""")
    versions = []
    if version is None:
        timestamp = utils.timestamp()
    else:
        timestamp = str(version)
    for version, name in utils.get_migrations(migrations_directory):
        if version <= timestamp:
            versions.append((version,))
    cursor.executemany("""\
        INSERT INTO schema_migrations VALUES (%s)
        """, versions)
    print('Schema migrations initialized.')


def cli_loader(parser):
    parser.add_argument('--version', type=int,
                        help='Set the schema version to VERSION, '
                             'default current timestamp')
    return cli_command


from .. import utils


__all__ = ('cli_loader',)


@utils.with_cursor
def cli_command(cursor, migrations_directory='', version=None, **kwargs):
    cursor.execute("""\
        SELECT 1 FROM information_schema.tables
        WHERE table_name = 'schema_migrations'""")
    table_exists = cursor.fetchone()
    if table_exists:
        print('Schema migrations already initialized.')
        return

    cursor.execute("""\
        CREATE TABLE schema_migrations (
            version TEXT NOT NULL,
            applied TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
        )""")
    versions = []
    if version is None:
        timestamp = utils.timestamp()
    else:
        timestamp = str(version)
    for version, name in utils.get_migrations(migrations_directory):
        if version <= timestamp:
            versions.append((version,))
    cursor.executemany("""\
        INSERT INTO schema_migrations VALUES (%s)
        """, versions)
    print('Schema migrations initialized.')


def cli_loader(parser):
    parser.add_argument('--version', type=int,
                        help='Set the schema version to VERSION, '
                             'default current timestamp')
    return cli_command

Stop changing schema_migrations data if the table already exists
Stop changing schema_migrations data if the table already exists

  
  from .. import utils
  
  
  __all__ = ('cli_loader',)
  
  
  @utils.with_cursor
  def cli_command(cursor, migrations_directory='', version=None, **kwargs):
      cursor.execute("""\
+         SELECT 1 FROM information_schema.tables
+         WHERE table_name = 'schema_migrations'""")
+     table_exists = cursor.fetchone()
+     if table_exists:
+         print('Schema migrations already initialized.')
+         return
+ 
+     cursor.execute("""\
-         CREATE TABLE IF NOT EXISTS schema_migrations (
+         CREATE TABLE schema_migrations (
              version TEXT NOT NULL,
              applied TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
          )""")
-     cursor.execute("""\
-         DELETE FROM schema_migrations""")
      versions = []
      if version is None:
          timestamp = utils.timestamp()
      else:
          timestamp = str(version)
      for version, name in utils.get_migrations(migrations_directory):
          if version <= timestamp:
              versions.append((version,))
      cursor.executemany("""\
          INSERT INTO schema_migrations VALUES (%s)
          """, versions)
      print('Schema migrations initialized.')
  
  
  def cli_loader(parser):
      parser.add_argument('--version', type=int,
                          help='Set the schema version to VERSION, '
                               'default current timestamp')
      return cli_command
  
Stop changing schema_migrations data if the table already exists
## Code Before:

from .. import utils


__all__ = ('cli_loader',)


@utils.with_cursor
def cli_command(cursor, migrations_directory='', version=None, **kwargs):
    cursor.execute("""\
        CREATE TABLE IF NOT EXISTS schema_migrations (
            version TEXT NOT NULL,
            applied TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
        )""")
    cursor.execute("""\
        DELETE FROM schema_migrations""")
    versions = []
    if version is None:
        timestamp = utils.timestamp()
    else:
        timestamp = str(version)
    for version, name in utils.get_migrations(migrations_directory):
        if version <= timestamp:
            versions.append((version,))
    cursor.executemany("""\
        INSERT INTO schema_migrations VALUES (%s)
        """, versions)
    print('Schema migrations initialized.')


def cli_loader(parser):
    parser.add_argument('--version', type=int,
                        help='Set the schema version to VERSION, '
                             'default current timestamp')
    return cli_command

## Instruction:
Stop changing schema_migrations data if the table already exists
## Code After:

from .. import utils


__all__ = ('cli_loader',)


@utils.with_cursor
def cli_command(cursor, migrations_directory='', version=None, **kwargs):
    cursor.execute("""\
        SELECT 1 FROM information_schema.tables
        WHERE table_name = 'schema_migrations'""")
    table_exists = cursor.fetchone()
    if table_exists:
        print('Schema migrations already initialized.')
        return

    cursor.execute("""\
        CREATE TABLE schema_migrations (
            version TEXT NOT NULL,
            applied TIMESTAMP WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
        )""")
    versions = []
    if version is None:
        timestamp = utils.timestamp()
    else:
        timestamp = str(version)
    for version, name in utils.get_migrations(migrations_directory):
        if version <= timestamp:
            versions.append((version,))
    cursor.executemany("""\
        INSERT INTO schema_migrations VALUES (%s)
        """, versions)
    print('Schema migrations initialized.')


def cli_loader(parser):
    parser.add_argument('--version', type=int,
                        help='Set the schema version to VERSION, '
                             'default current timestamp')
    return cli_command
