()
| 58 | |
| 59 | |
| 60 | def main(): |
| 61 | connection.default() |
| 62 | |
| 63 | # Management functions would normally be used in development, and possibly for deployments. |
| 64 | # They are typically not part of a core application. |
| 65 | log.info("### creating keyspace...") |
| 66 | management.create_keyspace_simple(KEYSPACE, 1) |
| 67 | log.info("### syncing model...") |
| 68 | management.sync_table(FamilyMembers) |
| 69 | |
| 70 | # default uuid is assigned |
| 71 | simmons = FamilyMembers.create(surname='Simmons', name='Gene', birth_year=1949, sex='m') |
| 72 | |
| 73 | # add members to his family later |
| 74 | FamilyMembers.create(id=simmons.id, surname='Simmons', name='Nick', birth_year=1989, sex='m') |
| 75 | sophie = FamilyMembers.create(id=simmons.id, surname='Simmons', name='Sophie', sex='f') |
| 76 | |
| 77 | nick = FamilyMembers.objects(id=simmons.id, surname='Simmons', name='Nick') |
| 78 | try: |
| 79 | nick.iff(birth_year=1988).update(birth_year=1989) |
| 80 | except LWTException: |
| 81 | print("precondition not met") |
| 82 | |
| 83 | log.info("### setting individual column to NULL by updating it to None") |
| 84 | nick.update(birth_year=None) |
| 85 | |
| 86 | # showing validation |
| 87 | try: |
| 88 | FamilyMembers.create(id=simmons.id, surname='Tweed', name='Shannon', birth_year=1957, sex='f') |
| 89 | except ValidationError: |
| 90 | log.exception('INTENTIONAL VALIDATION EXCEPTION; Failed creating instance:') |
| 91 | FamilyMembers.create(id=simmons.id, surname='Tweed', name='Shannon', sex='f') |
| 92 | |
| 93 | log.info("### add multiple as part of a batch") |
| 94 | # If creating many at one time, can use a batch to minimize round-trips |
| 95 | hogan_id = uuid4() |
| 96 | with BatchQuery() as b: |
| 97 | FamilyMembers.batch(b).create(id=hogan_id, surname='Hogan', name='Hulk', sex='m') |
| 98 | FamilyMembers.batch(b).create(id=hogan_id, surname='Hogan', name='Linda', sex='f') |
| 99 | FamilyMembers.batch(b).create(id=hogan_id, surname='Hogan', name='Nick', sex='m') |
| 100 | FamilyMembers.batch(b).create(id=hogan_id, surname='Hogan', name='Brooke', sex='f') |
| 101 | |
| 102 | log.info("### All members") |
| 103 | for m in FamilyMembers.all(): |
| 104 | print(m, m.birth_year, m.sex) |
| 105 | |
| 106 | log.info("### Select by partition key") |
| 107 | for m in FamilyMembers.objects(id=simmons.id): |
| 108 | print(m, m.birth_year, m.sex) |
| 109 | |
| 110 | log.info("### Constrain on clustering key") |
| 111 | for m in FamilyMembers.objects(id=simmons.id, surname=simmons.surname): |
| 112 | print(m, m.birth_year, m.sex) |
| 113 | |
| 114 | log.info("### Constrain on clustering key") |
| 115 | kids = FamilyMembers.objects(id=simmons.id, surname=simmons.surname, name__in=['Nick', 'Sophie']) |
| 116 | |
| 117 | log.info("### Delete a record") |
no test coverage detected