Skip to content

Commit

Permalink
Merge b773a3e into 44f1b13
Browse files Browse the repository at this point in the history
  • Loading branch information
dougthor42 committed Mar 29, 2019
2 parents 44f1b13 + b773a3e commit 19d7579
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 2 deletions.
79 changes: 79 additions & 0 deletions migrations/0006_make_primary_keys_autoincrement.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
"""
make_pks_autoincrement
date created: 2019-03-28 21:47:26.858802
"""

metric_original = """CREATE TABLE `metric` (
`metric_id` INTEGER NOT NULL,
`name` VARCHAR(120) NOT NULL UNIQUE,
`units` VARCHAR(24),
`upper_limit` REAL,
`lower_limit` REAL,
PRIMARY KEY(metric_id)
);"""

metric_new = """CREATE TABLE `metric` (
`metric_id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`name` VARCHAR(120) NOT NULL UNIQUE,
`units` VARCHAR(24),
`upper_limit` REAL,
`lower_limit` REAL
);"""

datapoint_original = """CREATE TABLE `datapoint` (
`datapoint_id` INTEGER NOT NULL,
`metric_id` INTEGER NOT NULL,
`value` REAL NOT NULL,
`timestamp` INTEGER NOT NULL,
PRIMARY KEY(datapoint_id),
FOREIGN KEY(`metric_id`) REFERENCES "metric" ( "metric_id" ) ON DELETE CASCADE
);"""

datapoint_new = """CREATE TABLE `datapoint` (
`datapoint_id` INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
`metric_id` INTEGER NOT NULL,
`value` REAL NOT NULL,
`timestamp` INTEGER NOT NULL,
FOREIGN KEY(`metric_id`) REFERENCES "metric" ( "metric_id" ) ON DELETE CASCADE
);"""

COPY_SQL = """
/* ===== Begin copy for `{tbl_name}` ===== */
ALTER TABLE `{tbl_name}` RENAME TO `{tbl_name}_temp`;
{create_table_sql};
INSERT INTO `{tbl_name}` SELECT * FROM `{tbl_name}_temp`;
DROP TABLE `{tbl_name}_temp`;
"""

def _create_copy_sql(tbl_name, create_table_sql):
"""
Generate the SQL to copy one table to another.
Parameters
----------
tbl_name : string
The name of the table to copy. The intermediate table will be have
"_temp" appended to it.
create_table_sql : string
The SQL to generate the new table.
"""
# Apparently migrator.execute_sql can only execute one statment at a
# time. So we gotta do it one by one. It's *probably* save enough
# to split on the semicolon...
sql = COPY_SQL.format(tbl_name=tbl_name,
create_table_sql=create_table_sql)
return sql.split(";")


def upgrade(migrator):
sql = _create_copy_sql("metric", metric_new)
sql += _create_copy_sql("datapoint", datapoint_new)
for line in sql:
migrator.execute_sql(line)


def downgrade(migrator):
sql = _create_copy_sql("metric", metric_original)
sql += _create_copy_sql("datapoint", datapoint_original)
for line in sql:
migrator.execute_sql(line)
5 changes: 3 additions & 2 deletions src/trendlines/orm.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from peewee import CharField
from peewee import OperationalError
from peewee_moves import DatabaseManager
from playhouse.sqlite_ext import AutoIncrementField

from trendlines import logger
from trendlines import utils
Expand Down Expand Up @@ -60,7 +61,7 @@ class Metric(InternalModel):
Table holding all of the Metric information.
"""

metric_id = IntegerField(primary_key=True)
metric_id = AutoIncrementField()
name = CharField(max_length=120, unique=True)
units = CharField(max_length=24, null=True)
upper_limit = FloatField(null=True)
Expand All @@ -82,7 +83,7 @@ class DataPoint(DataModel):
naive :class:`datetime.datetime` objects (no timezone info).
"""

datapoint_id = IntegerField(primary_key=True)
datapoint_id = AutoIncrementField()
metric = ForeignKeyField(Metric, backref="datapoints",
on_delete="CASCADE")
value = FloatField()
Expand Down

0 comments on commit 19d7579

Please sign in to comment.