|
| 1 | +package io.ebean.test.containers; |
| 2 | + |
| 3 | +import io.ebean.Database; |
| 4 | +import io.ebean.datasource.DataSourcePool; |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import java.sql.Connection; |
| 8 | +import java.sql.PreparedStatement; |
| 9 | +import java.sql.SQLException; |
| 10 | +import java.util.HashSet; |
| 11 | + |
| 12 | +import com.pgvector.PGvector; |
| 13 | + |
| 14 | +import static org.assertj.core.api.Assertions.assertThat; |
| 15 | + |
| 16 | +public class PGvectorContainerTest { |
| 17 | + private final HashSet<Connection> connections = new HashSet<>(); |
| 18 | + |
| 19 | + /** |
| 20 | + * Helper function to register the PGvector types only once per connection. |
| 21 | + * @param connection the connection |
| 22 | + * @return the same connection |
| 23 | + * @throws SQLException if an error occurs |
| 24 | + */ |
| 25 | + private Connection wrapConnection(Connection connection) throws SQLException { |
| 26 | + if(connections.add(connection)) { |
| 27 | + PGvector.registerTypes(connection); |
| 28 | + } |
| 29 | + return connection; |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void extraDb() throws java.sql.SQLException { |
| 34 | + PGvectorContainer container = PGvectorContainer.builder("pg18") |
| 35 | + .port(0) |
| 36 | + .extraDb("myextra") |
| 37 | + .build(); |
| 38 | + |
| 39 | + container.startMaybe(); |
| 40 | + assertThat(container.port()).isGreaterThan(0); |
| 41 | + |
| 42 | + ContainerConfig containerConfig = container.config(); |
| 43 | + assertThat(containerConfig.port()).isEqualTo(container.port()); |
| 44 | + |
| 45 | + String jdbcUrl = container.config().jdbcUrl(); |
| 46 | + assertThat(jdbcUrl).contains(":" + containerConfig.port()); |
| 47 | + runSomeSql(container); |
| 48 | + |
| 49 | + DataSourcePool dataSource = container.ebean().dataSourceBuilder().build(); |
| 50 | + try (Connection connection = wrapConnection(dataSource.getConnection())) { |
| 51 | + exeSql(connection, "INSERT INTO items (embedding) values ('[7,8,9]')"); |
| 52 | + } |
| 53 | + dataSource.shutdown(); |
| 54 | + |
| 55 | + Database ebean = container.ebean().builder() |
| 56 | + .register(false) |
| 57 | + .defaultDatabase(false) |
| 58 | + .build(); |
| 59 | + // This can't be done yet, because Ebean doesn't know about PGvector type |
| 60 | +// ebean.sqlUpdate("insert into items (embedding) values (?)") |
| 61 | +// .setParameter(new PGvector(new float[] { 10f, 11f, 12f })) |
| 62 | +// .execute(); |
| 63 | + |
| 64 | + ebean.shutdown(); |
| 65 | + } |
| 66 | + |
| 67 | + private void runSomeSql(PGvectorContainer container) { |
| 68 | + try { |
| 69 | + Connection connection = wrapConnection(container.createConnection()); |
| 70 | + exeSql(connection, "CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3))"); |
| 71 | + exeSql(connection, "INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]')"); |
| 72 | + } catch (SQLException e) { |
| 73 | + throw new RuntimeException(e); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + private static void exeSql(Connection connection, String sql) throws SQLException { |
| 78 | + try (PreparedStatement st = connection.prepareStatement(sql)) { |
| 79 | + st.execute(); |
| 80 | + } |
| 81 | + } |
| 82 | +} |
0 commit comments