Esempio n. 1
0
def test_rename_table(engine_with_schema):
    engine, schema = engine_with_schema
    table_name = "test_rename_table"
    new_table_name = "test_rename_table_new"
    old_table = tables.create_mathesar_table(table_name, schema, [], engine)
    old_oid = tables.get_oid_from_table(old_table.name, old_table.schema, engine)

    tables.rename_table(table_name, schema, engine, new_table_name)
    new_table = tables.reflect_table(new_table_name, schema, engine)
    new_oid = tables.get_oid_from_table(new_table.name, new_table.schema, engine)

    assert old_oid == new_oid
    assert new_table.name == new_table_name

    with pytest.raises(NoSuchTableError):
        tables.reflect_table(table_name, schema, engine)
Esempio n. 2
0
def test_change_column_nullable_with_data(engine_with_schema, nullable_tup):
    engine, schema = engine_with_schema
    table_name = "atablefornulling"
    target_column_name = "thecolumntochange"
    table = Table(
        table_name,
        MetaData(bind=engine, schema=schema),
        Column(target_column_name, Integer, nullable=nullable_tup[0]),
    )
    table.create()
    ins = table.insert().values([
        {
            target_column_name: 1
        },
        {
            target_column_name: 2
        },
        {
            target_column_name: 3
        },
    ])
    with engine.begin() as conn:
        conn.execute(ins)
    table_oid = tables.get_oid_from_table(table_name, schema, engine)
    changed_column = columns.change_column_nullable(table_oid, 0,
                                                    nullable_tup[1], engine)
    assert changed_column.nullable is nullable_tup[1]
Esempio n. 3
0
def test_change_column_nullable_changes_raises_with_null_data(
        engine_with_schema):
    engine, schema = engine_with_schema
    table_name = "atablefornulling"
    target_column_name = "thecolumntochange"
    table = Table(
        table_name,
        MetaData(bind=engine, schema=schema),
        Column(target_column_name, Integer, nullable=True),
    )
    table.create()
    ins = table.insert().values([
        {
            target_column_name: 1
        },
        {
            target_column_name: 2
        },
        {
            target_column_name: None
        },
    ])
    with engine.begin() as conn:
        conn.execute(ins)
    table_oid = tables.get_oid_from_table(table_name, schema, engine)
    with pytest.raises(IntegrityError) as e:
        columns.change_column_nullable(table_oid, 0, False, engine)
        assert type(e.orig) == NotNullViolation
Esempio n. 4
0
def create_table_from_csv(data_file, name, schema):
    engine = create_mathesar_engine(schema.database.name)
    db_table = create_db_table_from_data_file(data_file, name, schema)
    db_table_oid = tables.get_oid_from_table(db_table.name, db_table.schema,
                                             engine)
    table, _ = Table.objects.get_or_create(oid=db_table_oid, schema=schema)
    data_file.table_imported_to = table
    data_file.save()
    return table
Esempio n. 5
0
def empty_nasa_table(patent_schema):
    engine = create_mathesar_engine(patent_schema.database.name)
    db_table = SATable(NASA_TABLE,
                       MetaData(bind=engine),
                       Column('nasa_col1', String),
                       schema=patent_schema.name)
    db_table.create()
    db_table_oid = get_oid_from_table(db_table.name, db_table.schema, engine)
    table = Table.objects.create(oid=db_table_oid, schema=patent_schema)
    return table
Esempio n. 6
0
def _rename_column(schema, table_name, old_col_name, new_col_name, engine):
    """
    Renames the colum of a table and assert the change went through
    """
    table_oid = tables.get_oid_from_table(table_name, schema, engine)
    column_index = columns.get_column_index_from_name(table_oid, old_col_name,
                                                      engine)
    columns.rename_column(table_oid, column_index, new_col_name, engine)
    table = tables.reflect_table(table_name, schema, engine)
    assert new_col_name in table.columns
    assert old_col_name not in table.columns
    return table
Esempio n. 7
0
 def column_index(self):
     """
     Get the ordinal index of this column in its table, if it is
     attached to a table that is associated with the column's engine.
     """
     if (self.engine is not None and self.table is not None
             and inspect(self.engine).has_table(self.table.name,
                                                schema=self.table.schema)):
         table_oid = tables.get_oid_from_table(self.table.name,
                                               self.table.schema,
                                               self.engine)
         return get_column_index_from_name(table_oid, self.name,
                                           self.engine)
Esempio n. 8
0
def test_change_column_nullable_changes(engine_with_schema, nullable_tup):
    engine, schema = engine_with_schema
    table_name = "atablefornulling"
    target_column_name = "thecolumntochange"
    nontarget_column_name = "notthecolumntochange"
    table = Table(
        table_name,
        MetaData(bind=engine, schema=schema),
        Column(target_column_name, Integer, nullable=nullable_tup[0]),
        Column(nontarget_column_name, String),
    )
    table.create()
    table_oid = tables.get_oid_from_table(table_name, schema, engine)
    changed_column = columns.change_column_nullable(table_oid, 0,
                                                    nullable_tup[1], engine)
    assert changed_column.nullable is nullable_tup[1]
Esempio n. 9
0
def test_get_column_index_from_name(engine_with_schema):
    engine, schema = engine_with_schema
    table_name = "table_with_columns"
    zero_name = "colzero"
    one_name = "colone"
    table = Table(
        table_name,
        MetaData(bind=engine, schema=schema),
        Column(zero_name, Integer),
        Column(one_name, String),
    )
    table.create()
    table_oid = tables.get_oid_from_table(table_name, schema, engine)
    assert columns.get_column_index_from_name(table_oid, zero_name,
                                              engine) == 0
    assert columns.get_column_index_from_name(table_oid, one_name, engine) == 1
Esempio n. 10
0
def test_drop_column_correct_column(engine_with_schema):
    engine, schema = engine_with_schema
    table_name = "atable"
    target_column_name = "thecolumntodrop"
    nontarget_column_name = "notthecolumntodrop"
    table = Table(
        table_name,
        MetaData(bind=engine, schema=schema),
        Column(target_column_name, Integer),
        Column(nontarget_column_name, String),
    )
    table.create()
    table_oid = tables.get_oid_from_table(table_name, schema, engine)
    columns.drop_column(engine, table_oid, 0)
    altered_table = tables.reflect_table_from_oid(table_oid, engine)
    assert len(altered_table.columns) == 1
    assert nontarget_column_name in altered_table.columns
    assert target_column_name not in altered_table.columns
Esempio n. 11
0
def test_create_column(engine_with_schema):
    engine, schema = engine_with_schema
    table_name = "atableone"
    target_type = "BOOLEAN"
    initial_column_name = "original_column"
    new_column_name = "added_column"
    table = Table(
        table_name,
        MetaData(bind=engine, schema=schema),
        Column(initial_column_name, Integer),
    )
    table.create()
    table_oid = tables.get_oid_from_table(table_name, schema, engine)
    column_data = {"name": new_column_name, "type": target_type}
    created_col = columns.create_column(engine, table_oid, column_data)
    altered_table = tables.reflect_table_from_oid(table_oid, engine)
    assert len(altered_table.columns) == 2
    assert created_col.name == new_column_name
    assert created_col.type.compile(engine.dialect) == "BOOLEAN"
Esempio n. 12
0
def test_retype_column_correct_column(engine_with_schema):
    engine, schema = engine_with_schema
    table_name = "atableone"
    target_type = "boolean"
    target_column_name = "thecolumntochange"
    nontarget_column_name = "notthecolumntochange"
    table = Table(
        table_name,
        MetaData(bind=engine, schema=schema),
        Column(target_column_name, Integer),
        Column(nontarget_column_name, String),
    )
    table.create()
    table_oid = tables.get_oid_from_table(table_name, schema, engine)
    with patch.object(columns.alteration, "alter_column_type") as mock_retyper:
        columns.retype_column(table_oid, 0, target_type, engine)
    mock_retyper.assert_called_with(
        schema,
        table_name,
        target_column_name,
        "boolean",
        engine,
        friendly_names=False,
    )