Skip to content

Commit

Permalink
Create migration script & Fix postgres tables definition
Browse files Browse the repository at this point in the history
  • Loading branch information
7PH committed Feb 4, 2024
1 parent 858e683 commit 0a97bbb
Show file tree
Hide file tree
Showing 6 changed files with 2,142 additions and 34 deletions.
1 change: 0 additions & 1 deletion app/database/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
data
db.load
8 changes: 3 additions & 5 deletions app/database/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,9 @@ RUN apk --update add pgloader
# Change workdir
WORKDIR /app

COPY db.load .

# Create a non-root user and group with the specified UID and GID
RUN addgroup -g $DOCKER_GID $DOCKER_USER && \
adduser -u $DOCKER_UID -G $DOCKER_USER -D $DOCKER_USER && \
chown -R $DOCKER_USER:$DOCKER_USER .
RUN addgroup -g "$DOCKER_GID" "$DOCKER_USER" && \
adduser -u "$DOCKER_UID" -G "$DOCKER_USER" -D "$DOCKER_USER" && \
chown -R "$DOCKER_USER:$DOCKER_USER" .

USER $DOCKER_UID:$DOCKER_GID
102 changes: 102 additions & 0 deletions app/server/migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import * as sqlite from 'sqlite';
import * as sqlite3 from 'sqlite3';
import { DatabaseHelper } from './skychat/DatabaseHelper';
import SQL from 'sql-template-strings';

const SQLITE_DB_PATH = 'database.db';

function loadSQLite() {
try {
return sqlite.open({
filename: SQLITE_DB_PATH,
mode: sqlite3.OPEN_READONLY,
driver: sqlite3.Database,
});
} catch (error) {
throw new Error('No migration to do, SQLite database not found');
}
}

(async () => {
// Load new pg client
await DatabaseHelper.load();

// Also load old SQLite database
const sqliteDb = await loadSQLite();

// Empty the new database
await DatabaseHelper.db.query('TRUNCATE TABLE users CASCADE');
await DatabaseHelper.db.query('TRUNCATE TABLE messages CASCADE');

// Migrate users
console.log('Migrating users');
const users = await sqliteDb.all('SELECT * FROM users');
for (const user of users) {
await DatabaseHelper.db.query(
SQL`INSERT INTO users (
id,
username,
username_custom,
email,
password,
money,
xp,
"right",
data,
storage,
tms_created,
tms_last_seen
)
VALUES (
${user.id},
${user.username},
${user.username_custom},
${user.email},
${user.password},
${Math.round(user.money)},
${user.xp},
${user.right},
${user.data},
${user.storage},
${user.tms_created},
${user.tms_last_seen}
)`,
);
}

// Reset posgres sequence
console.log('Resetting users sequence');
await DatabaseHelper.db.query("SELECT setval('users_id_seq', (SELECT MAX(id) FROM users))");

// Migrate messages
console.log('Migrating messages');
const messages = await sqliteDb.all('SELECT * FROM messages');
for (const message of messages) {
await DatabaseHelper.db.query(
SQL`INSERT INTO messages (
id,
room_id,
user_id,
quoted_message_id,
content,
date,
ip
)
VALUES (
${message.id},
${message.room_id},
${message.user_id},
${message.quoted_message_id},
${message.content},
TO_TIMESTAMP(${Math.round(message.date / 1000)}),
${message.ip}
)`,
);
}

// Reset posgres sequence
console.log('Resetting messages sequence');
await DatabaseHelper.db.query("SELECT setval('messages_id_seq', (SELECT MAX(id) FROM messages))");

console.log('Done');
})();
6 changes: 3 additions & 3 deletions app/server/skychat/DatabaseHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ CREATE TABLE IF NOT EXISTS users (
xp int NOT NULL,
"right" int NOT NULL,
data varchar(4096) NOT NULL,
storage varchar(8192) DEFAULT '{}'::varchar(8192) NOT NULL,
storage text DEFAULT '{}' NOT NULL,
tms_created int NOT NULL,
tms_last_seen int NOT NULL,
CONSTRAINT username_unique UNIQUE(username)
Expand All @@ -22,9 +22,9 @@ CREATE TABLE IF NOT EXISTS messages (
room_id INTEGER NOT NULL,
user_id INTEGER NOT NULL,
quoted_message_id INTEGER DEFAULT NULL,
content varchar(2048) NOT NULL,
content text NOT NULL,
date timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
ip varchar(39) DEFAULT NULL
ip text DEFAULT NULL
);
`;
Expand Down
Loading

0 comments on commit 0a97bbb

Please sign in to comment.