-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
82 additions
and
2 deletions.
There are no files selected for viewing
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
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) |
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