|
| 1 | +# Database Schema Changes |
| 2 | +This document covers making changes to the database schema (how the database tables |
| 3 | +are set up) in Uchu in development. Readng and writing to the database while running |
| 4 | +is not covered. |
| 5 | + |
| 6 | +## Setup |
| 7 | +Part of the setup for pushing changes requires making database migrates, which defines |
| 8 | +how a database migrates to new versions. To start, make sure you have `dotnet` in |
| 9 | +your system PATH and either `python3` or `python` in your PATH. Which Python keyword |
| 10 | +you use depends on your setup, but it will most likely be `python` on Windows and |
| 11 | +`python3` on other systems. See external instructions for your operating system for |
| 12 | +setting up .NET and Python. |
| 13 | + |
| 14 | +With .NET installed, you will also need the Microsoft Entity Framework CLI tools. |
| 15 | +This only needs to be run once if you don't have the CLI tools installed: |
| 16 | +```bash |
| 17 | +dotnet tool install --global dotnet-ef |
| 18 | +``` |
| 19 | + |
| 20 | +## Making Schema Changes |
| 21 | +Since Uchu uses Microsoft Entity Framework, changes to the database schema are done |
| 22 | +in the C# classes. No matter what is done, database migrates will be required. |
| 23 | + |
| 24 | +### Column Changes |
| 25 | +The simpler of the two types of changes are to the columns of an existing table/class, |
| 26 | +including adding or deleting (mostly adding - be careful deleting data for backward |
| 27 | +compatibility). To do this, modify the database model class in [Uchu.Core/Database/Models](../Uchu.Core/Database/Models). |
| 28 | +Properties in the models can also have attributes. Some examples include: |
| 29 | +* `[Key]` - Marks the column as being the primary key. *1 and only 1 column must have this.* |
| 30 | +* `[Required]` - Marks the column as being required, or `NOT NULL` in SQL. |
| 31 | +* `[MaxLength(N)]` - For strings, this specifies the max length of strings as length N. |
| 32 | + |
| 33 | +With Entity Framework, some types are able to be automatically handled, such as `DateTime`s |
| 34 | +and references to other database models. |
| 35 | + |
| 36 | +### New Models/Tables |
| 37 | +Adding new database models/tables is more complicated since it requires creating the new |
| 38 | +database model and setting it up with the database contexts. The steps to do this involve: |
| 39 | +1. Create the new database model as a C# class in [Uchu.Core/Database/Models](../Uchu.Core/Database/Models). |
| 40 | + *Remember to specify the primary key with `[Key]`.* |
| 41 | +2. Add the Model as a `DbSet<ClassName>` in [Uchu.Core/Database/Providers/UchuContextBase.cs](../Uchu.Core/Database/Providers/UchuContextBase.cs) |
| 42 | + similar to the other `DbSet<T>` properties. |
| 43 | +3. Add the Model as a `DbSet<ClassName>` in [Uchu.Core/Database/UchuContext.cs](../Uchu.Core/Database/UchuContext.cs) |
| 44 | + similar to the other `DbSet<T>` properties. |
| 45 | + |
| 46 | +## Creating Database Migrates |
| 47 | +After the changes to the schema have been made, database migrates need to be made so the |
| 48 | +database can be updated when Uchu starts to have the new columns or tables. Both ways |
| 49 | +require a unique name for the migration. While it can be random, it is recommended to |
| 50 | +be a human-readable name relevant to the change. In the commands below, this will be |
| 51 | +`MigrateName`. |
| 52 | + |
| 53 | +### Script (Recommended) |
| 54 | +*If you are on macOS, this method is not supported since it involves downloading MariaDB,* |
| 55 | +*which does not have downloads for macOS. Contributions are open for resolving this.* |
| 56 | + |
| 57 | +A helper script exists for creating the database migrates for all the supported database |
| 58 | +providers. The following can be run to generate the database migrates: |
| 59 | +```bash |
| 60 | +cd scripts |
| 61 | +python3 CreateMigrates.py MigrateName |
| 62 | +``` |
| 63 | + |
| 64 | +As mentioned before, `python3` may need to be replaced with `python`. |
| 65 | + |
| 66 | +### Manually (Not Recommended) |
| 67 | +Unlike SQLite and PostgreSQL, MySQL requires a running database. Setup on how to do |
| 68 | +this is not covered and it is recommended to perform a proper secure setup. |
| 69 | + |
| 70 | +For SQLite and PosgreSQL, the migrations can be created with the following: |
| 71 | +```bash |
| 72 | +cd Uchu.Core |
| 73 | +dotnet ef migrations add SqliteMigrationName --context SqliteContext |
| 74 | +dotnet ef migrations add PostgresMigrationName --context PostgresContext |
| 75 | +``` |
| 76 | + |
| 77 | +The MySQL step is similar, but you will need to modify [Uchu.Core/Database/Providers/MySqlContext.cs](../Uchu.Core/Database/Providers/MySqlContext.cs) |
| 78 | +temporarily with a valid connection string for your setup. **Make sure to |
| 79 | +not commit this and to revert it.** After this, run the following: |
| 80 | +```bash |
| 81 | +dotnet ef migrations add MySqlMigrationName --context MySqlContext |
| 82 | +``` |
| 83 | + |
| 84 | +## Commit Changes |
| 85 | +After creating or modifying the database models, modifying the database contexts |
| 86 | +(if applicable), and generating the migrates, the changes can be committed including |
| 87 | +after trying to start Uchu. An error should occur if the migrates cause issues. |
0 commit comments