-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #47 from Code4GovTech/supabase_migration_orm
Supabase migration orm
- Loading branch information
Showing
9 changed files
with
288 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +0,0 @@ | ||
import os, sys | ||
from typing import Any | ||
from supabase import create_client, Client | ||
from supabase.lib.client_options import ClientOptions | ||
from abc import ABC, abstractmethod | ||
|
||
client_options = ClientOptions(postgrest_client_timeout=None) | ||
|
||
|
||
|
||
class SupabaseInterface(): | ||
|
||
_instance = None | ||
|
||
def __init__(self): | ||
if not SupabaseInterface._instance: | ||
|
||
# Load environment variables | ||
from dotenv import load_dotenv | ||
load_dotenv() | ||
|
||
SUPABASE_URL = os.getenv('SUPABASE_URL') | ||
SUPABASE_KEY = os.getenv('SUPABASE_KEY') | ||
self.client: Client = create_client(SUPABASE_URL, SUPABASE_KEY) | ||
SupabaseInterface._instance = self | ||
else: | ||
SupabaseInterface._instance = self._instance | ||
|
||
|
||
|
||
@staticmethod | ||
def get_instance(): | ||
# Static method to retrieve the singleton instance | ||
if not SupabaseInterface._instance: | ||
# If no instance exists, create a new one | ||
SupabaseInterface._instance = SupabaseInterface() | ||
return SupabaseInterface._instance | ||
|
||
|
||
def readAll(self, table): | ||
data = self.client.table(f"{table}").select("*").execute() | ||
return data.data | ||
|
||
def add_data(self, data,table_name): | ||
data = self.client.table(table_name).insert(data).execute() | ||
return data.data | ||
|
||
def add_data_filter(self, data, table_name): | ||
# Construct the filter based on the provided column names and values | ||
filter_data = {column: data[column] for column in ['dmp_id','issue_number','owner']} | ||
|
||
# Check if the data already exists in the table based on the filter | ||
existing_data = self.client.table(table_name).select("*").eq('dmp_id',data['dmp_id']).execute() | ||
|
||
# If the data already exists, return without creating a new record | ||
if existing_data.data: | ||
return "Data already exists" | ||
|
||
# If the data doesn't exist, insert it into the table | ||
new_data = self.client.table(table_name).insert(data).execute() | ||
return new_data.data | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
from flask import Flask | ||
from flask_sqlalchemy import SQLAlchemy | ||
from datetime import datetime | ||
|
||
db = SQLAlchemy() | ||
|
||
class DmpOrg(db.Model): | ||
__tablename__ = 'dmp_orgs' | ||
|
||
id = db.Column(db.Integer, primary_key=True, autoincrement=True) | ||
created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) | ||
name = db.Column(db.String, nullable=False) | ||
description = db.Column(db.Text, nullable=True) | ||
link = db.Column(db.String, nullable=False) | ||
repo_owner = db.Column(db.String, nullable=False) | ||
|
||
# Relationship to DmpIssueUpdate | ||
issues = db.relationship('DmpIssueUpdate', backref='organization', lazy=True) | ||
|
||
# Updated relationship name to avoid conflict | ||
dmp_issues = db.relationship('DmpIssue', backref='organization', lazy=True) | ||
|
||
def __repr__(self): | ||
return f"<DmpOrg(id={self.id}, name={self.name})>" | ||
|
||
def to_dict(self): | ||
return { | ||
'id': self.id, | ||
'created_at': self.created_at.isoformat(), | ||
'name': self.name, | ||
'description': self.description, | ||
'link': self.link, | ||
'repo_owner': self.repo_owner | ||
} | ||
|
||
class DmpIssue(db.Model): | ||
__tablename__ = 'dmp_issues' | ||
|
||
id = db.Column(db.Integer, primary_key=True, autoincrement=True) | ||
issue_url = db.Column(db.String, nullable=False) | ||
issue_number = db.Column(db.Integer, nullable=False) | ||
mentor_username = db.Column(db.String, nullable=True) | ||
contributor_username = db.Column(db.String, nullable=True) | ||
title = db.Column(db.String, nullable=False) | ||
org_id = db.Column(db.Integer, db.ForeignKey('dmp_orgs.id'), nullable=False) | ||
description = db.Column(db.Text, nullable=True) | ||
repo = db.Column(db.String, nullable=True) | ||
|
||
|
||
# Relationship to Prupdates | ||
pr_updates = db.relationship('Prupdates', backref='pr_details', lazy=True) | ||
|
||
def __repr__(self): | ||
return f"<DmpIssue(id={self.id}, title={self.title})>" | ||
|
||
def to_dict(self): | ||
return { | ||
'id': self.id, | ||
'issue_url': self.issue_url, | ||
'issue_number': self.issue_number, | ||
'mentor_username': self.mentor_username, | ||
'contributor_username': self.contributor_username, | ||
'title': self.title, | ||
'org_id': self.org_id, | ||
'description': self.description, | ||
'repo': self.repo | ||
} | ||
|
||
class DmpIssueUpdate(db.Model): | ||
__tablename__ = 'dmp_issue_updates' | ||
|
||
created_at = db.Column(db.DateTime, default=datetime.utcnow, nullable=False) | ||
body_text = db.Column(db.Text, nullable=False) | ||
comment_link = db.Column(db.String, nullable=False) | ||
comment_id = db.Column(db.BigInteger, primary_key=True, nullable=False) | ||
comment_api = db.Column(db.String, nullable=False) | ||
comment_updated_at = db.Column(db.DateTime, nullable=False) | ||
dmp_id = db.Column(db.Integer, db.ForeignKey('dmp_orgs.id'), nullable=False) | ||
created_by = db.Column(db.String, nullable=False) | ||
|
||
def __repr__(self): | ||
return f"<DmpIssueUpdate(comment_id={self.comment_id}, dmp_id={self.dmp_id})>" | ||
|
||
def to_dict(self): | ||
return { | ||
'created_at': self.created_at.isoformat(), | ||
'body_text': self.body_text, | ||
'comment_link': self.comment_link, | ||
'comment_id': self.comment_id, | ||
'comment_api': self.comment_api, | ||
'comment_updated_at': self.comment_updated_at.isoformat(), | ||
'dmp_id': self.dmp_id, | ||
'created_by': self.created_by | ||
} | ||
|
||
class Prupdates(db.Model): | ||
__tablename__ = 'dmp_pr_updates' | ||
|
||
created_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) | ||
pr_id = db.Column(db.Integer, nullable=False,primary_key=True) | ||
status = db.Column(db.String, nullable=False) | ||
title = db.Column(db.String, nullable=False) | ||
pr_updated_at = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) | ||
merged_at = db.Column(db.DateTime) | ||
closed_at = db.Column(db.DateTime) | ||
dmp_id = db.Column(db.Integer, db.ForeignKey('dmp_issues.id'), nullable=False) # ForeignKey relationship | ||
link = db.Column(db.String, nullable=False) | ||
|
||
def __repr__(self): | ||
return f'<PullRequest {self.pr_id} - {self.title}>' | ||
|
||
def to_dict(self): | ||
return { | ||
'created_at': self.created_at.isoformat(), | ||
'pr_id': self.pr_id, | ||
'status': self.status, | ||
'title': self.title, | ||
'pr_updated_at': self.pr_updated_at.isoformat(), | ||
'merged_at': self.merged_at.isoformat() if self.merged_at else None, | ||
'closed_at': self.closed_at.isoformat() if self.closed_at else None, | ||
'dmp_id': self.dmp_id, | ||
'link': self.link | ||
} | ||
|
||
class DmpWeekUpdate(db.Model): | ||
__tablename__ = 'dmp_week_updates' | ||
|
||
id = db.Column(db.Integer, primary_key=True, autoincrement=True) | ||
issue_url = db.Column(db.String, nullable=False) | ||
week = db.Column(db.Integer, nullable=False) | ||
total_task = db.Column(db.Integer, nullable=False) | ||
completed_task = db.Column(db.Integer, nullable=False) | ||
progress = db.Column(db.Integer, nullable=False) | ||
task_data = db.Column(db.Text, nullable=False) | ||
dmp_id = db.Column(db.Integer, nullable=False) | ||
|
||
def __repr__(self): | ||
return f"<DmpWeekUpdate(id={self.id}, week={self.week}, dmp_id={self.dmp_id})>" | ||
|
||
# if __name__ == '__main__': | ||
# db.create_all() |
Oops, something went wrong.