Skip to content

Commit

Permalink
Merge pull request #2279 from sumanau7/add_dbt_plugin_version
Browse files Browse the repository at this point in the history
Add dbt plugin version
  • Loading branch information
beckjake authored Apr 6, 2020
2 parents 6a26a5f + f5697fd commit 2c24102
Show file tree
Hide file tree
Showing 7 changed files with 111 additions and 19 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
- Added timeout to registry download call ([#2195](https:/fishtown-analytics/dbt/issues/2195), [#2228](https:/fishtown-analytics/dbt/pull/2228))
- When a macro is called with invalid arguments, include the calling model in the output ([#2073](https:/fishtown-analytics/dbt/issues/2073), [#2238](https:/fishtown-analytics/dbt/pull/2238))
- When a warn exception is not in a jinja do block, return an empty string instead of None ([#2222](https:/fishtown-analytics/dbt/issues/2222), [#2259](https:/fishtown-analytics/dbt/pull/2259))
- Add dbt plugin versions to --version([#2272](https:/fishtown-analytics/dbt/issues/2272), [#2279](https:/fishtown-analytics/dbt/pull/2279))

Contributors:
- [@raalsky](https:/Raalsky) ([#2224](https:/fishtown-analytics/dbt/pull/2224), [#2228](https:/fishtown-analytics/dbt/pull/2228))
- [@ilkinulas](https:/ilkinulas) [#2199](https:/fishtown-analytics/dbt/pull/2199)
- [@kyleabeauchamp](https:/kyleabeauchamp) [#2262](https:/fishtown-analytics/dbt/pull/2262)
- [@jeremyyeo](https:/jeremyyeo) [#2259](https:/fishtown-analytics/dbt/pull/2259)
- [@sumanau7](https:/sumanau7) [#2279](https:/fishtown-analytics/dbt/pull/2279)

## dbt 0.16.0 (March 23, 2020)

Expand Down
35 changes: 29 additions & 6 deletions core/dbt/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import importlib
import os
import json

import requests
Expand Down Expand Up @@ -37,23 +39,44 @@ def get_version_information():
version_msg = ("installed version: {}\n"
" latest version: {}\n\n".format(installed_s, latest_s))

plugin_version_msg = "Plugins:\n"
for plugin_name, version in _get_dbt_plugins_info():
plugin_version_msg += ' - {plugin_name}: {version}\n'.format(
plugin_name=plugin_name, version=version
)
if latest is None:
return ("{}The latest version of dbt could not be determined!\n"
"Make sure that the following URL is accessible:\n{}"
.format(version_msg, PYPI_VERSION_URL))
"Make sure that the following URL is accessible:\n{}\n\n{}"
.format(version_msg, PYPI_VERSION_URL, plugin_version_msg))

if installed == latest:
return "{}Up to date!".format(version_msg)
return "{}Up to date!\n\n{}".format(version_msg, plugin_version_msg)

elif installed > latest:
return ("{}Your version of dbt is ahead of the latest "
"release!".format(version_msg))
"release!\n\n{}".format(version_msg, plugin_version_msg))

else:
return ("{}Your version of dbt is out of date! "
"You can find instructions for upgrading here:\n"
"https://docs.getdbt.com/docs/installation"
.format(version_msg))
"https://docs.getdbt.com/docs/installation\n\n{}"
.format(version_msg, plugin_version_msg))


def _get_dbt_plugins_info():
for path in importlib.util.find_spec('dbt').submodule_search_locations:
plugin_root, _ = os.path.split(path)
_, plugin_name = os.path.split(plugin_root)
if plugin_name == 'core':
continue
try:
mod = importlib.import_module(
f'dbt.adapters.{plugin_name}.__version__'
)
except ImportError:
# not an adpater
continue
yield plugin_name, mod.version


__version__ = '0.17.0a1'
Expand Down
1 change: 1 addition & 0 deletions plugins/bigquery/dbt/adapters/bigquery/__version__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = '0.17.0a1'
1 change: 1 addition & 0 deletions plugins/postgres/dbt/adapters/postgres/__version__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = '0.17.0a1'
1 change: 1 addition & 0 deletions plugins/redshift/dbt/adapters/redshift/__version__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = '0.17.0a1'
1 change: 1 addition & 0 deletions plugins/snowflake/dbt/adapters/snowflake/__version__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
version = '0.17.0a1'
89 changes: 76 additions & 13 deletions test/unit/test_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,85 @@

import dbt.main
import dbt.version
import sys


class VersionTest(unittest.TestCase):

@patch("dbt.version.__version__", "0.10.0")
@patch('dbt.version._get_dbt_plugins_info', autospec=True)
@patch('dbt.version.requests.get')
def test_versions_equal(self, mock_get):
mock_get.return_value.json.return_value = {'info': {'version': '0.10.0'}}
def test_versions_equal(self, mock_get, mock_get_dbt_plugins_info):
mock_get.return_value.json.return_value = {
'info': {'version': '0.10.0'}
}
mock_get_dbt_plugins_info.return_value = [
('dbt-postgres', '0.10.0'),
('dbt-redshift', '0.10.0'),
('dbt-bigquery', '0.10.0'),
('dbt-snowflake', '0.10.0')
]

latest_version = dbt.version.get_latest_version()
installed_version = dbt.version.get_installed_version()
version_information = dbt.version.get_version_information()

expected_version_information = "installed version: 0.10.0\n" \
" latest version: 0.10.0\n\n" \
"Up to date!"
"Up to date!\n\n" \
"Plugins:\n" \
" - dbt-postgres: 0.10.0\n" \
" - dbt-redshift: 0.10.0\n" \
" - dbt-bigquery: 0.10.0\n" \
" - dbt-snowflake: 0.10.0\n"

self.assertEqual(latest_version, installed_version)
self.assertEqual(latest_version, installed_version)
self.assertMultiLineEqual(version_information,
expected_version_information)

@patch("dbt.version.__version__", "0.10.2-a1")
@patch('dbt.version._get_dbt_plugins_info', autospec=True)
@patch('dbt.version.requests.get')
def test_installed_version_greater(self, mock_get):
mock_get.return_value.json.return_value = {'info': {'version': '0.10.1'}}

def test_installed_version_greater(self, mock_get, mock_get_dbt_plugins_info):
mock_get.return_value.json.return_value = {
'info': {'version': '0.10.1'}
}
mock_get_dbt_plugins_info.return_value = [
('dbt-postgres', '0.10.0'),
('dbt-redshift', '0.10.0'),
('dbt-bigquery', '0.10.0'),
('dbt-snowflake', '0.10.0')
]
latest_version = dbt.version.get_latest_version()
installed_version = dbt.version.get_installed_version()
version_information = dbt.version.get_version_information()

expected_version_information = "installed version: 0.10.2-a1\n" \
" latest version: 0.10.1\n\n" \
"Your version of dbt is ahead of the latest release!"
"Your version of dbt is ahead of the latest release!\n\n" \
"Plugins:\n" \
" - dbt-postgres: 0.10.0\n" \
" - dbt-redshift: 0.10.0\n" \
" - dbt-bigquery: 0.10.0\n" \
" - dbt-snowflake: 0.10.0\n"

assert installed_version > latest_version
self.assertMultiLineEqual(version_information,
expected_version_information)

@patch("dbt.version.__version__", "0.9.5")
@patch('dbt.version._get_dbt_plugins_info', autospec=True)
@patch('dbt.version.requests.get')
def test_installed_version_lower(self, mock_get):
mock_get.return_value.json.return_value = {'info': {'version': '0.10.0'}}

def test_installed_version_lower(self, mock_get, mock_get_dbt_plugins_info):
mock_get.return_value.json.return_value = {
'info': {'version': '0.10.0'}
}
mock_get_dbt_plugins_info.return_value = [
('dbt-postgres', '0.10.0'),
('dbt-redshift', '0.10.0'),
('dbt-bigquery', '0.10.0'),
('dbt-snowflake', '0.10.0')
]
latest_version = dbt.version.get_latest_version()
installed_version = dbt.version.get_installed_version()
version_information = dbt.version.get_version_information()
Expand All @@ -56,7 +90,12 @@ def test_installed_version_lower(self, mock_get):
" latest version: 0.10.0\n\n" \
"Your version of dbt is out of date! " \
"You can find instructions for upgrading here:\n" \
"https://docs.getdbt.com/docs/installation"
"https://docs.getdbt.com/docs/installation\n\n" \
"Plugins:\n" \
" - dbt-postgres: 0.10.0\n" \
" - dbt-redshift: 0.10.0\n" \
" - dbt-bigquery: 0.10.0\n" \
" - dbt-snowflake: 0.10.0\n"

assert installed_version < latest_version
self.assertMultiLineEqual(version_information,
Expand All @@ -66,8 +105,32 @@ def test_installed_version_lower(self, mock_get):
@patch('sys.stderr')
@patch('dbt.version.requests.get')
def test_dbt_version_flag(self, mock_get, stderr):
mock_get.return_value.json.return_value = {'info': {'version': '0.10.1'}}
mock_get.return_value.json.return_value = {
'info': {'version': '0.10.1'}
}

with self.assertRaises(SystemExit) as exc:
dbt.main.handle_and_check(['--version'])
self.assertEqual(exc.exception.code, 0)

@patch('importlib.util.find_spec', autospec=True)
@patch('importlib.import_module', autospec=True)
def test_get_dbt_plugins_info_with_version_info(
self, mock_mod, mock_find_spec
):
mock_submodule = unittest.mock.Mock()
mock_find_spec.return_value = mock_submodule
mock_submodule.submodule_search_locations = [
'/tmp/dbt/plugins/postgres/dbt', '/tmp/dbt/plugins/snowflake/dbt']
mod_version = unittest.mock.Mock()
mock_mod.return_value = mod_version
mod_version.version = '1.0'
self.assertEqual(
list(dbt.version._get_dbt_plugins_info()),
[('postgres', '1.0'), ('snowflake', '1.0')]
)
mock_find_spec.assert_called_once_with('dbt')
mock_mod.assert_has_calls([
unittest.mock.call('dbt.adapters.postgres.__version__'),
unittest.mock.call('dbt.adapters.snowflake.__version__')
])

0 comments on commit 2c24102

Please sign in to comment.