Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add migrations via atlas #59

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
migrations/atlas.sum binary
10 changes: 10 additions & 0 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Development
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Either move everything related with development here from README or consolidate it with README file


## Migrations

If you make changes to the database schema, you need to generate a new migration.
To do this you need to install [atlas](https://atlasgo.io/).

```shell
atlas migrate diff --env=local <migration name>
```
21 changes: 21 additions & 0 deletions atlas.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
data "external_schema" "peewee" {
program = [
"helpers/db_schema.sh"
]
}

env "local" {
src = data.external_schema.peewee.url
dev = "sqlite://dev?mode=memory"
url = "sqlite://whoisdevices.db"

migration {
dir = "file://migrations"
}

format {
migrate {
diff = format("{{ sql . \" \" }}")
}
}
}
4 changes: 4 additions & 0 deletions helpers/db_schema.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash
export PYTHONPATH=.

poetry run python whois/db_schema.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this wrapper needed? Maybe we can run it as a module python -m whois.db_schema

19 changes: 19 additions & 0 deletions migrations/20240321230454_init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
-- Create "device" table
CREATE TABLE `device` (
`mac_address` char NOT NULL,
`hostname` varchar NULL,
`last_seen` datetime NOT NULL,
`user_id` integer NULL,
`flags` integer NULL,
PRIMARY KEY (`mac_address`),
CONSTRAINT `0` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`) ON UPDATE NO ACTION ON DELETE NO ACTION
);
-- Create "user" table
CREATE TABLE `user` (
`id` integer NOT NULL,
`username` varchar NOT NULL,
`password` varchar NOT NULL,
`display_name` varchar NOT NULL,
`flags` integer NULL,
PRIMARY KEY (`id`)
);
2 changes: 2 additions & 0 deletions migrations/atlas.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
h1:EMRfUNLQLucjfPsImSG2fyQeY/BZRZ/LVYOtJAfHzf8=
20240321230454_init.sql h1:+0SqLh3omTCq19IGAYbyECsORj2cicbwW9rOqVwh6VQ=
14 changes: 14 additions & 0 deletions whois/db_schema.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import peewee
from playhouse.reflection import print_table_sql

from whois.database import Device, User


def print_schema():
for model in [Device, User]:
print_table_sql(model)
print(";")


if __name__ == "__main__":
print_schema()