def test_schema_get_with_reflect_change(client, test_db_name): engine = create_mathesar_engine(test_db_name) schema_name = 'a_new_schema' with engine.begin() as conn: conn.execute(text(f'CREATE SCHEMA {schema_name};')) cache.clear() response = client.get('/api/v0/schemas/') response_data = response.json() orig_created = [ schema for schema in response_data['results'] if schema['name'] == schema_name ] assert len(orig_created) == 1 orig_id = orig_created[0]['id'] new_schema_name = 'even_newer_schema' with engine.begin() as conn: conn.execute( text(f'ALTER SCHEMA {schema_name} RENAME TO {new_schema_name};')) cache.clear() response = client.get('/api/v0/schemas/') response_data = response.json() orig_created = [ schema for schema in response_data['results'] if schema['name'] == schema_name ] assert len(orig_created) == 0 modified = [ schema for schema in response_data['results'] if schema['name'] == new_schema_name ] modified_id = modified[0]['id'] assert len(modified) == 1 assert orig_id == modified_id
def patent_schema(test_db_model, create_schema): engine = create_mathesar_engine(test_db_model.name) install.install_mathesar_on_database(engine) with engine.begin() as conn: conn.execute(text(f'DROP SCHEMA IF EXISTS "{PATENT_SCHEMA}" CASCADE;')) yield create_schema(PATENT_SCHEMA) with engine.begin() as conn: conn.execute(text(f'DROP SCHEMA {base.SCHEMA} CASCADE;'))
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
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
def table_for_reflection(test_db_name): engine = create_mathesar_engine(test_db_name) schema_name = 'a_new_schema' table_name = 'a_new_table' with engine.begin() as conn: conn.execute(text(f'CREATE SCHEMA {schema_name};')) with engine.begin() as conn: conn.execute( text(f'CREATE TABLE {schema_name}.{table_name}' f' (id INTEGER, name VARCHAR);')) yield schema_name, table_name, engine with engine.begin() as conn: conn.execute(text(f'DROP SCHEMA {schema_name} CASCADE;'))
def reflect_schemas_from_database(database): engine = create_mathesar_engine(database) db_schema_oids = { schema["oid"] for schema in get_mathesar_schemas_with_oids(engine) } database = Database.objects.get(name=database) schemas = [ Schema.objects.get_or_create(oid=oid, database=database) for oid in db_schema_oids ] for schema in Schema.objects.all(): if schema.database.name == database and schema.oid not in db_schema_oids: schema.delete() return schemas
def test_schema_get_with_reflect_new(client, test_db_name): engine = create_mathesar_engine(test_db_name) schema_name = 'a_new_schema' with engine.begin() as conn: conn.execute(text(f'CREATE SCHEMA {schema_name};')) cache.clear() response = client.get('/api/v0/schemas/') # The schema number should only change after the GET request response_data = response.json() actual_created = [ schema for schema in response_data['results'] if schema['name'] == schema_name ] assert len(actual_created) == 1 with engine.begin() as conn: conn.execute(text(f'DROP SCHEMA {schema_name} CASCADE;'))
def create_schema_and_object(name, database): engine = create_mathesar_engine(database) all_schemas = get_mathesar_schemas(engine) if name in all_schemas: raise ValidationError({"name": f"Schema name {name} is not unique"}) try: database_model = Database.objects.get(name=database) except ObjectDoesNotExist: raise ValidationError({"database": f"Database '{database}' not found"}) create_schema(name, engine) schema_oid = get_schema_oid_from_name(name, engine) schema = Schema.objects.create(oid=schema_oid, database=database_model) return schema
def create_db_table_from_data_file(data_file, name, schema): engine = create_mathesar_engine(schema.database.name) sv_filename = data_file.file.path dialect = csv.dialect.SimpleDialect(data_file.delimiter, data_file.quotechar, data_file.escapechar) with open(sv_filename, 'rb') as sv_file: sv_reader = get_sv_reader(sv_file, dialect=dialect) column_names = sv_reader.fieldnames table = tables.create_string_column_table(name=name, schema=schema.name, column_names=column_names, engine=engine) records.create_records_from_csv(table, engine, sv_filename, column_names, delimiter=dialect.delimiter, escape=dialect.escapechar, quote=dialect.quotechar) return table
def check_schema_response(response_schema, schema, schema_name, test_db_name, len_tables=1, check_schema_objects=True): assert response_schema['id'] == schema.id assert response_schema['name'] == schema_name assert response_schema['database'] == test_db_name assert 'has_dependencies' in response_schema assert len(response_schema['tables']) == len_tables if len_tables > 0: response_table = response_schema['tables'][0] assert 'id' in response_table response_table_id = response_table['id'] assert 'name' in response_table assert response_table['url'].startswith('http') assert response_table['url'].endswith( f'/api/v0/tables/{response_table_id}/') if check_schema_objects: assert schema_name in schemas.get_mathesar_schemas( create_mathesar_engine(test_db_name))
def test_schema_get_with_reflect_delete(client, test_db_name): engine = create_mathesar_engine(test_db_name) schema_name = 'a_new_schema' with engine.begin() as conn: conn.execute(text(f'CREATE SCHEMA {schema_name};')) cache.clear() response = client.get('/api/v0/schemas/') response_data = response.json() orig_created = [ schema for schema in response_data['results'] if schema['name'] == schema_name ] assert len(orig_created) == 1 with engine.begin() as conn: conn.execute(text(f'DROP SCHEMA {schema_name};')) cache.clear() response = client.get('/api/v0/schemas/') response_data = response.json() orig_created = [ schema for schema in response_data['results'] if schema['name'] == schema_name ] assert len(orig_created) == 0
def _sa_engine(self): global _engines # We're caching this since the engine is used frequently. if self.name not in _engines: _engines[self.name] = create_mathesar_engine(self.name) return _engines[self.name]