def create_tables(): """Create tables filled with test data.""" occupations_table = Table('occupations', metadata, Column('id', Integer, primary_key=True), Column('name', String(20))) users_table = Table('users', metadata, Column('id', Integer, primary_key=True), Column('name', String(20)), Column('occupation_id', Integer, ForeignKey("occupations.id"))) addresses_table = Table('addresses', metadata, Column('id', Integer, primary_key=True), Column('user_id', Integer, ForeignKey("users.id")), Column('street', String(50)), Column('city', String(40))) mapper(Occupation, occupations_table) mapper(User, users_table, properties={ 'occupation' : relation(Occupation, lazy=False), 'addresses': relation(Address, backref='user', lazy=False)}) mapper(Address, addresses_table) try: metadata.create_all() except Exception, error: # workaround for a problem with PySqlite < 2.6 and SQLAlchemy < 0.5 if 'Cannot operate on a closed cursor' in str(error): metadata.create_all(checkfirst=False) else: raise
def setup_package(): assert os.path.exists( _config_file), 'Config file %s must exist' % _config_file load_config(configfile=_config_file) log_to_stream(sys.stdout, level=logging.DEBUG) get_engine() metadata.create_all()
def setUp(self): """Set database configuration and set up database. """ self.sa_dburi = config.get('sqlalchemy.dburi') set_db_uri('sqlite:///:memory:', 'sqlalchemy') bind_metadata() self.users_table = Table("users", metadata, Column("user_id", Integer, primary_key=True), Column("user_name", String(40)), Column("password", String(10))) metadata.create_all()
def create_tables(drop_all=False): """Create all tables defined in the model in the database. Optionally drop existing tables before creating them. """ get_engine() if drop_all: print "Dropping all database tables defined in model." metadata.drop_all() metadata.create_all() print "All database tables defined in model created."
def setup_module(): global users_table, test_table, multi_table, fresh_multi_table config.update({ 'sqlalchemy.dburi': 'sqlite:///:memory:', 'fresh.dburi': 'sqlite:///freshtest.db', }) if os.path.exists('freshtest.db'): os.unlink('freshtest.db') fresh_metadata = get_metadata('fresh') if metadata.is_bound(): metadata.bind = None bind_metadata() metadata.bind.echo = True fresh_metadata.bind.echo = True users_table = Table("users", metadata, Column("user_id", Integer, primary_key=True), Column("user_name", String(40)), Column("password", String(10))) persons_table = Table("persons", metadata, Column("id", Integer, primary_key=True), Column("name", String(40))) addresses_table = Table("addresses", metadata, Column("id", Integer, primary_key=True), Column("address", String(40)), Column("city", String(40)), Column("person_id", Integer, ForeignKey(persons_table.c.id))) multi_table = Table('multi', metadata, Column('id', Integer, primary_key=True)) metadata.create_all() test_table = Table("test", fresh_metadata, Column("id", Integer, primary_key=True), Column("val", String(40))) fresh_multi_table = Table('multi', fresh_metadata, Column('id', Integer, primary_key=True)) fresh_metadata.create_all() mapper(User, users_table) mapper(Person, persons_table) mapper(Address, addresses_table, properties=dict( person=relation(Person, backref='addresses'))) mapper(Test, test_table) start_server()
def create(command, args): print "Creating tables at %s" % (config.get("sqlalchemy.dburi")) get_engine() get_model() metadata.create_all()
def init_db(user_name=None, password=None, user_display_name=None, user_email_address=None): get_engine() metadata.create_all() session.begin() try: admin = Group.by_name(u'admin') except InvalidRequestError: admin = Group(group_name=u'admin',display_name=u'Admin') try: lab_controller = Group.by_name(u'lab_controller') except InvalidRequestError: lab_controller = Group(group_name=u'lab_controller', display_name=u'Lab Controller') #Setup User account if user_name: if password: user = User(user_name=user_name.decode('utf8'), password=password.decode('utf8')) if user_display_name: user.display_name = user_display_name.decode('utf8') if user_email_address: user.email_address = user_email_address.decode('utf8') admin.users.append(user) else: print "Password must be provided with username" elif len(admin.users) == 0: print "No admin account exists, please create one with --user" sys.exit(1) # Create distro_expire perm if not present try: distro_expire_perm = Permission.by_name(u'distro_expire') except NoResultFound: distro_expire_perm = Permission(u'distro_expire') # Create proxy_auth perm if not present try: proxy_auth_perm = Permission.by_name(u'proxy_auth') except NoResultFound: proxy_auth_perm = Permission(u'proxy_auth') # Create tag_distro perm if not present try: tag_distro_perm = Permission.by_name(u'tag_distro') except NoResultFound: tag_distro_perm = Permission(u'tag_distro') admin.permissions.append(tag_distro_perm) # Create stop_task perm if not present try: stop_task_perm = Permission.by_name(u'stop_task') except NoResultFound: stop_task_perm = Permission(u'stop_task') lab_controller.permissions.append(stop_task_perm) admin.permissions.append(stop_task_perm) # Create secret_visible perm if not present try: secret_visible_perm = Permission.by_name(u'secret_visible') except NoResultFound: secret_visible_perm = Permission(u'secret_visible') lab_controller.permissions.append(secret_visible_perm) admin.permissions.append(secret_visible_perm) #Setup Hypervisors Table if Hypervisor.query.count() == 0: kvm = Hypervisor(hypervisor=u'KVM') xen = Hypervisor(hypervisor=u'Xen') hyperv = Hypervisor(hypervisor=u'HyperV') vmware = Hypervisor(hypervisor=u'VMWare') #Setup kernel_type Table if KernelType.query.count() == 0: default = KernelType(kernel_type=u'default', uboot=False) highbank = KernelType(kernel_type=u'highbank', uboot=False) imx = KernelType(kernel_type=u'imx', uboot=False) mvebu = KernelType(kernel_type=u'mvebu', uboot=True) omap = KernelType(kernel_type=u'omap', uboot=False) tegra = KernelType(kernel_type=u'tegra', uboot=False) #Setup base Architectures if Arch.query.count() == 0: i386 = Arch(u'i386') x86_64 = Arch(u'x86_64') ia64 = Arch(u'ia64') ppc = Arch(u'ppc') ppc64 = Arch(u'ppc64') s390 = Arch(u's390') s390x = Arch(u's390x') armhfp = Arch(u'armhfp') #Setup base power types if PowerType.query.count() == 0: apc_snmp = PowerType(u'apc_snmp') PowerType(u'apc_snmp_then_etherwake') bladecenter = PowerType(u'bladecenter') bullpap = PowerType(u'bladepap') drac = PowerType(u'drac') ether_wake = PowerType(u'ether_wake') PowerType(u'hyper-v') ilo = PowerType(u'ilo') integrity = PowerType(u'integrity') ipmilan = PowerType(u'ipmilan') ipmitool = PowerType(u'ipmitool') lpar = PowerType(u'lpar') rsa = PowerType(u'rsa') virsh = PowerType(u'virsh') wti = PowerType(u'wti') #Setup key types if Key.query.count() == 0: DISKSPACE = Key(u'DISKSPACE',True) COMMENT = Key(u'COMMENT') CPUFAMILY = Key(u'CPUFAMILY',True) CPUFLAGS = Key(u'CPUFLAGS') CPUMODEL = Key(u'CPUMODEL') CPUMODELNUMBER = Key(u'CPUMODELNUMBER', True) CPUSPEED = Key(u'CPUSPEED',True) CPUVENDOR = Key(u'CPUVENDOR') DISK = Key(u'DISK',True) FORMFACTOR = Key(u'FORMFACTOR') HVM = Key(u'HVM') MEMORY = Key(u'MEMORY',True) MODEL = Key(u'MODEL') MODULE = Key(u'MODULE') NETWORK = Key(u'NETWORK') NR_DISKS = Key(u'NR_DISKS',True) NR_ETH = Key(u'NR_ETH',True) NR_IB = Key(u'NR_IB',True) PCIID = Key(u'PCIID') PROCESSORS = Key(u'PROCESSORS',True) RTCERT = Key(u'RTCERT') SCRATCH = Key(u'SCRATCH') STORAGE = Key(u'STORAGE') USBID = Key(u'USBID') VENDOR = Key(u'VENDOR') XENCERT = Key(u'XENCERT') NETBOOT = Key(u'NETBOOT_METHOD') #Setup ack/nak reposnses if Response.query.count() == 0: ACK = Response(response=u'ack') NAK = Response(response=u'nak') if RetentionTag.query.count() == 0: SCRATCH = RetentionTag(tag=u'scratch', is_default=1, expire_in_days=30) SIXTYDAYS = RetentionTag(tag=u'60days', needs_product=False, expire_in_days=60) ONETWENTYDAYS = RetentionTag(tag=u'120days', needs_product=False, expire_in_days=120) ACTIVE = RetentionTag(tag=u'active', needs_product=True) AUDIT = RetentionTag(tag=u'audit', needs_product=True) config_items = [ # name, description, numeric (u'root_password', u'Plaintext root password for provisioned systems', False), (u'root_password_validity', u"Maximum number of days a user's root password is valid for", True), (u'default_guest_memory', u"Default memory (MB) for dynamic guest provisioning", True), (u'default_guest_disk_size', u"Default disk size (GB) for dynamic guest provisioning", True), (u'guest_name_prefix', u'Prefix for names of dynamic guests in oVirt', False), ] for name, description, numeric in config_items: try: ConfigItem.by_name(name) except NoResultFound: ConfigItem(name=name, description=description, numeric=numeric) session.flush() if ConfigItem.by_name(u'root_password').current_value() is None: ConfigItem.by_name(u'root_password').set(u'beaker', user=admin.users[0]) session.commit() session.close()
def create_tables(): """Create tables filled with test data.""" occupations_table = Table( "occupations", metadata, Column("id", Integer, primary_key=True), Column("name", String(20)) ) users_table = Table( "users", metadata, Column("id", Integer, primary_key=True), Column("name", String(20)), Column("occupation_id", Integer, ForeignKey("occupations.id")), ) addresses_table = Table( "addresses", metadata, Column("id", Integer, primary_key=True), Column("user_id", Integer, ForeignKey("users.id")), Column("street", String(50)), Column("city", String(40)), ) mapper(Occupation, occupations_table) mapper( User, users_table, properties={ "occupation": relation(Occupation, lazy=False), "addresses": relation(Address, backref="user", lazy=False), }, ) mapper(Address, addresses_table) metadata.create_all() occupations = ({"id": 1, "name": "Doctor"}, {"id": 2, "name": "Actor"}, {"id": 3, "name": "Programmer"}) users = ( {"id": 1, "name": "Smith", "occupation_id": 1}, {"id": 2, "name": "John", "occupation_id": 2}, {"id": 3, "name": "Fred", "occupation_id": 2}, {"id": 4, "name": "Albert", "occupation_id": 3}, {"id": 5, "name": "Nicole", "occupation_id": None}, {"id": 6, "name": "Sarah", "occupation_id": None}, {"id": 7, "name": "Walter", "occupation_id": 1}, {"id": 8, "name": "Bush", "occupation_id": None}, ) addresses = ( {"id": 1, "user_id": None, "street": "street P", "city": "city X"}, {"id": 2, "user_id": 2, "street": "street B", "city": "city Z"}, {"id": 3, "user_id": 4, "street": "street C", "city": "city X"}, {"id": 4, "user_id": 1, "street": "street D", "city": "city Y"}, {"id": 5, "user_id": None, "street": "street E", "city": "city Z"}, {"id": 6, "user_id": 1, "street": "street F", "city": "city Z"}, {"id": 7, "user_id": 3, "street": "street G", "city": "city Y"}, {"id": 8, "user_id": 7, "street": "street H", "city": "city Y"}, {"id": 9, "user_id": 1, "street": "street I", "city": "city X"}, {"id": 10, "user_id": 6, "street": "street J", "city": "city X"}, {"id": 11, "user_id": 3, "street": "street K", "city": "city Z"}, {"id": 12, "user_id": 2, "street": "street L", "city": "city X"}, {"id": 13, "user_id": 8, "street": "street M", "city": "city X"}, {"id": 14, "user_id": 5, "street": "street N", "city": "city Y"}, {"id": 15, "user_id": 1, "street": "street O", "city": "city Y"}, {"id": 16, "user_id": 3, "street": "street A", "city": "city X"}, ) metadata.bind.execute(occupations_table.insert(), occupations) metadata.bind.execute(users_table.insert(), users) metadata.bind.execute(addresses_table.insert(), addresses) SOAddress.createTable() # SQLObject tests will need only "addresses" for kw in addresses: SOAddress(**kw)
def setup_module(): global fresh_metadata, users_table, test_table, Person, Address, Test config.update({ "sqlalchemy.dburi" : "sqlite:///:memory:"}) if os.path.exists('freshtest.db'): os.unlink('freshtest.db') get_engine() fresh_metadata = MetaData() # :memory: can't be used in multiple threads fresh_metadata.bind = 'sqlite:///freshtest.db' metadata.bind.echo = True fresh_metadata.bind.echo = True users_table = Table("users", metadata, Column("user_id", Integer, primary_key=True), Column("user_name", String(40)), Column("password", String(10))) mapper(User, users_table) if ActiveMapper: class Person(ActiveMapper): class mapping: id = column(Integer, primary_key=True) name = column(String(40)) addresses = one_to_many("Address") class Address(ActiveMapper): class mapping: id = column(Integer, primary_key=True) address = column(String(40)) city = column(String(40)) person_id = column(Integer, foreign_key=ForeignKey("person.id")) else: persons_table = Table("persons", metadata, Column("id", Integer, primary_key=True), Column("name", String(40))) addresses_table = Table("addresses", metadata, Column("id", Integer, primary_key=True), Column("address", String(40)), Column("city", String(40)), Column("person_id", Integer, ForeignKey(persons_table.c.id))) class Person(object): pass class Address(object): pass mapper(Person, persons_table) mapper(Address, addresses_table, properties=dict( person=relation(Person, backref='addresses'))) test_table = Table("test", fresh_metadata, Column("id", Integer, primary_key=True), Column("val", String(40))) class Test(object): pass mapper(Test, test_table) try: metadata.create_all() fresh_metadata.create_all() except Exception, error: # workaround for a problem with PySqlite < 2.6 and SQLAlchemy < 0.5 if 'Cannot operate on a closed cursor' in str(error): metadata.create_all(checkfirst=False) fresh_metadata.create_all(checkfirst=False) else: raise
def sacreate(command, args): print "Creating tables at %s" % (config.get("sqlalchemy.dburi")) from turbogears.database import bind_meta_data, metadata bind_meta_data() get_model() metadata.create_all()
def setup_package(): assert os.path.exists(_config_file), "Config file %s must exist" % _config_file load_config(configfile=_config_file) log_to_stream(sys.stdout, level=logging.DEBUG) get_engine() metadata.create_all()
def setup_module(): global fresh_metadata, users_table, test_table, Person, Address, Test config.update({"sqlalchemy.dburi": "sqlite:///:memory:"}) if os.path.exists('freshtest.db'): os.unlink('freshtest.db') get_engine() fresh_metadata = MetaData() # :memory: can't be used in multiple threads fresh_metadata.bind = 'sqlite:///freshtest.db' metadata.bind.echo = True fresh_metadata.bind.echo = True users_table = Table("users", metadata, Column("user_id", Integer, primary_key=True), Column("user_name", String(40)), Column("password", String(10))) mapper(User, users_table) if ActiveMapper: class Person(ActiveMapper): class mapping: id = column(Integer, primary_key=True) name = column(String(40)) addresses = one_to_many("Address") class Address(ActiveMapper): class mapping: id = column(Integer, primary_key=True) address = column(String(40)) city = column(String(40)) person_id = column(Integer, foreign_key=ForeignKey("person.id")) else: persons_table = Table("persons", metadata, Column("id", Integer, primary_key=True), Column("name", String(40))) addresses_table = Table( "addresses", metadata, Column("id", Integer, primary_key=True), Column("address", String(40)), Column("city", String(40)), Column("person_id", Integer, ForeignKey(persons_table.c.id))) class Person(object): pass class Address(object): pass mapper(Person, persons_table) mapper(Address, addresses_table, properties=dict(person=relation(Person, backref='addresses'))) test_table = Table("test", fresh_metadata, Column("id", Integer, primary_key=True), Column("val", String(40))) class Test(object): pass mapper(Test, test_table) try: metadata.create_all() fresh_metadata.create_all() except Exception, error: # workaround for a problem with PySqlite < 2.6 and SQLAlchemy < 0.5 if 'Cannot operate on a closed cursor' in str(error): metadata.create_all(checkfirst=False) fresh_metadata.create_all(checkfirst=False) else: raise
def init_db(user_name=None, password=None, user_display_name=None, user_email_address=None): get_engine() metadata.create_all() session.begin() try: admin = Group.by_name(u'admin') except InvalidRequestError: admin = Group(group_name=u'admin',display_name=u'Admin') session.add(admin) try: lab_controller = Group.by_name(u'lab_controller') except InvalidRequestError: lab_controller = Group(group_name=u'lab_controller', display_name=u'Lab Controller') session.add(lab_controller) #Setup User account if user_name: if password: user = User(user_name=user_name.decode('utf8'), password=password.decode('utf8')) if user_display_name: user.display_name = user_display_name.decode('utf8') if user_email_address: user.email_address = user_email_address.decode('utf8') admin.users.append(user) else: print "Password must be provided with username" elif len(admin.users) == 0: print "No admin account exists, please create one with --user" sys.exit(1) # Create distro_expire perm if not present try: distro_expire_perm = Permission.by_name(u'distro_expire') except NoResultFound: distro_expire_perm = Permission(u'distro_expire') session.add(distro_expire_perm) # Create proxy_auth perm if not present try: proxy_auth_perm = Permission.by_name(u'proxy_auth') except NoResultFound: proxy_auth_perm = Permission(u'proxy_auth') session.add(proxy_auth_perm) # Create tag_distro perm if not present try: tag_distro_perm = Permission.by_name(u'tag_distro') except NoResultFound: tag_distro_perm = Permission(u'tag_distro') admin.permissions.append(tag_distro_perm) # Create stop_task perm if not present try: stop_task_perm = Permission.by_name(u'stop_task') except NoResultFound: stop_task_perm = Permission(u'stop_task') lab_controller.permissions.append(stop_task_perm) admin.permissions.append(stop_task_perm) # Create secret_visible perm if not present try: secret_visible_perm = Permission.by_name(u'secret_visible') except NoResultFound: secret_visible_perm = Permission(u'secret_visible') lab_controller.permissions.append(secret_visible_perm) admin.permissions.append(secret_visible_perm) #Setup Hypervisors Table if Hypervisor.query.count() == 0: for h in [u'KVM', u'Xen', u'HyperV', u'VMWare']: session.add(Hypervisor(hypervisor=h)) #Setup kernel_type Table if KernelType.query.count() == 0: for type in [u'default', u'highbank', u'imx', u'omap', u'tegra']: session.add(KernelType(kernel_type=type, uboot=False)) for type in [u'mvebu']: session.add(KernelType(kernel_type=type, uboot=True)) #Setup base Architectures if Arch.query.count() == 0: for arch in [u'i386', u'x86_64', u'ia64', u'ppc', u'ppc64', u's390', u's390x', u'armhfp']: session.add(Arch(arch)) #Setup base power types if PowerType.query.count() == 0: for power_type in [u'apc_snmp', u'apc_snmp_then_etherwake', u'bladecenter', u'bladepap', u'drac', u'ether_wake', u'hyper-v', u'ilo', u'integrity', u'ipmilan', u'ipmitool', u'lpar', u'rsa', u'virsh', u'wti']: session.add(PowerType(power_type)) #Setup key types if Key.query.count() == 0: session.add(Key(u'DISKSPACE',True)) session.add(Key(u'COMMENT')) session.add(Key(u'CPUFAMILY',True)) session.add(Key(u'CPUFLAGS')) session.add(Key(u'CPUMODEL')) session.add(Key(u'CPUMODELNUMBER', True)) session.add(Key(u'CPUSPEED',True)) session.add(Key(u'CPUVENDOR')) session.add(Key(u'DISK',True)) session.add(Key(u'FORMFACTOR')) session.add(Key(u'HVM')) session.add(Key(u'MEMORY',True)) session.add(Key(u'MODEL')) session.add(Key(u'MODULE')) session.add(Key(u'NETWORK')) session.add(Key(u'NR_DISKS',True)) session.add(Key(u'NR_ETH',True)) session.add(Key(u'NR_IB',True)) session.add(Key(u'PCIID')) session.add(Key(u'PROCESSORS',True)) session.add(Key(u'RTCERT')) session.add(Key(u'SCRATCH')) session.add(Key(u'STORAGE')) session.add(Key(u'USBID')) session.add(Key(u'VENDOR')) session.add(Key(u'XENCERT')) session.add(Key(u'NETBOOT_METHOD')) #Setup ack/nak reposnses if Response.query.count() == 0: session.add(Response(response=u'ack')) session.add(Response(response=u'nak')) if RetentionTag.query.count() == 0: session.add(RetentionTag(tag=u'scratch', is_default=1, expire_in_days=30)) session.add(RetentionTag(tag=u'60days', needs_product=False, expire_in_days=60)) session.add(RetentionTag(tag=u'120days', needs_product=False, expire_in_days=120)) session.add(RetentionTag(tag=u'active', needs_product=True)) session.add(RetentionTag(tag=u'audit', needs_product=True)) config_items = [ # name, description, numeric (u'root_password', u'Plaintext root password for provisioned systems', False), (u'root_password_validity', u"Maximum number of days a user's root password is valid for", True), (u'default_guest_memory', u"Default memory (MB) for dynamic guest provisioning", True), (u'default_guest_disk_size', u"Default disk size (GB) for dynamic guest provisioning", True), (u'guest_name_prefix', u'Prefix for names of dynamic guests in oVirt', False), ] for name, description, numeric in config_items: ConfigItem.lazy_create(name=name, description=description, numeric=numeric) if ConfigItem.by_name(u'root_password').current_value() is None: ConfigItem.by_name(u'root_password').set(u'beaker', user=admin.users[0]) session.commit() session.close()