Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop the nodeversion and public columns of DbNode #2937

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# -*- coding: utf-8 -*-
###########################################################################
# Copyright (c), The AiiDA team. All rights reserved. #
# This file is part of the AiiDA code. #
# #
# The code is hosted on GitHub at https://github.com/aiidateam/aiida_core #
# For further information on the license, see the LICENSE.txt file #
# For further information please visit http://www.aiida.net #
###########################################################################
# pylint: disable=invalid-name,too-few-public-methods
"""Drop the columns `nodeversion` and `public` from the `DbNode` model."""
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
from __future__ import absolute_import

# Remove when https://github.com/PyCQA/pylint/issues/1931 is fixed
# pylint: disable=no-name-in-module,import-error,no-member
from django.db import migrations

from aiida.backends.djsite.db.migrations import upgrade_schema_version

REVISION = '1.0.34'
DOWN_REVISION = '1.0.33'


class Migration(migrations.Migration):
"""Drop the columns `nodeversion` and `public` from the `DbNode` model."""

dependencies = [
('db', '0033_replace_text_field_with_json_field'),
]

operations = [
migrations.RemoveField(
model_name='dbnode',
name='nodeversion',
),
migrations.RemoveField(
model_name='dbnode',
name='public',
),
upgrade_schema_version(REVISION, DOWN_REVISION)
]
2 changes: 1 addition & 1 deletion aiida/backends/djsite/db/migrations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class DeserializationException(AiidaException):
pass


LATEST_MIGRATION = '0033_replace_text_field_with_json_field'
LATEST_MIGRATION = '0034_drop_node_columns_nodeversion_public'


def _update_schema_version(version, apps, schema_editor):
Expand Down
13 changes: 2 additions & 11 deletions aiida/backends/djsite/db/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,7 @@ class DbNode(m.Model):
ctime = m.DateTimeField(default=timezone.now, db_index=True, editable=False)
mtime = m.DateTimeField(auto_now=True, db_index=True, editable=False)
# Cannot delete a user if something is associated to it
user = m.ForeignKey(AUTH_USER_MODEL, on_delete=m.PROTECT,
related_name='dbnodes')
user = m.ForeignKey(AUTH_USER_MODEL, on_delete=m.PROTECT, related_name='dbnodes')

# Direct links
outputs = m.ManyToManyField('self', symmetrical=False,
Expand All @@ -166,15 +165,7 @@ class DbNode(m.Model):
# Used only if dbnode is a calculation, or remotedata
# Avoid that computers can be deleted if at least a node exists pointing
# to it.
dbcomputer = m.ForeignKey('DbComputer', null=True, on_delete=m.PROTECT,
related_name='dbnodes')

# Index that is incremented every time a modification is done on itself or on attributes.
# Managed by the aiida.orm.Node class. Do not modify
nodeversion = m.IntegerField(default=1, editable=False)

# For the API: whether this node
public = m.BooleanField(default=False)
dbcomputer = m.ForeignKey('DbComputer', null=True, on_delete=m.PROTECT, related_name='dbnodes')

objects = m.Manager()
# Return aiida Node instances or their subclasses instead of DbNode instances
Expand Down
8 changes: 5 additions & 3 deletions aiida/backends/djsite/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@
# I remove the argument I just read
actual_argv = [actual_argv[0]] + actual_argv[2:]

# Load the general load_dbenv.
from aiida.backends.utils import load_dbenv
load_dbenv(profile=profile_name)
# Load the profile
from aiida.manage.configuration import load_profile
from aiida.manage.manager import get_manager
load_profile(profile_name)
get_manager().get_backend()

execute_from_command_line(actual_argv)
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# -*- coding: utf-8 -*-
"""Drop the columns `nodeversion` and `public` from the `DbNode` model.

Revision ID: 1830c8430131
Revises: 1b8ed3425af9
Create Date: 2019-05-27 15:35:37.404644

"""
# pylint: disable=invalid-name,no-member,import-error,no-name-in-module
from __future__ import division
from __future__ import print_function
from __future__ import absolute_import

import sqlalchemy as sa
from alembic import op

# revision identifiers, used by Alembic.
revision = '1830c8430131'
down_revision = '1b8ed3425af9'
branch_labels = None
depends_on = None


def upgrade():
op.drop_column('db_dbnode', 'nodeversion')
op.drop_column('db_dbnode', 'public')


def downgrade():
op.add_column('db_dbnode', sa.Column('public', sa.BOOLEAN(), autoincrement=False, nullable=True))
op.add_column('db_dbnode', sa.Column('nodeversion', sa.INTEGER(), autoincrement=False, nullable=True))
2 changes: 0 additions & 2 deletions aiida/backends/sqlalchemy/models/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,6 @@ class DbNode(Base):
description = Column(Text(), nullable=True, default='')
ctime = Column(DateTime(timezone=True), default=timezone.now)
mtime = Column(DateTime(timezone=True), default=timezone.now, onupdate=timezone.now)
nodeversion = Column(Integer, default=1)
public = Column(Boolean, default=False)
attributes = Column(JSONB)
extras = Column(JSONB)

Expand Down
4 changes: 0 additions & 4 deletions aiida/backends/sqlalchemy/tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -211,10 +211,6 @@ def do_value_checks(attr_name, original, changed):
for str_attr in ['label', 'description']:
do_value_checks(str_attr, 'original', 'changed')

# Since we already changed an attribute twice, the starting nodeversion is 3 and not 1
do_value_checks('nodeversion', 3, 4)
do_value_checks('public', True, False)

for str_attr in ['ctime', 'mtime']:
do_value_checks(str_attr, timezone.now(), timezone.now())

Expand Down
Binary file modified aiida/backends/tests/fixtures/calcjob/arithmetic.add_old.aiida
Binary file not shown.
7 changes: 1 addition & 6 deletions aiida/backends/tests/orm/implementation/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ def test_creation(self):
self.assertTrue(isinstance(node.ctime, datetime))
self.assertIsNone(node.mtime)
self.assertIsNone(node.process_type)
self.assertEqual(node.public, False)
self.assertEqual(node.version, 1)
self.assertEqual(node.attributes, dict())
self.assertEqual(node.extras, dict())
self.assertEqual(node.node_type, self.node_type)
Expand Down Expand Up @@ -81,8 +79,6 @@ def test_creation(self):
self.assertTrue(isinstance(node.ctime, datetime))
self.assertTrue(isinstance(node.mtime, datetime))
self.assertIsNone(node.process_type)
self.assertEqual(node.public, False)
self.assertEqual(node.version, 1)
self.assertEqual(node.attributes, dict())
self.assertEqual(node.extras, dict())
self.assertEqual(node.node_type, self.node_type)
Expand All @@ -92,11 +88,10 @@ def test_creation(self):
# Try to construct a UUID from the UUID value to prove that it has a valid UUID
UUID(node.uuid)

# Change a column, which should trigger the save, update the mtime and version, but leave the ctime untouched
# Change a column, which should trigger the save, update the mtime but leave the ctime untouched
node.label = 'test'
self.assertEqual(node.ctime, node_ctime)
self.assertTrue(node.mtime > node_mtime)
self.assertEqual(node.version, 2)

def test_creation_with_time(self):
"""
Expand Down
57 changes: 0 additions & 57 deletions aiida/backends/tests/test_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -949,44 +949,6 @@ def test_attr_listing(self):

self.assertEquals(a.extras, all_extras)

def test_versioning(self):
"""
Test the versioning of the node when setting attributes and extras
"""
a = orm.Data()
attrs_to_set = {
'bool': self.boolval,
'integer': self.intval,
'float': self.floatval,
'string': self.stringval,
'dict': self.dictval,
'list': self.listval,
}

for key, value in attrs_to_set.items():
a.set_attribute(key, value)
self.assertEquals(a.get_attribute(key), value)

a.store()

# Check after storing
for key, value in attrs_to_set.items():
self.assertEquals(a.get_attribute(key), value)

# Even if I stored many attributes, this should stay at 1
self.assertEquals(a.version, 1)

# I check increment on new version
a.set_extra('a', 'b')
self.assertEquals(a.version, 2)

# In both cases, the node version must increase
a.label = 'test'
self.assertEquals(a.version, 3)

a.description = 'test description'
self.assertEquals(a.version, 4)

def test_delete_extras(self):
"""
Checks the ability of deleting extras, also when they are dictionaries
Expand Down Expand Up @@ -1145,25 +1107,6 @@ def test_basetype_as_extra(self):
self.assertEqual(n.get_extra('a')['b'][0], "sometext3")
self.assertIsInstance(n.get_extra('a')['b'][0], six.string_types)

def test_versioning_lowlevel(self):
"""
Checks the versioning.
"""
a = orm.Data()
a.store()

# Even if I stored many attributes, this should stay at 1
self.assertEquals(a.version, 1)

a.label = "label1"
a.label = "label2"
self.assertEquals(a.version, 3)

a.description = "desc1"
a.description = "desc2"
a.description = "desc3"
self.assertEquals(a.version, 6)

def test_comments(self):
# This is the best way to compare dates with the stored ones, instead
# of directly loading datetime.datetime.now(), or you can get a
Expand Down
4 changes: 1 addition & 3 deletions aiida/orm/implementation/django/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,9 +173,7 @@ def _(dbmodel, backend):
label=dbmodel.label,
description=dbmodel.description,
dbcomputer_id=dbmodel.dbcomputer_id,
user_id=dbmodel.user_id,
public=dbmodel.public,
nodeversion=dbmodel.nodeversion)
user_id=dbmodel.user_id)

from . import nodes
return nodes.DjangoNode.from_dbmodel(djnode_instance, backend)
Expand Down
4 changes: 0 additions & 4 deletions aiida/orm/implementation/django/dummy_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,10 +160,6 @@ class DbNode(Base):
user_id = Column(Integer, ForeignKey('db_dbuser.id', deferrable=True, initially="DEFERRED"), nullable=False)
user = relationship('DbUser', backref='dbnodes')

public = Column(Boolean, default=False)

nodeversion = Column(Integer, default=1)

attributes = relationship('DbAttribute', uselist=True, backref='dbnode')
extras = relationship('DbExtra', uselist=True, backref='dbnode')

Expand Down
14 changes: 1 addition & 13 deletions aiida/orm/implementation/django/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,6 @@ def set_attribute(self, key, value):
:param value: value of the attribute
"""
self.ATTRIBUTE_CLASS.set_value_for_node(self.dbmodel, key, value)
self._increment_version_number()

def set_attributes(self, attributes):
"""Set attributes.
Expand All @@ -187,7 +186,6 @@ def set_attributes(self, attributes):
"""
for key, value in attributes.items():
self.ATTRIBUTE_CLASS.set_value_for_node(self.dbmodel, key, value)
self._increment_version_number()

def reset_attributes(self, attributes):
"""Reset the attributes.
Expand All @@ -197,7 +195,6 @@ def reset_attributes(self, attributes):
:param attributes: the new attributes to set
"""
self.ATTRIBUTE_CLASS.reset_values_for_node(self.dbmodel, attributes)
self._increment_version_number()

def delete_attribute(self, key):
"""Delete an attribute.
Expand Down Expand Up @@ -262,16 +259,13 @@ def get_extras(self, keys):
"""
raise NotImplementedError

def set_extra(self, key, value, increase_version=True):
def set_extra(self, key, value):
"""Set an extra to the given value.

:param key: name of the extra
:param value: value of the extra
:param increase_version: boolean, if True will increase the node version upon successfully setting the extra
"""
self.EXTRA_CLASS.set_value_for_node(self.dbmodel, key, value)
if increase_version:
self._increment_version_number()

def set_extras(self, extras):
"""Set extras.
Expand All @@ -282,7 +276,6 @@ def set_extras(self, extras):
"""
for key, value in extras.items():
self.EXTRA_CLASS.set_value_for_node(self.dbmodel, key, value)
self._increment_version_number()

def reset_extras(self, extras):
"""Reset the extras.
Expand Down Expand Up @@ -398,11 +391,6 @@ def store(self, attributes=None, links=None, with_transaction=True):

return self

def _increment_version_number(self):
"""Increment the node version number of this node by one directly in the database."""
self._dbmodel.nodeversion = self.version + 1
self._dbmodel.save()


class DjangoNodeCollection(BackendNodeCollection):
"""The collection of Node entries."""
Expand Down
Loading