micronaut-projects / micronaut-test-resources

Test resources support
Apache License 2.0
8 stars 17 forks source link

Create Intellij IDEA datasource configurations when spinning a database container #635

Open alvarosanchez opened 5 months ago

alvarosanchez commented 5 months ago

Intellij IDEA stores datasource connections locally. It would be super cool if Test Resources could have an opt-in feature to easily add them when spinning up a database container. Options that come to my mind described below.

Copy datasource settings to a build/target file, to the clipboard, or to the system output

Intellij already allows copy/pasting datasources. When you copy, what it pastes is something like this:

#DataSourceSettings#
#LocalDataSource: postgres@localhost
#BEGIN#
<data-source source="LOCAL" name="postgres@localhost" uuid="2593b01a-b2ef-470e-b90c-51237ea640c0"><database-info product="" version="" jdbc-version="" driver-name="" driver-version="" dbms="POSTGRES"/><driver-ref>postgresql</driver-ref><synchronize>true</synchronize><jdbc-driver>org.postgresql.Driver</jdbc-driver><jdbc-url>jdbc:postgresql://localhost:5432/postgres</jdbc-url><jdbc-additional-properties><property name="com.intellij.clouds.kubernetes.db.host.port"/><property name="com.intellij.clouds.kubernetes.db.enabled" value="false"/><property name="com.intellij.clouds.kubernetes.db.container.port"/></jdbc-additional-properties><secret-storage>master_key</secret-storage><schema-mapping/><working-dir>$ProjectFileDir$</working-dir></data-source>
#END#

We could dump this to a file, to the system out, or even copy to the clipboard so that I can paste it and start doing queries.

Add the datasource directly to .idea files.

Datasources are stored internally in these files (the files can contain multiple <data-source> tags):

<!-- .idea/dataSources.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="DataSourceManagerImpl" format="xml" multifile-model="true">
    <data-source source="LOCAL" name="postgres@localhost" uuid="2593b01a-b2ef-470e-b90c-51237ea640c0">
      <driver-ref>postgresql</driver-ref>
      <synchronize>true</synchronize>
      <jdbc-driver>org.postgresql.Driver</jdbc-driver>
      <jdbc-url>jdbc:postgresql://localhost:5432/postgres</jdbc-url>
      <jdbc-additional-properties>
        <property name="com.intellij.clouds.kubernetes.db.host.port" />
        <property name="com.intellij.clouds.kubernetes.db.enabled" value="false" />
        <property name="com.intellij.clouds.kubernetes.db.container.port" />
      </jdbc-additional-properties>
      <working-dir>$ProjectFileDir$</working-dir>
    </data-source>
  </component>
</project>
<!-- .idea/dataSources.local.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
  <component name="dataSourceStorageLocal" created-in="IU-241.17011.79">
    <data-source name="postgres@localhost" uuid="2593b01a-b2ef-470e-b90c-51237ea640c0">
      <database-info product="" version="" jdbc-version="" driver-name="" driver-version="" dbms="POSTGRES" />
      <secret-storage>master_key</secret-storage>
      <schema-mapping />
    </data-source>
  </component>
</project>

It would be ✨magical if we could automatically add/remove these entries

graemerocher commented 5 months ago

sweet idea