#190 Fixing the models relationship
Merged 7 years ago by skrzepto. Opened 7 years ago by skrzepto.
skrzepto/fedora-hubs delete-user-fix  into  develop

file modified
+3 -2
@@ -108,7 +108,7 @@ 

      role = sa.Column(sa.Enum(*roles), primary_key=True)

  

      user = relation("User", backref=backref('associations', cascade="all, delete-orphan"))

-     hub = relation("Hub", backref="associations")

+     hub = relation("Hub", backref=backref('associations', cascade="all, delete-orphan"))

  

      @classmethod

      def get(cls, session, hub, user, role):
@@ -124,7 +124,8 @@ 

      name = sa.Column(sa.String(50), primary_key=True)

      summary = sa.Column(sa.String(128))

      created_on = sa.Column(sa.DateTime, default=datetime.datetime.utcnow)

-     widgets = relation('Widget', backref=backref('hub'))

+     widgets = relation('Widget', cascade='all,delete-orphan', single_parent=True,

+                        backref=backref('hub', cascade='all'))

      left_width = sa.Column(sa.Integer, nullable=False, default=8)

      archived = sa.Column(sa.Boolean, default=False)

      user_hub = sa.Column(sa.Boolean, default=False)

when a hub is deleted, delete the association and also all its widgets are deleted as well.

assumption is that a widget has one hub parent

used this script to verify

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#!/usr/bin/env python
""" Delete a hub from the db.

Useful for testing what happens when you recreate them.
"""

import fedmsg.config
fedmsg_config = fedmsg.config.load_config()

import hubs.models

session = hubs.models.init(fedmsg_config['hubs.sqlalchemy.uri'])

hubs_name = raw_input('What hub do you want to delete: ')
print "Looking for hubs %r" % hubs_name

hub = hubs.models.Hub.get(session, hubs_name)
if not hub:
    print "No such hub %r" % hubs_name
else:
    print "Found %r.  Deleting." % hub
    session.delete(hub)

session.commit()

This one might have to be split at 80 characters

One small change, after that :thumbsup: for me

rebased

7 years ago

updated the line and squashed the commits

Pull-Request has been merged by skrzepto

7 years ago
Metadata