-
-
Notifications
You must be signed in to change notification settings - Fork 188
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
495 additions
and
52 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 |
---|---|---|
@@ -0,0 +1,5 @@ | ||
DELETE FROM customer_group; | ||
|
||
INSERT INTO customer_group (customer_id, group_id) VALUES ( 1, 2); | ||
|
||
INSERT INTO customer_group (customer_id, group_id) VALUES ( 1, 3); |
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,67 @@ | ||
import datetime | ||
import random | ||
from typing import Dict, List | ||
|
||
import click | ||
from faker import Faker | ||
from schema import Customer, CustomerGroup, Group | ||
from sqlalchemy.orm import sessionmaker | ||
|
||
from pgsync.base import pg_engine, subtransactions | ||
from pgsync.constants import DEFAULT_SCHEMA | ||
from pgsync.helper import teardown | ||
from pgsync.utils import config_loader, get_config | ||
|
||
|
||
@click.command() | ||
@click.option( | ||
"--config", | ||
"-c", | ||
help="Schema config", | ||
type=click.Path(exists=True), | ||
) | ||
def main(config): | ||
|
||
config: str = get_config(config) | ||
teardown(drop_db=False, config=config) | ||
|
||
for document in config_loader(config): | ||
|
||
database: str = document.get("database", document["index"]) | ||
with pg_engine(database) as engine: | ||
schema: str = document.get("schema", DEFAULT_SCHEMA) | ||
connection = engine.connect().execution_options( | ||
schema_translate_map={None: schema} | ||
) | ||
Session = sessionmaker(bind=connection, autoflush=True) | ||
session = Session() | ||
|
||
customers = [ | ||
Customer(name="CustomerA"), | ||
Customer(name="CustomerB"), | ||
Customer(name="CustomerC"), | ||
] | ||
with subtransactions(session): | ||
session.add_all(customers) | ||
|
||
groups = [ | ||
Group(group_name="GroupA"), | ||
Group(group_name="GroupB"), | ||
Group(group_name="GroupC"), | ||
] | ||
with subtransactions(session): | ||
session.add_all(groups) | ||
|
||
customers_groups = [ | ||
CustomerGroup(customer=customers[0], group=groups[0]), | ||
CustomerGroup(customer=customers[1], group=groups[1]), | ||
CustomerGroup(customer=customers[2], group=groups[2]), | ||
] | ||
with subtransactions(session): | ||
session.add_all(customers_groups) | ||
|
||
session.commit() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,35 @@ | ||
[ | ||
{ | ||
"database": "through", | ||
"index": "through", | ||
"nodes": | ||
{ | ||
"table": "customer", | ||
"columns": | ||
[ | ||
"id", | ||
"name" | ||
], | ||
"children": | ||
[ | ||
{ | ||
"table": "group", | ||
"columns": | ||
[ | ||
"id", | ||
"group_name" | ||
], | ||
"relationship": | ||
{ | ||
"variant": "object", | ||
"type": "one_to_many", | ||
"through_tables": | ||
[ | ||
"customer_group" | ||
] | ||
} | ||
} | ||
] | ||
} | ||
} | ||
] |
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,88 @@ | ||
import click | ||
import sqlalchemy as sa | ||
from sqlalchemy.ext.declarative import declarative_base | ||
from sqlalchemy.schema import UniqueConstraint | ||
|
||
from pgsync.base import create_database, create_schema, pg_engine | ||
from pgsync.constants import DEFAULT_SCHEMA | ||
from pgsync.helper import teardown | ||
from pgsync.utils import config_loader, get_config | ||
|
||
Base = declarative_base() | ||
|
||
|
||
class Customer(Base): | ||
__tablename__ = "customer" | ||
__table_args__ = (UniqueConstraint("name"),) | ||
id = sa.Column(sa.Integer, primary_key=True, autoincrement=True) | ||
name = sa.Column(sa.String, nullable=False) | ||
|
||
|
||
class Group(Base): | ||
__tablename__ = "group" | ||
__table_args__ = ( | ||
UniqueConstraint( | ||
"group_name", | ||
), | ||
) | ||
id = sa.Column(sa.Integer, primary_key=True, autoincrement=True) | ||
group_name = sa.Column(sa.String, nullable=False) | ||
|
||
|
||
class CustomerGroup(Base): | ||
__tablename__ = "customer_group" | ||
__table_args__ = ( | ||
UniqueConstraint( | ||
"customer_id", | ||
"group_id", | ||
), | ||
) | ||
id = sa.Column(sa.Integer, primary_key=True, autoincrement=True) | ||
customer_id = sa.Column( | ||
sa.Integer, | ||
sa.ForeignKey(Customer.id, ondelete="CASCADE"), | ||
) | ||
customer = sa.orm.relationship( | ||
Customer, | ||
backref=sa.orm.backref("customers"), | ||
) | ||
group_id = sa.Column( | ||
sa.Integer, | ||
sa.ForeignKey(Group.id, ondelete="CASCADE"), | ||
) | ||
group = sa.orm.relationship( | ||
Group, | ||
backref=sa.orm.backref("groups"), | ||
) | ||
|
||
|
||
def setup(config: str) -> None: | ||
for document in config_loader(config): | ||
database: str = document.get("database", document["index"]) | ||
schema: str = document.get("schema", DEFAULT_SCHEMA) | ||
create_database(database) | ||
create_schema(database, schema) | ||
with pg_engine(database) as engine: | ||
engine = engine.connect().execution_options( | ||
schema_translate_map={None: schema} | ||
) | ||
Base.metadata.drop_all(engine) | ||
Base.metadata.create_all(engine) | ||
|
||
|
||
@click.command() | ||
@click.option( | ||
"--config", | ||
"-c", | ||
help="Schema config", | ||
type=click.Path(exists=True), | ||
) | ||
def main(config): | ||
|
||
config: str = get_config(config) | ||
teardown(config=config) | ||
setup(config) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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
Oops, something went wrong.