multidb
This commit is contained in:
@@ -6,7 +6,7 @@ PYTHONPATH=. python3 _tools/db/migrate_sqlite_to_pgsql.py
|
||||
rm -rf venv_migrate
|
||||
|
||||
# reset wszystkich sekwencji w pgsql
|
||||
docker exec -it pgsql-db psql -U zbiorki -d zbiorki
|
||||
docker exec -it zbiorka-pgsql-db psql -U zbiorki -d zbiorki
|
||||
|
||||
|
||||
DO $$
|
||||
|
@@ -1,23 +1,28 @@
|
||||
import sys
|
||||
import os
|
||||
from dotenv import load_dotenv
|
||||
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), "../../")))
|
||||
|
||||
load_dotenv()
|
||||
|
||||
from sqlalchemy import create_engine, MetaData
|
||||
from sqlalchemy.orm import sessionmaker
|
||||
from config import Config
|
||||
from dotenv import load_dotenv
|
||||
load_dotenv()
|
||||
|
||||
|
||||
# Źródło: SQLite
|
||||
sqlite_engine = create_engine("sqlite:///instance/baza.db")
|
||||
sqlite_meta = MetaData()
|
||||
sqlite_meta.reflect(bind=sqlite_engine)
|
||||
|
||||
|
||||
# Cel: PostgreSQL
|
||||
pg_engine = create_engine(Config.SQLALCHEMY_DATABASE_URI)
|
||||
pg_meta = MetaData()
|
||||
pg_meta.reflect(bind=pg_engine)
|
||||
|
||||
|
||||
# Sesje
|
||||
SQLiteSession = sessionmaker(bind=sqlite_engine)
|
||||
PGSession = sessionmaker(bind=pg_engine)
|
||||
@@ -25,19 +30,20 @@ PGSession = sessionmaker(bind=pg_engine)
|
||||
sqlite_session = SQLiteSession()
|
||||
pg_session = PGSession()
|
||||
|
||||
|
||||
def migrate_table(table_name):
|
||||
print("➡️ Używana baza docelowa:", Config.SQLALCHEMY_DATABASE_URI)
|
||||
print(f"\n➡️ Migruję tabelę: {table_name}")
|
||||
print("Używana baza docelowa:", Config.SQLALCHEMY_DATABASE_URI)
|
||||
print(f"Migruję tabelę: {table_name}")
|
||||
source_table = sqlite_meta.tables.get(table_name)
|
||||
target_table = pg_meta.tables.get(table_name)
|
||||
|
||||
if source_table is None or target_table is None:
|
||||
print(f"⚠️ Pominięto: {table_name} (brak w jednej z baz)")
|
||||
print(f"Pominięto: {table_name} (brak w jednej z baz)")
|
||||
return
|
||||
|
||||
rows = sqlite_session.execute(source_table.select()).fetchall()
|
||||
if not rows:
|
||||
print("ℹ️ Brak danych do migracji.")
|
||||
print("Brak danych do migracji.")
|
||||
return
|
||||
|
||||
insert_data = [dict(row._mapping) for row in rows]
|
||||
@@ -46,16 +52,17 @@ def migrate_table(table_name):
|
||||
with pg_engine.begin() as conn:
|
||||
conn.execute(target_table.delete())
|
||||
conn.execute(target_table.insert(), insert_data)
|
||||
print(f"✅ Przeniesiono: {len(rows)} rekordów")
|
||||
print(f"Przeniesiono: {len(rows)} rekordów")
|
||||
except Exception as e:
|
||||
print(f"❌ Błąd przy migracji {table_name}: {e}")
|
||||
print(f"Błąd przy migracji {table_name}: {e}")
|
||||
|
||||
|
||||
def main():
|
||||
tables = ["user", "shopping_list", "item", "expense", "receipt", "suggested_product"]
|
||||
tables = ["zbiorka", "przedmiot", "uzytkownik", "wydatek", "ustawienia_globalne", "wplata"]
|
||||
for table in tables:
|
||||
migrate_table(table)
|
||||
print("\n🎉 Migracja zakończona pomyślnie.")
|
||||
print("\nMigracja zakończona pomyślnie.")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
14
app.py
14
app.py
@@ -178,13 +178,13 @@ def load_user(user_id):
|
||||
|
||||
@event.listens_for(Engine, "connect")
|
||||
def set_sqlite_pragma(dbapi_connection, connection_record):
|
||||
try:
|
||||
cursor = dbapi_connection.cursor()
|
||||
cursor.execute("PRAGMA foreign_keys=ON")
|
||||
cursor.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
if dbapi_connection.__class__.__module__.startswith('sqlite3'):
|
||||
try:
|
||||
cursor = dbapi_connection.cursor()
|
||||
cursor.execute("PRAGMA foreign_keys=ON")
|
||||
cursor.close()
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
def get_real_ip():
|
||||
headers = request.headers
|
||||
|
13
deploy.sh
13
deploy.sh
@@ -19,6 +19,19 @@ GIT_BRANCH="${GIT_BRANCH:-$(git -C "$REPO_DIR" rev-parse --abbrev-ref HEAD 2>/de
|
||||
DB_PROFILE="sqlite" # domyślnie sqlite, czyli brak profilu pgsql/mysql
|
||||
SERVICES=()
|
||||
|
||||
# Tworzenie katalogów danych dla baz jeśli brak
|
||||
if [[ "$DB_PROFILE" == "pgsql" ]]; then
|
||||
if [[ ! -d "./db/pgsql" ]]; then
|
||||
log "Tworzę katalog ./db/pgsql dla danych PostgreSQL"
|
||||
mkdir -p ./db/pgsql
|
||||
fi
|
||||
elif [[ "$DB_PROFILE" == "mysql" ]]; then
|
||||
if [[ ! -d "./db/mysql" ]]; then
|
||||
log "Tworzę katalog ./db/mysql dla danych MySQL"
|
||||
mkdir -p ./db/mysql
|
||||
fi
|
||||
fi
|
||||
|
||||
# Przetwarzanie argumentów
|
||||
if [[ $# -gt 0 ]]; then
|
||||
case "$1" in
|
||||
|
@@ -42,7 +42,7 @@ services:
|
||||
|
||||
pgsql:
|
||||
image: postgres:17
|
||||
container_name: pgsql-db
|
||||
container_name: zbiorka-pgsql-db
|
||||
environment:
|
||||
POSTGRES_DB: ${DB_NAME}
|
||||
POSTGRES_USER: ${DB_USER}
|
||||
@@ -54,7 +54,7 @@ services:
|
||||
|
||||
mysql:
|
||||
image: mysql:8
|
||||
container_name: mysql-db
|
||||
container_name: zbiorka-mysql-db
|
||||
environment:
|
||||
MYSQL_DATABASE: ${DB_NAME}
|
||||
MYSQL_USER: ${DB_USER}
|
||||
|
@@ -3,4 +3,7 @@ Flask-SQLAlchemy
|
||||
Flask-Login
|
||||
Werkzeug
|
||||
waitress
|
||||
markdown
|
||||
markdown
|
||||
psycopg2-binary # pgsql
|
||||
pymysql # mysql
|
||||
cryptography # mysql8
|
Reference in New Issue
Block a user