Last active
December 18, 2016 21:35
-
-
Save byronformwalt/10986746fa0e80290b69bcb41e486313 to your computer and use it in GitHub Desktop.
Demonstrating Use of Out-of-Schema Properties with Orator
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from orator.migrations import Migration | |
class CreateTableExamples(Migration): | |
def up(self): | |
""" | |
Run the migrations. | |
""" | |
with self.schema.create('examples') as table: | |
table.big_increments('id') | |
table.char('name',255) | |
pass | |
def down(self): | |
""" | |
Revert the migrations. | |
""" | |
self.schema.drop_if_exists('hands') | |
pass |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from orator import Model | |
class Example(Model): | |
# Exclude timestamps. | |
__timestamps__ = [] | |
def __init__(self,c=None): | |
super(Example, self).__init__() | |
self.__name = "This world is so confusing from the inside!" # Commenting this line will make saving possible. | |
self.name = "Hello World!" | |
def __str__(self): | |
return self.name | |
def __repr__(sself): | |
return self.__name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Define how orator's CLI should connect to the database. | |
databases: | |
postgresql: | |
driver: postgres | |
host: localhost | |
database: gist_examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# Configure the database connection, and make a connection. | |
config = { | |
'postgresql': { | |
'driver': 'postgres', | |
'host': 'localhost', | |
'database': 'gist_examples' | |
} | |
} | |
from orator import DatabaseManager | |
db = DatabaseManager(config) | |
# Bind the Model base class to the database connection. | |
from orator import Model | |
Model.set_connection_resolver(db) | |
# Import the Example model class. | |
from example_model import Example | |
# Create an instance of the Example model class. | |
example = Example() | |
print("==> Here is my example: '{}'\n".format(example)) | |
# Attempt to save the instance as a new record in the database. | |
# This generates an error because we defined a property that | |
example.save() | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Create the database if it does not already exist. | |
if psql -lqt | cut -d \| -f 1 | grep -qw "gist_examples"; then | |
# Do nothing. | |
echo "Database 'gist_examples' already exists." | |
else | |
# Create the database, since it does not already exist. | |
echo "Creating database 'gist_examples'." | |
psql -c "CREATE DATABASE ""gist_examples"";" | |
fi | |
# Run orator migrations interactively. | |
MIGRATION_PATH=./ | |
orator migrate --path=$MIGRATION_PATH | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Here is the error that appears;