예제 #1
0
    def test_create_sql(self):
        table = Table("users")
        table.add_column("id", "integer")
        table.add_column("name", "string")

        sql = 'CREATE TABLE "users" ("id" INTEGER NOT NULL, "name" VARCHAR NOT NULL)'
        self.assertEqual([sql], self.platform.compile_create_sql(table))
예제 #2
0
파일: test_table.py 프로젝트: circulon/orm
    def test_create_sql(self):
        table = Table("users")
        table.add_column("id", "integer")
        table.add_column("name", "string")

        sql = 'CREATE TABLE "users" (id INTEGER, name VARCHAR)'
        self.assertEqual(sql, self.platform.compile_create_sql(table))
예제 #3
0
    def test_create_sql_with_unique_constraint(self):
        table = Table("users")
        table.add_column("id", "integer")
        table.add_column("name", "string")
        table.add_constraint("name", "unique", ["name"])
        table.set_primary_key("id")

        sql = 'CREATE TABLE "users" ("id" INTEGER NOT NULL, "name" VARCHAR NOT NULL, UNIQUE(name))'
        self.platform.constraintize(table.get_added_constraints())
        self.assertEqual(self.platform.compile_create_sql(table), [sql])
예제 #4
0
    def test_rename_table(self):
        table = Table("users")
        table.add_column("name", "string")

        diff = TableDiff("users")
        diff.from_table = table
        diff.new_name = "clients"

        sql = ['ALTER TABLE "users" RENAME TO "clients"']

        self.assertEqual(sql, self.platform.compile_alter_sql(diff))
예제 #5
0
    def test_alter_rename(self):
        table = Table("users")
        table.add_column("post", "integer")

        diff = TableDiff("users")
        diff.from_table = table
        diff.rename_column("post", "comment", "integer")

        sql = [
            "CREATE TEMPORARY TABLE __temp__users AS SELECT post FROM users",
            'DROP TABLE "users"',
            'CREATE TABLE "users" ("comment" INTEGER NOT NULL)',
            'INSERT INTO "users" ("comment") SELECT post FROM __temp__users',
            "DROP TABLE __temp__users",
        ]

        self.assertEqual(sql, self.platform.compile_alter_sql(diff))
예제 #6
0
    def test_create_sql_with_foreign_key_constraint(self):
        table = Table("users")
        table.add_column("id", "integer")
        table.add_column("profile_id", "integer")
        table.add_column("comment_id", "integer")
        table.add_foreign_key("profile_id", "profiles", "id")
        table.add_foreign_key("comment_id", "comments", "id")
        table.set_primary_key("id")

        sql = (
            'CREATE TABLE "users" ('
            '"id" INTEGER NOT NULL, "profile_id" INTEGER NOT NULL, "comment_id" INTEGER NOT NULL, '
            'CONSTRAINT users_profile_id_foreign FOREIGN KEY ("profile_id") REFERENCES "profiles"("id"), '
            'CONSTRAINT users_comment_id_foreign FOREIGN KEY ("comment_id") REFERENCES "comments"("id"))'
        )

        self.platform.constraintize(table.get_added_constraints())
        self.assertEqual(self.platform.compile_create_sql(table), [sql])
예제 #7
0
파일: test_table.py 프로젝트: circulon/orm
    def test_create_sql_with_foreign_key_constraint(self):
        table = Table("users")
        table.add_column("id", "integer")
        table.add_column("profile_id", "integer")
        table.add_column("comment_id", "integer")
        table.add_foreign_key("profile_id", "profiles", "id")
        table.add_foreign_key("comment_id", "comments", "id")
        table.set_primary_key("id")

        sql = (
            'CREATE TABLE "users" ('
            "id INTEGER PRIMARY KEY, profile_id INTEGER, comment_id INTEGER, "
            "CONSTRAINT users_profile_id_foreign FOREIGN KEY (profile_id) REFERENCES profiles(id), "
            "CONSTRAINT users_comment_id_foreign FOREIGN KEY (comment_id) REFERENCES comments(id))"
        )

        self.platform.constraintize(table.get_added_constraints())
        self.assertEqual(self.platform.compile_create_sql(table), sql)
예제 #8
0
    def test_alter_advanced_rename_columns(self):
        table = Table("users")
        table.add_column("post", "integer")
        table.add_column("user", "integer")
        table.add_column("email", "integer")

        diff = TableDiff("users")
        diff.from_table = table
        diff.rename_column("post", "comment", "integer")

        sql = [
            "CREATE TEMPORARY TABLE __temp__users AS SELECT post, user, email FROM users",
            "DROP TABLE users",
            'CREATE TABLE "users" (comment INTEGER, user INTEGER, email INTEGER)',
            'INSERT INTO "users" (comment, user, email) SELECT post, user, email FROM __temp__users',
            "DROP TABLE __temp__users",
        ]

        self.assertEqual(sql, self.platform.compile_alter_sql(diff))
예제 #9
0
    def test_alter_rename_column_and_rename_table(self):
        table = Table("users")
        table.add_column("post", "integer")

        diff = TableDiff("users")
        diff.from_table = table
        diff.new_name = "clients"
        diff.rename_column("post", "comment", "integer")

        sql = [
            "CREATE TEMPORARY TABLE __temp__users AS SELECT post FROM users",
            "DROP TABLE users",
            'CREATE TABLE "users" (comment INTEGER)',
            'INSERT INTO "users" (comment) SELECT post FROM __temp__users',
            "DROP TABLE __temp__users",
            "ALTER TABLE users RENAME TO clients",
        ]

        self.assertEqual(sql, self.platform.compile_alter_sql(diff))
예제 #10
0
    def test_alter_rename_column_and_rename_table_and_drop_index(self):
        table = Table("users")
        table.add_column("post", "integer")
        table.add_index("name", "name_unique", "unique")

        diff = TableDiff("users")
        diff.from_table = table
        diff.new_name = "clients"
        diff.rename_column("post", "comment", "integer")
        diff.remove_index("name")

        sql = [
            "DROP INDEX name",
            "CREATE TEMPORARY TABLE __temp__users AS SELECT post FROM users",
            'DROP TABLE "users"',
            'CREATE TABLE "users" ("comment" INTEGER NOT NULL)',
            'INSERT INTO "users" ("comment") SELECT post FROM __temp__users',
            "DROP TABLE __temp__users",
            'ALTER TABLE "users" RENAME TO "clients"',
        ]

        self.assertEqual(sql, self.platform.compile_alter_sql(diff))
예제 #11
0
파일: test_table.py 프로젝트: circulon/orm
    def test_create_sql_with_multiple_unique_constraint(self):
        table = Table("users")
        table.add_column("id", "integer")
        table.add_column("email", "string")
        table.add_column("name", "string")
        table.add_constraint("name", "unique", ["name", "email"])
        table.set_primary_key("id")

        sql = 'CREATE TABLE "users" (id INTEGER PRIMARY KEY, email VARCHAR, name VARCHAR, UNIQUE(name, email))'
        self.platform.constraintize(table.get_added_constraints())
        self.assertEqual(self.platform.compile_create_sql(table), sql)
예제 #12
0
    def test_alter_can_drop_column(self):
        table = Table("users")
        table.add_column("post", "integer")
        table.add_column("name", "string")
        table.add_column("email", "string")

        diff = TableDiff("users")
        diff.from_table = table
        diff.drop_column("post")

        sql = [
            "CREATE TEMPORARY TABLE __temp__users AS SELECT name, email FROM users",
            'DROP TABLE "users"',
            'CREATE TABLE "users" ("name" VARCHAR NOT NULL, "email" VARCHAR NOT NULL)',
            'INSERT INTO "users" ("name", "email") SELECT name, email FROM __temp__users',
            "DROP TABLE __temp__users",
        ]

        self.assertEqual(sql, self.platform.compile_alter_sql(diff))
예제 #13
0
    def test_primary_key(self):
        table = Table("users")
        table.add_column("id", "integer")
        table.set_primary_key("id")

        self.assertEqual(table.primary_key, "id")
예제 #14
0
    def test_add_columns(self):
        table = Table("users")
        table.add_column("name", "string")

        self.assertIsInstance(table.added_columns["name"], Column)