async def seed(): log.item('Seeding table hashtags') hashtags = [ Hashtag(name='important'), Hashtag(name='obsolete'), Hashtag(name='outdated'), ] await Hashtag.insert(hashtags)
async def seed(): log.item('Seeding table user_info') # You can use .insert() as a List of model instances await UserInfo.insert([ UserInfo(extra1='user1 extra', user_id=1), UserInfo(extra1='user2 extra', user_id=2), UserInfo(extra1='user3 extra', user_id=3), UserInfo(extra1='user4 extra', user_id=4), #UserInfo(extra1='user5 extra', user_id=5), ])
async def create_tables(connections: str): #log.header('Creating tables for connections: [' + connections + ']') metakeys = get_metakeys(connections) for metakey in metakeys: engine = db.engine(metakey=metakey) log.header( 'Creating tables in {} in topologically order'.format(metakey)) metadata = db.metadata(metakey=metakey) for table in metadata.sorted_tables: log.item('Creating table {}'.format(str(table.name))) metadata.create_all(engine) print()
async def drop_tables(connections: str): """Drop tables for connection(s)""" #log.header('Dropping tables for connections: [' + connections + ']') metakeys = get_metakeys(connections) for metakey in metakeys: engine = db.engine(metakey=metakey) log.header( 'Dropping tables from {} in topologically order'.format(metakey)) metadata = db.metadata(metakey=metakey) for table in reversed(metadata.sorted_tables): log.item('Dropping table {}'.format(str(table.name))) metadata.drop_all(engine) print()
async def seed(): log.item('Seeding table tags') tags = [ Tag(name='linux', creator_id=1), Tag(name='mac', creator_id=1), Tag(name='bsd', creator_id=2), Tag(name='laravel', creator_id=2), Tag(name='lumen', creator_id=3), Tag(name='django', creator_id=3), Tag(name='flask', creator_id=4), Tag(name='fastapi', creator_id=4), Tag(name='starlette', creator_id=4), ] await Tag.insert(tags)
async def seed(): log.item('Seeding format') await Format.insert([ { 'key': 'md', 'name': 'Markdown' }, { 'key': 'htm', 'name': 'HTML' }, { 'key': 'txt', 'name': 'Text' }, ])
async def seed(): log.item('Seeding table contacts') contacts = [ #NO - Contact(name='Administrator', title='God', address='777 Heaven Ln', phone='777-777-7777', user_id=1), #Contact(name='Manager One', title='Manager1', address='111 Manager Way', phone='111-111-1111', user_id=2), Contact(name='Manager Two', title='Manager2', address='222 Manager Way', phone='222-222-2222', user_id=3), Contact(name='User One', title='User1', address='333 User Dr.', phone='333-333-3333', user_id=4), #NO - Contact(name='User Two', title='User2', address='444 User Dr.', phone='444-444-4444', user_id=5), ] await Contact.insert(contacts)
async def seed(): log.item('Seeding table comments') # You can use the parent with child relations for creating # Have to use Dict notation. If you use Model pydantic class, post_id is required which defeats the purpose of auto ID linkage post = await Post.query().find(1) await post.create( 'comments', [ { 'title': 'Post1 Comment2', 'body': 'Body for post1 comment2', #'post_id': 1, # No id needed, thats what post.create() does 'creator_id': 2, }, ]) # You can use .insert() as a List of model instances await Comment.insert([ # Post 1 has 2 comments #NO-Comment(title='Post1 Comment1', body='Body for post1 comment1', post_id=1), #Comment(title='Post1 Comment2', body='Body for post1 comment2', post_id=1), # Post 2 has 3 Comment(title='Post3 Comment1', body='Body for post3 comment1', post_id=3, creator_id=3), Comment(title='Post3 Comment2', body='Body for post3 comment2', post_id=3, creator_id=4), #NO-Comment(title='Post3 Comment3', body='Body for post3 comment3', post_id=3), ]) # You can also user .insert() as a list of Dict await Comment.insert([{ 'title': 'Post3 Comment3', 'body': 'Body for post3 comment3', 'post_id': 3, 'creator_id': 1, }])
async def seed(): log.item('Seeding table posts') posts = [] fake = Faker() for _ in range(100): topic_id = random.randint(1, 15) title = fake.text(max_nb_chars=50) post = Post( slug=fake.slug(title), title=title, body=fake.paragraph(nb_sentences=5), format_key='md', topic_id=topic_id, view_count=0, creator_id=1, updator_id=1, ) #post.save() posts.append(post) #dump(posts) await Post.insert(posts)
async def seed(): log.item('Seeding space') await Space.insert_with_relations([ { 'slug': '/it', 'name': 'IT', 'order': 1, 'sections': [ { 'slug': '/production', 'name': 'Production', 'icon': None, 'order': 1, 'topics': [ { 'slug': '/opennebula', 'name': 'OpenNebula', 'desc': 'Virtualization stack setup and documentation', 'icon': 'PhoneIcon', 'order': 1, }, { 'slug': '/servers', 'name': 'Servers', 'desc': 'Physical and virtual servers with more really long comments', 'icon': 'PhoneIcon', 'order': 2, }, { 'slug': '/networking', 'name': 'Networking', 'desc': 'Networking setup', 'icon': 'PhoneIcon', 'order': 3, }, ] }, { 'slug': '/branch', 'name': 'Branch Office', 'icon': None, 'order': 2, 'topics': [ { 'slug': '/dallas-office', 'name': 'Dallas Office', 'desc': 'Main headquarters with even more long comments because I ramble', 'icon': 'PhoneIcon', 'order': 1, }, { 'slug': '/dallas-warehouse', 'name': 'Dallas Warehouse', 'desc': 'Main dallas warehouse', 'icon': 'PhoneIcon', 'order': 2, }, ] }, { 'slug': '/support', 'name': 'Support', 'icon': None, 'order': 3, 'topics': [ { 'slug': '/pc', 'name': 'PC', 'desc': 'PC Support and more really long comments', 'icon': 'PhoneIcon', 'order': 1, }, { 'slug': '/phones', 'name': 'Phones', 'desc': 'Phone Support and whatnot and the other', 'icon': 'PhoneIcon', 'order': 2, }, { 'slug': '/printers', 'name': 'Printers', 'desc': 'Printer Support because people cant figure that stuff out', 'icon': 'PhoneIcon', 'order': 3, }, ] }, { 'slug': '/something', 'name': 'Something', 'icon': None, 'order': 4, 'topics': [ { 'slug': '/this-and-that', 'name': 'This and That', 'desc': 'This and that long comments so I can see how it looks on the screen', 'icon': 'PhoneIcon', 'order': 1, }, { 'slug': '/the-other', 'name': 'The Other', 'desc': 'The other, as in this that and the other like ol greg used to say', 'icon': 'PhoneIcon', 'order': 2, }, { 'slug': '/some-son-beach', 'name': 'Some Son Beach', 'desc': 'Another one of ol gregs stupid sayings', 'icon': 'PhoneIcon', 'order': 3, }, ] }, ] }, { 'slug': '/dev', 'name': 'Development', 'order': 2, 'sections': [ { 'slug': '/apps', 'name': 'Applications', 'icon': None, 'order': 1, 'topics': [ { 'slug': '/wiki', 'name': 'Wiki', 'desc': 'Wiki application and more long comments just because', 'icon': 'PhoneIcon', 'order': 1, }, { 'slug': '/tools', 'name': 'Tools', 'desc': 'Tools application and even longer comments still blah blah', 'icon': 'PhoneIcon', 'order': 2, }, ] }, { 'slug': '/languages', 'name': 'Languages', 'icon': None, 'order': 2, 'topics': [ { 'slug': '/python', 'name': 'Python', 'desc': 'Python language', 'icon': 'PhoneIcon', 'order': 1, }, { 'slug': '/rust', 'name': 'Rust', 'desc': 'Rust language', 'icon': 'PhoneIcon', 'order': 2, }, ] }, ] }, ])
async def seed(): log.item('Seeding table images') # Get related tablenames with proper prefixes posts = uvicore.db.tablename('app1.posts') users = uvicore.db.tablename('auth.users')
async def seed(): log.item('Seeding table posts') # Get all tags keyed by name tags = await Tag.query().key_by('name').get() # Get all hastags keyed by name hashtags = await Hashtag.query().key_by('name').get() #post = PostModel(slug='test-post1', title='Test Post1', other='other stuff1', creator_id=1) #await post.save() # Now I want to do inline, though has to be DIct # where I create the post with comments=[{dict}] # WORKS!!! await Post.insert_with_relations([ { 'slug': 'test-post1', 'title': 'Test Post1', 'body': 'This is the body for test post1. I like the color red and green.', 'other': 'other stuff1', 'creator_id': 1, 'owner_id': 2, 'comments': [{ 'title': 'Post1 Comment1', 'body': 'Body for post1 comment1', #'post_id': 1, # No id needed, thats what post.create() does 'creator_id': 1, }], # Many-To-Many tags works with existing Model, new Model or new Dict 'tags': [ # Existing Tag tags['linux'], tags['mac'], tags['bsd'], tags[ 'bsd'], # Yes its a duplicate, testing that it doesn't fail # New Tag as Model (tag created and linked) Tag(name='test1', creator_id=4), # New Tag as Dict (tag created and linked) { 'name': 'test2', 'creator_id': 4 }, ], # Polymorphic One-To-One 'image': { 'filename': 'post1-image.png', 'size': 1234932, }, # Polymorphic One-To-Many Attributes 'attributes': [ { 'key': 'post1-test1', 'value': 'value for post1-test1' }, { 'key': 'post1-test2', 'value': 'value for post1-test2' }, { 'key': 'badge', 'value': 'IT' }, ], # Polymorphic Many-To-Many Hashtags 'hashtags': [ hashtags['important'], hashtags['outdated'], hashtags[ 'outdated'], # Yes its a duplicate, testing that it doesn't fail # New hashtag by model Hashtag(name='test1'), # New hashtag by dict { 'name': 'test2' }, ], }, ]) # Example of adding attributes later post = await Post.query().find(1) # ISSUE: How can we update an attribute that is a dict? # If it weren't a dict we could get it (post.attributes['badge']) then change an attribute then call post.attributes['badge'].save() probably # But if a dict, how can we update a value? Doing a .create/.add like so # await post.add('attributes', [ # {'key': 'post1-test2', 'value': 'xxxx'}, # ]) # Gives us an Integrity error due to models.py insert() around line 92. It assume a bulk insert and cannot upsert # If we don't pass a list it does a single insert which will also fail with IntegrityError. I would have to add code # to know how to SELECT to see if exists based on PK or in the case of polymorphism, all 3 or more poly columns. # Example of adding attributes whos value is also a Dict - DOES NOT WORK YET, need auto-serialization, though I could serialize manually to str # await post.add('attributes', [ # {'key': 'post1-test3', 'value': { # 'this': 'value', # 'is': 'a dict itself!' # }}, # {'key': 'post1-test4', 'value': ['one', 'two', 'three']} # ]) # # Blow out all attributes and set this complete List # await post.set('attributes', [ # {'key': 'post1-test3', 'value': 'value for post1-test3'}, # {'key': 'post1-test4', 'value': 'value for post1-test4'}, # ]) # Example of setting all a Polymorphic Many-To-Many Hashtags - WORKS # await post.set('hashtags', [ # {'name': 'test1'}, # {'name': 'test2'}, # ]) # Example of setting all Many-To-Many Tags - WORKS # await post.set('tags', [ # tags['linux'], # ]) # Example of deleting all Polymorphic Many-To-Many Hashtags - DELETE DOES NOT WORK FOR POLY MTM (as currently designed) #await post.delete('hashtags') # Example of deleting all One-To-Many comments - DELETE DOES NOT WORK FOR OTM (as currently designed) #await post.delete('comments') # Example of deleteing a HasOne child - WORKS #post = await Post.query().find(1) #await post.delete('image') # Example of linking Polymorphic Many-To-Many Hashags - WORKS # await post.link('hashtags', [ # hashtags.get('obsolete') # ]) # Example of linking tags (does not create, only links EXISTING tags) - WORKS # await post.link('tags', [ # # Linking can be EXISTING Dict # # { # # 'id': 1, # # 'name': 'linux', # # 'creator_id': 1, # # } # # Or existing Model # tags.get('linux'), # tags.get('mac'), # tags.get('bsd'), # ]) # Test unlink # await post.unlink('tags', tags.get('linux')) # As not list # await post.unlink('tags', [tags.get('mac')]) # As list # await post.unlink('tags') # All # Create (if not exists) AND link tags # await post.create('tags', [ # tags['linux'], # Already exists, won't re-create # Tag(id=1, name='linux', creator_id=1), # Already exists, should just link # Tag(name='test1', creator_id=4), # Does not exist, should create and link # { # 'name': 'test2', # 'creator_id': 4, # } # ]) #post.create() # Show Attributes #post.attributes # Create and link attributes #post.create('attributes', [{'key': 'asdf', 'value': 'asdf'}]) # Delete and unlink attributes # post.delete('attributes') # all # post.delete('attributes', [attribute1, attribute2]) # by model # post.delete('attributes', 'key1', 'key2') # not by pk, but secondary pk the "key" column somehow # contacts table for a One-To-One Poly # combined PK of table_name + table_pk for unique (so could get rid of ID column technically) # id | table_name | table_pk | name | email | phone # ------------------------------------------------------ # 1 | users | 1 | Matthew | @asdf | 555 # 2 | employee | 4 | Bob | @asdf | 444 # attributes table for a One-To-Many Poly # Only unique has to be ID column, or I suppose a combo of table_name+table_pk+key would do it, would also be the composit index # Then could get rid of ID column # id | table_name | table_pk | key | value # ------------------------------------------- # 1 | users | 1 | name | matthew # 2 | users | 1 | age | 37 # poly_tags pivot table for a Many-To-Many Poly # entity_tags # poly_tags # tag_relations # tag_linkage # post_id | tag_id | # table_name | table_pk | tag_id # ------------------------------ # posts | 1 | 5 # posts | 1 | 6 # comments | 23 | 5 # comments | 23 | 7 # NO, add does not exist. Use create to make/link or link to just link # .add() = create record and linkage #post.query('attributes').add({'key': 'value'}) # this works NOW - it creates and links #post.create('comments', ['asdf']) # So this should create a tag and link it #post.create('tags', ['tag1...]) # Easier than .tags() # Link and unlink should be ONLY for ManyToMany # Because all othe relations the ID is a foreing key on one of the tables # So to unlink it, you have to DELETE the record, there is no "link" # post.link('tags', tags) # post.unlink('tags', tag[0]) #unlink one tag # post.unlink('tags') # unlink all tags # You can insert one so you can insert relations right after post = await Post( slug='test-post2', title='Test Post2', body= 'This is the body for test post2. My favorite frameworks are Laravel and Uvicore!', other=None, creator_id=1, owner_id=2).save() # Create AND Link if nto exist Many-To-Many tags await post.link('tags', [ tags['linux'], tags['bsd'], ]) # Create Polymorphic One-To-One await post.create( 'image', { #'imageable_type': 'posts', # NO, inferred #'imageable_id': 2, # NO, inferred 'filename': 'post2-image.png', 'size': 2483282 }) # Create Polymorphic One-To-Many # NOTE: .add is simplay an alias for .create() await post.add('attributes', [ { 'key': 'post2-test1', 'value': 'value for post2-test1' }, { 'key': 'post2-test2', 'value': 'value for post2-test2' }, { 'key': 'badge', 'value': 'IT' }, ]) # Create Polymorphic Many-To-Many await post.add( 'hashtags', [ hashtags['obsolete'], hashtags['outdated'], hashtags[ 'outdated'], # Yes its a duplicate, testing that it doesn't fail ]) # You can NOT insert relations right away, these tags will be IGNORED # User Dict with insert_with_relations if you want this post = await Post( slug='test-post3', title='Test Post3', body= 'This is the body for test post1. I like the programming in PHP, Python and anything Typescript.', other='other stuff2-bad', # We'll update this away below creator_id=2, owner_id=1, tags=[ # TAGS IGNORED tags['linux'], tags['bsd'], ]).save() # Test an update post.other = 'other stuff3' await post.save() await post.add('attributes', [ { 'key': 'badge', 'value': 'DEV' }, ]) await post.add('hashtags', [ hashtags['important'], ]) # You can use .insert() as a List of model instances # But obviously you cant then add in tags # This WILL NOT insert relations at all await Post.insert([ # 2 posts for admin #Post(slug='test-post1', title='Test Post1', other='other stuff1', creator_id=1), #Post(slug='test-post2', title='Test Post2', other=None, creator_id=1, owner_id=2), # 3 posts for manager1 #Post(slug='test-post3', title='Test Post3', other='other stuff2', creator_id=2, owner_id=1), Post( slug='test-post4', title='Test Post4', body= 'This is the body for test post1. My favorite morotcycles are super fast crotch rockets!', other=None, creator_id=2, owner_id=1), Post( slug='test-post5', title='Test Post5', body='This is the body for test post1. Everyone loves and cynic.', other=None, creator_id=2, owner_id=2), # 2 posts for user2 #Post(slug='test-post6', title='Test Post6', other='other stuff3', creator_id=5), #Post(slug='test-post7', title='Test Post7', other=None, creator_id=5), ]) # You can also user .insert() as a list of Dict # This one inserts BelongsTo children FIRST (user, then contact, then post) # This is a multi nesting deep insert (NOT bulk, in a loop because of relations) # Creates User First, then Contact Second, Then finally Post with new creator_id await Post.insert_with_relations([{ 'slug': 'test-post6', 'title': 'Test Post6', 'body': 'This is the body for test post1. Everyone wants to fly.', 'other': 'other stuff6', #NO - 'creator_id': 5, 'creator': { 'username': '******', 'email': '*****@*****.**', 'first_name': 'User', 'last_name': 'Two', 'creator_id': 2, 'password': '******', 'contact': { 'name': 'User Two', 'title': 'User2', 'address': '444 User Dr.', 'phone': '444-444-4444' # NO user_id=5 }, 'info': { 'extra1': 'user5 extra', }, }, 'owner_id': 3, # Polymorphic One-To-One 'image': { 'filename': 'post6-image.png', 'size': 3345432, }, # Polymorphic One-To-Many 'attributes': [ { 'key': 'post6-test1', 'value': 'value for post6-test1' }, { 'key': 'post6-test2', 'value': 'value for post6-test2' }, { 'key': 'post6-test3', 'value': 'value for post6-test3' }, { 'key': 'badge', 'value': 'IT' }, #{'key': 'test', 'value': 'Hi there, my name is matthew reschke, what is your name? Again, my name is Matthew Reschke, what is your name? Again, my name is Matthew Reschke, what is your name?'}, ], # Polymorphic Many-To-Many 'hashtags': [hashtags['outdated']], }]) # This does NOT work yet, but would be nice. Especially if it can UPDATE an existing child #post = await Post.query().find(6) # await post.add('creator', { # 'email': '*****@*****.**', # 'contact': { # 'name': 'User Two', # 'title': 'User2', # 'address': '444 User Dr.', # 'phone': '444-444-4444' # # NO user_id=5 # }, # }) # You can insert a single model with .save() post = Post( slug='test-post7', title='Test Post7', body='This is the body for test post1. I like the to code alone.', other=None, creator_id=5, owner_id=4) await post.save() await post.create('tags', [ tags.get('linux'), tags.get('bsd'), tags.get('laravel'), ])