Whenever making any updates to the SQL content in the project, make sure to also update both the pg_YYYYMMDD__init.sql, sqlite_YYYYMMDD__init.sql files, and the Dockerfile. These files are used to initialize the database for PostgreSQL and SQLite respectively, ensuring that SQL changes are properly applied across different environments. Failing to update these files may lead to database inconsistencies, disrupting the system’s operation.
The middle part of the filename (YYYYMMDD, e.g., 20240205 in pg_20240205__init.sql) represents the date of the last modification. When updating these SQL files, you must update this date to the current modification date. This ensures that the file accurately reflects when the last changes were made, aiding in version control and troubleshooting.
For example, if you make modifications on September 5, 2024, the filenames should be updated to:
pg_20240905__init.sqlsqlite_20240905__init.sql
Mega's Docker setup relies on these SQL files for initializing the database, you must also update the Dockerfile to ensure the new SQL initialization files are copied and applied correctly. Make sure the Docker build includes the latest pg_YYYYMMDD__init.sql and sqlite_YYYYMMDD__init.sql filenames and paths, reflecting the current modification date.
For example, update the Dockerfile to reference the newly updated filenames:
# PostgreSQL initialization
COPY pg_20240905__init.sql /docker-entrypoint-initdb.d/
# SQLite initialization
COPY sqlite_20240905__init.sql /app/sqlite/-
Modify SQL Statements
When modifying the database structure (such as tables, indexes, constraints, etc.) or data, first make the changes in the corresponding SQL file in the project. -
Sync Changes to
pg_YYYYMMDD__init.sql,sqlite_YYYYMMDD__init.sql, and the Dockerfile
Apply the SQL changes to both initialization files:pg_YYYYMMDD__init.sqlfor PostgreSQLsqlite_YYYYMMDD__init.sqlfor SQLite
Update the
YYYYMMDDpart of the filename to reflect the current date of modification. Additionally, ensure that theDockerfileis updated to reference the new SQL files. -
Test Database Migration
Ensure the updated SQL files and the newDockerfileare applied in the development and test environments. Verify that the database changes are successful for both PostgreSQL and SQLite. -
Commit and Update Documentation
When committing your code, ensure the updates to bothpg_YYYYMMDD__init.sql,sqlite_YYYYMMDD__init.sql, and theDockerfileare included, with the correct date in the filenames, and document the related database changes in the project’s change log.
For instance, if you add a new table to the project:
CREATE TABLE users (
id SERIAL PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100) UNIQUE
);You need to add this SQL to both the pg_YYYYMMDD__init.sql and sqlite_YYYYMMDD__init.sql files, adapting the SQL syntax as needed for each database system. If the modification is made on September 5, 2024, the filenames should be updated to pg_20240905__init.sql and sqlite_20240905__init.sql. Update the Dockerfile to reference these new filenames for PostgreSQL and SQLite initialization.