<commit_before>package org.umlg.sqlg.test.topology;

import org.apache.commons.configuration2.builder.fluent.Configurations;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.lang3.time.StopWatch;
import org.apache.tinkerpop.gremlin.process.traversal.Order;
import org.apache.tinkerpop.gremlin.process.traversal.P;
import org.apache.tinkerpop.gremlin.process.traversal.Scope;
import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.junit.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.umlg.sqlg.structure.PropertyType;
import org.umlg.sqlg.structure.SqlgGraph;
import org.umlg.sqlg.structure.topology.EdgeLabel;
import org.umlg.sqlg.structure.topology.PropertyColumn;
import org.umlg.sqlg.structure.topology.Schema;
import org.umlg.sqlg.structure.topology.VertexLabel;
import org.umlg.sqlg.test.BaseTest;
import org.umlg.sqlg.util.SqlgUtil;

import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

@SuppressWarnings("rawtypes")
public class TestForeignSchema extends BaseTest {

    private final static Logger LOGGER = LoggerFactory.getLogger(TestForeignSchema.class);
    private SqlgGraph sqlgGraphFdw;

    @BeforeClass
    public static void beforeClass() {
        URL sqlProperties = Thread.currentThread().getContextClassLoader().getResource("sqlg.properties");
        try {
            Configurations configs = new Configurations();
            configuration = configs.properties(sqlProperties);
            Assume.assumeTrue(isPostgres());
            configuration.addProperty("distributed", true);
            if (!configuration.containsKey("jdbc.url"))
                throw new IllegalArgumentException(String.format("SqlGraph configuration requires that the %s be set", "jdbc.url"));

        } catch (ConfigurationException e) {
            throw new RuntimeException(e);
        }
    }

    @AfterClass
    public static void afterClass() throws SQLException {
        if (isPostgres()) {
            URL sqlProperties = Thread.currentThread().getContextClassLoader().getResource("sqlg.properties");
            try {
                Configurations configs = new Configurations();
                configuration = configs.properties(sqlProperties);
                SqlgGraph sqlgGraph = SqlgGraph.open(configuration);
                Connection connection = sqlgGraph.tx().getConnection();
                try (Statement statement = connection.createStatement()) {
                    statement.execute("DROP SERVER IF EXISTS sqlgraph_fwd_server CASCADE;");
                }
                sqlgGraph.tx().commit();
            } catch (ConfigurationException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Before
    public void before() throws Exception {
        Assume.assumeTrue(isPostgres());
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        this.sqlgGraph = SqlgGraph.open(configuration);
        SqlgUtil.dropDb(this.sqlgGraph);
        Connection connection = this.sqlgGraph.tx().getConnection();
        try (Statement statement = connection.createStatement()) {
            for (String table : List.of("V_A", "V_B", "E_ab", "E_ba",
                    "V_person", "V_software", "E_created", "E_knows",
                    "V_song", "V_artist", "E_followedBy", "E_writtenBy", "E_sungBy")) {
                String sql = String.format(
                        "DROP FOREIGN TABLE IF EXISTS \"%s\" CASCADE;",
                        table
                );
                statement.execute(sql);
            }
            String sql = String.format(
                    "DROP SCHEMA IF EXISTS \"%s\" CASCADE;",
                    "B"
            );
            statement.execute(sql);
        }
        this.sqlgGraph.tx().commit();
        this.sqlgGraph.tx().commit();
        this.sqlgGraph.close();
        this.sqlgGraph = SqlgGraph.open(configuration);

        this.sqlgGraphFdw = SqlgGraph.open("sqlg.fdw.properties");
        SqlgUtil.dropDb(this.sqlgGraphFdw);
        this.sqlgGraphFdw.tx().commit();
        this.sqlgGraphFdw.close();
        this.sqlgGraphFdw = SqlgGraph.open("sqlg.fdw.properties");


        grantReadOnlyUserPrivileges();
        assertNotNull(this.sqlgGraph);
        assertNotNull(this.sqlgGraph.getBuildVersion());
        this.gt = this.sqlgGraph.traversal();
        if (configuration.getBoolean("distributed", false)) {
            this.sqlgGraph1 = SqlgGraph.open(configuration);
            assertNotNull(this.sqlgGraph1);
            assertEquals(this.sqlgGraph.getBuildVersion(), this.sqlgGraph1.getBuildVersion());
        }
        stopWatch.stop();
        LOGGER.info("Startup time for test = " + stopWatch);
        connection = this.sqlgGraph.tx().getConnection();
        try (Statement statement = connection.createStatement()) {
            statement.execute("CREATE EXTENSION IF NOT EXISTS postgres_fdw;");
            String sql = String.format(
                    "CREATE SERVER \"%s\" FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host '%s', dbname '%s', port '%d');",
                    "sqlgraph_fwd_server",
                    "localhost",
                    "sqlgraphdb_fdw",
                    5432
            );
            statement.execute(sql);
            this.sqlgGraph.tx().commit();
        } catch (Exception e) {
            //swallow<commit_msg>added temporary logging to see why teamcity tests are failing<commit_after>package org.umlg.sqlg.test.topology;

import org.apache.commons.configuration2.builder.fluent.Configurations;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.apache.commons.lang3.time.StopWatch;
import org.apache.tinkerpop.gremlin.process.traversal.Order;
import org.apache.tinkerpop.gremlin.process.traversal.P;
import org.apache.tinkerpop.gremlin.process.traversal.Scope;
import org.apache.tinkerpop.gremlin.process.traversal.Traversal;
import org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.__;
import org.apache.tinkerpop.gremlin.structure.Edge;
import org.apache.tinkerpop.gremlin.structure.T;
import org.apache.tinkerpop.gremlin.structure.Vertex;
import org.junit.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.umlg.sqlg.structure.PropertyType;
import org.umlg.sqlg.structure.SqlgGraph;
import org.umlg.sqlg.structure.topology.EdgeLabel;
import org.umlg.sqlg.structure.topology.PropertyColumn;
import org.umlg.sqlg.structure.topology.Schema;
import org.umlg.sqlg.structure.topology.VertexLabel;
import org.umlg.sqlg.test.BaseTest;
import org.umlg.sqlg.util.SqlgUtil;

import java.net.URL;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.*;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

@SuppressWarnings("rawtypes")
public class TestForeignSchema extends BaseTest {

    private final static Logger LOGGER = LoggerFactory.getLogger(TestForeignSchema.class);
    private SqlgGraph sqlgGraphFdw;

    @BeforeClass
    public static void beforeClass() {
        URL sqlProperties = Thread.currentThread().getContextClassLoader().getResource("sqlg.properties");
        try {
            Configurations configs = new Configurations();
            configuration = configs.properties(sqlProperties);
            Assume.assumeTrue(isPostgres());
            configuration.addProperty("distributed", true);
            if (!configuration.containsKey("jdbc.url"))
                throw new IllegalArgumentException(String.format("SqlGraph configuration requires that the %s be set", "jdbc.url"));

        } catch (ConfigurationException e) {
            throw new RuntimeException(e);
        }
    }

    @AfterClass
    public static void afterClass() throws SQLException {
        if (isPostgres()) {
            URL sqlProperties = Thread.currentThread().getContextClassLoader().getResource("sqlg.properties");
            try {
                Configurations configs = new Configurations();
                configuration = configs.properties(sqlProperties);
                SqlgGraph sqlgGraph = SqlgGraph.open(configuration);
                Connection connection = sqlgGraph.tx().getConnection();
                try (Statement statement = connection.createStatement()) {
                    statement.execute("DROP SERVER IF EXISTS sqlgraph_fwd_server CASCADE;");
                }
                sqlgGraph.tx().commit();
            } catch (ConfigurationException e) {
                throw new RuntimeException(e);
            }
        }
    }

    @Before
    public void before() throws Exception {
        Assume.assumeTrue(isPostgres());
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        this.sqlgGraph = SqlgGraph.open(configuration);
        SqlgUtil.dropDb(this.sqlgGraph);
        Connection connection = this.sqlgGraph.tx().getConnection();
        try (Statement statement = connection.createStatement()) {
            for (String table : List.of("V_A", "V_B", "E_ab", "E_ba",
                    "V_person", "V_software", "E_created", "E_knows",
                    "V_song", "V_artist", "E_followedBy", "E_writtenBy", "E_sungBy")) {
                String sql = String.format(
                        "DROP FOREIGN TABLE IF EXISTS \"%s\" CASCADE;",
                        table
                );
                statement.execute(sql);
            }
            String sql = String.format(
                    "DROP SCHEMA IF EXISTS \"%s\" CASCADE;",
                    "B"
            );
            statement.execute(sql);
        }
        this.sqlgGraph.tx().commit();
        this.sqlgGraph.tx().commit();
        this.sqlgGraph.close();
        this.sqlgGraph = SqlgGraph.open(configuration);

        this.sqlgGraphFdw = SqlgGraph.open("sqlg.fdw.properties");
        SqlgUtil.dropDb(this.sqlgGraphFdw);
        this.sqlgGraphFdw.tx().commit();
        this.sqlgGraphFdw.close();
        this.sqlgGraphFdw = SqlgGraph.open("sqlg.fdw.properties");


        grantReadOnlyUserPrivileges();
        assertNotNull(this.sqlgGraph);
        assertNotNull(this.sqlgGraph.getBuildVersion());
        this.gt = this.sqlgGraph.traversal();
        if (configuration.getBoolean("distributed", false)) {
            this.sqlgGraph1 = SqlgGraph.open(configuration);
            assertNotNull(this.sqlgGraph1);
            assertEquals(this.sqlgGraph.getBuildVersion(), this.sqlgGraph1.getBuildVersion());
        }
        stopWatch.stop();
        LOGGER.info("Startup time for test = " + stopWatch);
        connection = this.sqlgGraph.tx().getConnection();
        try (Statement statement = connection.createStatement()) {
            statement.execute("CREATE EXTENSION IF NOT EXISTS postgres_fdw;");
            String sql = String.format(
                    "CREATE SERVER \"%s\" FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host '%s', dbname '%s', port '%d');",
                    "sqlgraph_fwd_server",
                    "localhost",
                    "sqlgraphdb_fdw",
                    5432
            );
            statement.execute(sql);
            this.sqlgGraph.tx().commit();
        } catch (Exception e) {
            //swallow
            LOGGER.error("Failed to create 'sqlgraph_fwd_server'", e);
{'fraction_non_alphanumeric': 0.11344430217669654, 'fraction_numerical': 0.00204865556978233, 'mean_word_length': 2.5850673194614444, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2499406', 'n_tokens_mistral': 3338, 'n_tokens_neox': 3213, 'n_words': 587}