
# revision identifiers, used by Alembic.
revision = '5254f4f31427'
down_revision = '512c71e4d93b'

import sqlalchemy as sa
from sqlalchemy.sql import table, column
from alembic import op
from ggrc.models.track_object_state import ObjectStates, ObjectStateTables

def upgrade():
  for table_name in ObjectStateTables.table_names:
    op.add_column(table_name, sa.Column('os_state', sa.String(length=16), nullable=True))

    # Set the value into all existing records
    object_table = table(table_name,
                         column('os_state', sa.String(length=16)))
    connection = op.get_bind()
    connection.execute(
      object_table.update().values(
        {
          'os_state': ObjectStates.DRAFT
        }
      )
    )

    # Make the field not-nullable
    op.alter_column(table_name, 'os_state',existing_type=sa.String(length=16),nullable=False)

def downgrade():
  for table_name in ObjectStateTables:
    op.drop_column(table_name, 'os_state')


# revision identifiers, used by Alembic.
revision = '5254f4f31427'
down_revision = '512c71e4d93b'

import sqlalchemy as sa
from sqlalchemy.sql import table, column
from alembic import op
from ggrc.models.track_object_state import ObjectStates, ObjectStateTables

def upgrade():
  for table_name in ObjectStateTables.table_names:
    op.add_column(table_name, sa.Column('os_state', sa.String(length=16), nullable=True))

    # Set the value into all existing records
    object_table = table(table_name,
                         column('os_state', sa.String(length=16)))
    connection = op.get_bind()
    connection.execute(
      object_table.update().values(
        {
          'os_state': ObjectStates.DRAFT
        }
      )
    )

    # Make the field not-nullable
    op.alter_column(table_name, 'os_state',existing_type=sa.String(length=16),nullable=False)

def downgrade():
  for table_name in ObjectStateTables.table_names:
    op.drop_column(table_name, 'os_state')

Fix db_downgrade for "System editable object state"
Fix db_downgrade for "System editable object state"

  
  # revision identifiers, used by Alembic.
  revision = '5254f4f31427'
  down_revision = '512c71e4d93b'
  
  import sqlalchemy as sa
  from sqlalchemy.sql import table, column
  from alembic import op
  from ggrc.models.track_object_state import ObjectStates, ObjectStateTables
  
  def upgrade():
    for table_name in ObjectStateTables.table_names:
      op.add_column(table_name, sa.Column('os_state', sa.String(length=16), nullable=True))
  
      # Set the value into all existing records
      object_table = table(table_name,
                           column('os_state', sa.String(length=16)))
      connection = op.get_bind()
      connection.execute(
        object_table.update().values(
          {
            'os_state': ObjectStates.DRAFT
          }
        )
      )
  
      # Make the field not-nullable
      op.alter_column(table_name, 'os_state',existing_type=sa.String(length=16),nullable=False)
  
  def downgrade():
-   for table_name in ObjectStateTables:
+   for table_name in ObjectStateTables.table_names:
      op.drop_column(table_name, 'os_state')
  
Fix db_downgrade for "System editable object state"
## Code Before:

# revision identifiers, used by Alembic.
revision = '5254f4f31427'
down_revision = '512c71e4d93b'

import sqlalchemy as sa
from sqlalchemy.sql import table, column
from alembic import op
from ggrc.models.track_object_state import ObjectStates, ObjectStateTables

def upgrade():
  for table_name in ObjectStateTables.table_names:
    op.add_column(table_name, sa.Column('os_state', sa.String(length=16), nullable=True))

    # Set the value into all existing records
    object_table = table(table_name,
                         column('os_state', sa.String(length=16)))
    connection = op.get_bind()
    connection.execute(
      object_table.update().values(
        {
          'os_state': ObjectStates.DRAFT
        }
      )
    )

    # Make the field not-nullable
    op.alter_column(table_name, 'os_state',existing_type=sa.String(length=16),nullable=False)

def downgrade():
  for table_name in ObjectStateTables:
    op.drop_column(table_name, 'os_state')

## Instruction:
Fix db_downgrade for "System editable object state"
## Code After:

# revision identifiers, used by Alembic.
revision = '5254f4f31427'
down_revision = '512c71e4d93b'

import sqlalchemy as sa
from sqlalchemy.sql import table, column
from alembic import op
from ggrc.models.track_object_state import ObjectStates, ObjectStateTables

def upgrade():
  for table_name in ObjectStateTables.table_names:
    op.add_column(table_name, sa.Column('os_state', sa.String(length=16), nullable=True))

    # Set the value into all existing records
    object_table = table(table_name,
                         column('os_state', sa.String(length=16)))
    connection = op.get_bind()
    connection.execute(
      object_table.update().values(
        {
          'os_state': ObjectStates.DRAFT
        }
      )
    )

    # Make the field not-nullable
    op.alter_column(table_name, 'os_state',existing_type=sa.String(length=16),nullable=False)

def downgrade():
  for table_name in ObjectStateTables.table_names:
    op.drop_column(table_name, 'os_state')
