Created
February 16, 2021 05:24
-
-
Save stevepiercy/a65f53d53716becc931bc9b41e1ba1af to your computer and use it in GitHub Desktop.
Import data via Python script
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
def setup_models(dbsession): | |
""" | |
Load table data in the following order: | |
table_1 | |
table_2 | |
""" | |
path = os.path.dirname(__file__) | |
fixture = os.path.join(path, "data/table_1.tsv") | |
with open(fixture, "r") as tsvfile: | |
tbl_reader = csv.reader(tsvfile, delimiter="\t") | |
try: | |
for row in tbl_reader: | |
table_1 = models.Table1(name=row[0]) | |
dbsession.add(table_1) | |
except OperationalError: | |
pass | |
fixture = os.path.join(path, "data/table_2.tsv") | |
with open(fixture, "r") as tsvfile: | |
tbl_reader = csv.reader(tsvfile, delimiter="\t") | |
try: | |
for row in tbl_reader: | |
table_2 = models.Table2(name=row[0]) | |
dbsession.add(table_2) | |
except OperationalError: | |
pass | |
@click.command() | |
@click.argument("config_uri") | |
def main(config_uri): | |
""" | |
Create tables and load initial items. | |
CONFIG_URI is a configuration file, for example, development.ini. | |
""" | |
setup_logging(config_uri) | |
env = bootstrap(config_uri) | |
engine = env["request"].registry["dbsession_factory"]().get_bind() | |
try: | |
meta.Base.metadata.create_all(engine) | |
with env["request"].tm: | |
dbsession = env["request"].dbsession | |
setup_models(dbsession) | |
except OperationalError: | |
print("""Unable to establish dbsession.""") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment